def callback_to_invoke_in_another_thread(): try: envelope = self._build_envelope(platform_options, endpoint_call_options) if cancellation_event is not None and cancellation_event.isSet( ): # Since there are no way to affect on ongoing request it's response will # be just ignored on cancel call return callback(envelope) except PubNubException as e: logger.error("Async request PubNubException. %s" % str(e)) callback( Envelope( result=None, status=endpoint_call_options.create_status( category=PNStatusCategory.PNBadRequestCategory, response=None, response_info=None, exception=e))) except Exception as e: logger.error("Async request Exception. %s" % str(e)) callback( Envelope(result=None, status=endpoint_call_options.create_status( category=PNStatusCategory. PNInternalExceptionCategory, response=None, response_info=None, exception=e))) finally: call.executed_cb()
def callback_to_invoke_in_separate_thread(): try: envelope = func() callback(envelope.result, envelope.status) except Exception as e: logger.error("Async file upload request Exception. %s" % str(e)) callback(Envelope(result=None, status=e)) finally: call.executed_cb()
def _build_envelope(self, p_options, e_options): """ A wrapper for _invoke_url to separate request logic """ status_category = PNStatusCategory.PNUnknownCategory response_info = None try: res = self._invoke_request(p_options, e_options, self.pubnub.base_origin) except PubNubException as e: if e._pn_error is PNERR_CONNECTION_ERROR: status_category = PNStatusCategory.PNUnexpectedDisconnectCategory elif e._pn_error is PNERR_CLIENT_TIMEOUT: status_category = PNStatusCategory.PNTimeoutCategory return Envelope(result=None, status=e_options.create_status( category=status_category, response=None, response_info=response_info, exception=e)) if res is not None: url = six.moves.urllib.parse.urlparse(res.url) query = six.moves.urllib.parse.parse_qs(url.query) uuid = None auth_key = None if 'uuid' in query and len(query['uuid']) > 0: uuid = query['uuid'][0] if 'auth_key' in query and len(query['auth_key']) > 0: auth_key = query['auth_key'][0] response_info = ResponseInfo(status_code=res.status_code, tls_enabled='https' == url.scheme, origin=url.hostname, uuid=uuid, auth_key=auth_key, client_request=res.request) if res.status_code != requests.codes.ok: if res.status_code == 403: status_category = PNStatusCategory.PNAccessDeniedCategory if res.status_code == 400: status_category = PNStatusCategory.PNBadRequestCategory if res.text is None: text = "N/A" else: text = res.text if res.status_code >= 500: err = PNERR_SERVER_ERROR else: err = PNERR_CLIENT_ERROR return Envelope(result=None, status=e_options.create_status( category=status_category, response=res.json(), response_info=response_info, exception=PubNubException( pn_error=err, errormsg=text, status_code=res.status_code))) else: return Envelope( result=e_options.create_response(res.json()), status=e_options.create_status( category=PNStatusCategory.PNAcknowledgmentCategory, response=res.json(), response_info=response_info, exception=None))