def builder(http_transport,
             postproc,
             uri,
             method='GET',
             body=None,
             headers=None,
             methodId=None,
             resumable=None):
   """Builds an HttpRequest, adding headers and response inspection."""
   additional_headers = headers_supplier()
   if additional_headers:
     headers = headers.copy() if headers else {}
     headers.update(additional_headers)
   request = http.HttpRequest(
       http_transport,
       postproc,
       uri,
       method=method,
       body=body,
       headers=headers,
       methodId=methodId,
       resumable=resumable)
   if response_inspector:
     request.add_response_callback(response_inspector)
   return request
Exemplo n.º 2
0
    def send_all(self, messages, dry_run=False):
        """Sends the given messages to FCM via the batch API."""
        if not isinstance(messages, list):
            raise ValueError('Messages must be an list of messaging.Message instances.')
        if len(messages) > 100:
            raise ValueError('send_all messages must not contain more than 100 messages.')

        responses = []

        def batch_callback(_, response, error):
            exception = None
            if error:
                exception = self._parse_batch_error(error)
            send_response = SendResponse(response, exception)
            responses.append(send_response)

        batch = http.BatchHttpRequest(batch_callback, _MessagingService.FCM_BATCH_URL)
        for message in messages:
            body = json.dumps(self._message_data(message, dry_run))
            req = http.HttpRequest(
                http=self._transport,
                postproc=self._postproc,
                uri=self._fcm_url,
                method='POST',
                body=body,
                headers=self._fcm_headers
            )
            batch.add(req)

        try:
            batch.execute()
        except googleapiclient.http.HttpError as error:
            raise self._parse_batch_error(error)
        else:
            return BatchResponse(responses)
Exemplo n.º 3
0
    def setUp(self):
        self.fs = GCSFilesystem()
        self.gcs_path = 'gs://walrus/data'

        self.list_req_mock = mock.MagicMock()

        objects_ret = mock.MagicMock()
        objects_ret.list.return_value = self.list_req_mock
        objects_ret.get_media.return_value = google_http.HttpRequest(
            None, None, self.gcs_path)

        api_client = mock.MagicMock()
        api_client.objects.return_value = objects_ret

        self.fs._api_client = api_client
        self.next_chunk_patch = patch.object(
            google_http.MediaIoBaseDownload, 'next_chunk')
Exemplo n.º 4
0
 def _request_builder(_, *args, **kwargs):
     return http.HttpRequest(AuthorizedHttp(credentials=credentials),
                             *args, **kwargs)
Exemplo n.º 5
0
 def BuildHttpRequest(unused_http, *args, **kwargs):
     if not hasattr(thread_local, "http"):
         thread_local.http = credentials.authorize(httplib2.Http())
     return http.HttpRequest(thread_local.http, *args, **kwargs)