def _dispatch(self, endpoint: str, data: dict) -> dict: request_url = self._get_url(endpoint, data) try: resp = requests.get(request_url) if resp.status_code != 200: # If the request failed for some reason raise ll_exceptions.ApiException( "API call to `{}` responded with code `{}`\n" "Complete API response: `{}`".format( request_url, resp.status_code, resp.text)) # raise an api exception resp_dict = resp.json() except (requests.exceptions.RequestException, json.JSONDecodeError, ll_exceptions.ApiException ) as e: # Catch all exceptions from the module if isinstance(e, requests.exceptions.ConnectTimeout): raise e # We want to raise this error to allow send_message to retry. if self.fail_silently: # If it should fail silently, it should just return an empty dictionary. resp_dict = {} else: raise e return resp_dict # Returns a json style object of the response.
async def async_send_message(self, endpoint: str, data: dict): """ Send asynchronous messages :param endpoint: The api endpoint :param data: A dict containing data for the request :return: response dict. """ request_url = self._get_url(endpoint, data) try: async with self.sess.get(request_url) as resp: resp_dict = await resp.json() # Don't leak implementation details except asyncio.TimeoutError as e: raise ll_exceptions.TimeoutException(str(e)) except aiohttp.web.HTTPClientError as e: raise ll_exceptions.ApiException(str(e)) except aiohttp.ClientError as e: raise ll_exceptions.NetworkException(str(e)) return resp_dict # Returns a json style object of the response.
def send_message(self, endpoint: str, data: dict) -> dict: """ Send synchronous messages :param endpoint: The api endpoint :param data: A dict containing data for the request :return: response dict. """ request_url = self._get_url(endpoint, data) try: resp = requests.get(request_url) resp.raise_for_status() resp_dict = resp.json() # Don't leak implementation details except requests.exceptions.Timeout as e: raise ll_exceptions.TimeoutException(str(e)) except requests.exceptions.HTTPError as e: raise ll_exceptions.ApiException(str(e)) except requests.exceptions.RequestException as e: raise ll_exceptions.NetworkException(str(e)) return resp_dict # Returns a json style object of the response.