def send(self): """ Sends this PushRequest to the server and return the PushResponse """ def respone_handler(promise): promise.response.text = str(promise.content) self._push_response = self.response_builder(promise.response) assert isinstance(self._push_response, PushResponse) # Executing on_response Callback if self.on_response: self.on_response(self._push_response) return promise.response self._response_promise, _ = Http().request(uri=self.url, method="POST", body=self.body, headers=self.headers, callback=respone_handler) return self
def send(self): """ Sends this PushRequest to the server and return the PushResponse """ def respone_handler(promise): promise.response.text = str(promise.content) self._push_response = self.response_builder(promise.response) assert isinstance(self._push_response, PushResponse) # Executing on_response Callback if self.on_response: self.on_response(self._push_response) return promise.response self._response_promise, _ = Http().request( uri=self.url, method="POST", body=self.body, headers=self.headers, callback=respone_handler ) return self
class PushRequest: """Represents a Push Service request for send a push""" type = None url = None body = None headers = None on_response = None response_builder = None _response_promise = None _push_response = None def __init__(self, type, url, body, headers, response_builder, on_response=None): """ Creates a PushRequest. type: The Push Service type of the request. url: is the URL push resource and can begin with either http or https. body: is the entity body to be sent with the request. It is a string object. headers: is a diccionary with extra headers that are to be sent with the request. response_builder: A callback that receive the Python Response object and returns a PushResponse object. on_response: A callback to be executed after received the PushResponse (Optional). """ self.type = type self.url = url self.body = body self.headers = headers self.on_response = on_response self.response_builder = response_builder #Default response: self._push_response = PushResponse(type=self.type, code=0, is_ok=False, raw='Not sended') def send(self): """ Sends this PushRequest to the server and return the PushResponse """ def respone_handler(promise): promise.response.text = str(promise.content) self._push_response = self.response_builder(promise.response) assert isinstance(self._push_response, PushResponse) # Executing on_response Callback if self.on_response: self.on_response(self._push_response) return promise.response self._response_promise, _ = Http().request(uri=self.url, method="POST", body=self.body, headers=self.headers, callback=respone_handler) return self def wait(self): """ After being sended wait until get the response. """ if self._response_promise is not None: self._response_promise.wait() return self @property def push_response(self): return self.wait()._push_response
class PushRequest: """Represents a Push Service request for send a push""" type = None url = None body = None headers = None on_response = None response_builder = None _response_promise = None _push_response = None def __init__(self, type, url, body, headers, response_builder, on_response=None): """ Creates a PushRequest. type: The Push Service type of the request. url: is the URL push resource and can begin with either http or https. body: is the entity body to be sent with the request. It is a string object. headers: is a diccionary with extra headers that are to be sent with the request. response_builder: A callback that receive the Python Response object and returns a PushResponse object. on_response: A callback to be executed after received the PushResponse (Optional). """ self.type = type self.url = url self.body = body self.headers = headers self.on_response = on_response self.response_builder = response_builder # Default response: self._push_response = PushResponse(type=self.type, code=0, is_ok=False, raw="Not sended") def send(self): """ Sends this PushRequest to the server and return the PushResponse """ def respone_handler(promise): promise.response.text = str(promise.content) self._push_response = self.response_builder(promise.response) assert isinstance(self._push_response, PushResponse) # Executing on_response Callback if self.on_response: self.on_response(self._push_response) return promise.response self._response_promise, _ = Http().request( uri=self.url, method="POST", body=self.body, headers=self.headers, callback=respone_handler ) return self def wait(self): """ After being sended wait until get the response. """ if self._response_promise is not None: self._response_promise.wait() return self @property def push_response(self): return self.wait()._push_response
def __init__(self, base_url=None, http=None): self.base_url = base_url if base_url is not None else \ 'http://proximobus.appspot.com' self.http = http if http is not None else Http()
#!/usr/bin/env python # # from __future__ import absolute_import from asynchttp import Http from pprint import pprint from json import loads import logging fmt = '%(asctime)s %(thread)d %(name)s %(levelname)-8s %(message)s' #logging.basicConfig(level=logging.DEBUG, format=fmt) http = Http() def callback(promise): promise.response.data = loads(promise.content) response, content = http.request('http://proximobus.appspot.com/agencies.json', callback=callback) # do something else here while the request is being downloaded and decoded... pprint(response.data) # handling exceptions in callbacks class SomeException(Exception): pass
#!/usr/bin/env python # # from __future__ import absolute_import from asynchttp import Http from httplib2 import ServerNotFoundError from pprint import pprint import logging fmt = '%(asctime)s %(thread)d %(name)s %(levelname)-8s %(message)s' #logging.basicConfig(level=logging.DEBUG, format=fmt) http = Http() response, content = http.request('http://proximobus.appspot.com/agencies.json') # do something else here while the request is being downloaded and decoded... # if you want to do the decoding of json and some processing in the background # see callback.py pprint(content) response, content = http.request( 'http://some.bad.address.that.does.not.exist/') # do something else where the request is processing, the exception will happen # in the worker. # when you go to access the result the exception will be raised
#!/usr/bin/env python # # from __future__ import absolute_import from asynchttp import Http from httplib2 import ServerNotFoundError from pprint import pprint import logging fmt = '%(asctime)s %(thread)d %(name)s %(levelname)-8s %(message)s' #logging.basicConfig(level=logging.DEBUG, format=fmt) http = Http() response, content = http.request('http://proximobus.appspot.com/agencies.json') # do something else here while the request is being downloaded and decoded... # if you want to do the decoding of json and some processing in the background # see callback.py pprint(content) response, content = http.request('http://some.bad.address.that.does.not.exist/') # do something else where the request is processing, the exception will happen # in the worker. # when you go to access the result the exception will be raised