Python apiclient.errors.HttpError() Examples

The following are 30 code examples of apiclient.errors.HttpError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module apiclient.errors , or try the search function .
Example #1
Source File: gdriveutils.py    From calibre-web with GNU General Public License v3.0 6 votes vote down vote up
def watchChange(drive, channel_id, channel_type, channel_address,
              channel_token=None, expiration=None):
    # Watch for all changes to a user's Drive.
    # Args:
    # service: Drive API service instance.
    # channel_id: Unique string that identifies this channel.
    # channel_type: Type of delivery mechanism used for this channel.
    # channel_address: Address where notifications are delivered.
    # channel_token: An arbitrary string delivered to the target address with
    #               each notification delivered over this channel. Optional.
    # channel_address: Address where notifications are delivered. Optional.
    # Returns:
    # The created channel if successful
    # Raises:
    # apiclient.errors.HttpError: if http request to create channel fails.
    body = {
        'id': channel_id,
        'type': channel_type,
        'address': channel_address
    }
    if channel_token:
        body['token'] = channel_token
    if expiration:
        body['expiration'] = expiration
    return drive.auth.service.changes().watch(body=body).execute() 
Example #2
Source File: workers.py    From crmint with Apache License 2.0 6 votes vote down vote up
def retry(self, func, max_retries=DEFAULT_MAX_RETRIES):
    """Decorator implementing retries with exponentially increasing delays."""
    @wraps(func)
    def func_with_retries(*args, **kwargs):
      """Retriable version of function being decorated."""
      tries = 0
      while tries < max_retries:
        try:
          return func(*args, **kwargs)
        except HttpError as e:
          # If it is a client side error, then there's no reason to retry.
          if e.resp.status > 399 and e.resp.status < 500:
            raise e
        except HTTPError as e:
          # If it is a client side error, then there's no reason to retry.
          if e.code > 399 and e.code < 500:
            raise e
        except Exception as e:  # pylint: disable=broad-except
          tries += 1
          delay = 5 * 2 ** (tries + random())
          time.sleep(delay)
      return func(*args, **kwargs)
    return func_with_retries 
Example #3
Source File: batchqueue.py    From api-samples with Apache License 2.0 6 votes vote down vote up
def add(self, request, callback, request_id=None):
    """Adds the request to the queue.

    Args:
      request: HttpRequest, Request to add to the batch.
      callback: callable, A callback to be called for this response, of the
        form callback(id, response, exception). The first parameter is the
        request id, and the second is the deserialized response object. The
        third is an apiclient.errors.HttpError exception object if an HTTP error
        occurred while processing the request, or None if no errors occurred.
      request_id: string, A unique id for the request. The id will be passed to
        the callback with the response.
    """

    # Create a unique id if one does not exist.
    if not request_id:
      request_id = str(uuid.uuid4())

    # Add the callback to the dictionary of call backs.
    self.call_backs[request_id] = callback

    # Add the request to the queue.
    self.queue.put((request, request_id)) 
Example #4
Source File: batchqueue.py    From api-samples with Apache License 2.0 6 votes vote down vote up
def call_back(self, request_id, response, exception):
    """The global call_back method for the BatchQueue instance.

    Called when the API responds.
    It keeps track of the number of times the response is called, and if
    the full quota is freed up, it will call execute.

    Args:
      request_id: The request id.
      response: The deserialized response object.
      exception: The apiclient.errors.HttpError exception object if an HTTP
        error occurred while processing the request, or None if no
        error occurred.
    """
    self.count += 1
    if self.count == self.quota:
      self.count = 0
      self.execute()

    callback = self.call_backs[request_id]
    callback(request_id, response, exception) 
Example #5
Source File: cleaner.py    From google-drive-trash-cleaner with GNU General Public License v3.0 6 votes vote down vote up
def execute_request(request, timeout=TIMEOUT_DEFAULT):
    """Execute Google API request
    Automatic retry upon Google backend error (500) until timeout
    """
    while timeout >= 0:
        try:
            response = request.execute()
        except HttpError as e:
            if int(e.args[0]['status']) == 500:
                timeout -= RETRY_INTERVAL
                time.sleep(RETRY_INTERVAL)
                continue
            raise e
        else:
            return response
    raise TimeoutError 
Example #6
Source File: inspect_dicom.py    From healthcare-deid with Apache License 2.0 6 votes vote down vote up
def get_word_count(vision, image_bytes):
  """Use Vision API to detect the count of words in the image."""
  vision_req_body = {
      'requests': [{
          'image': {
              'content': image_bytes,
          },
          'features': [{
              'type': 'TEXT_DETECTION'
          }]
      }]
  }
  try:
    result = run_deid_lib.request_with_retry(
        vision.annotate(body=vision_req_body).execute)
  except errors.HttpError as error:
    raise error
  if 'error' in result:
    raise Exception('Annotate() failed: {}'.format(result['error']))

  if result and result['responses'] and result['responses'][0]:
    return result['responses'][0]['fullTextAnnotation']['text'].count('\n')
  else:
    return 0 
Example #7
Source File: main.py    From cloud-pubsub-samples-python with Apache License 2.0 6 votes vote down vote up
def _setup_subscription(self):
        """Creates a subscription if it does not exist."""
        subscription_name = pubsub_utils.get_full_subscription_name()
        try:
            self.client.projects().subscriptions().get(
                subscription=subscription_name).execute()
        except errors.HttpError as e:
            if e.resp.status == 404:
                body = {
                    'topic': pubsub_utils.get_full_topic_name(),
                    'pushConfig': {
                        'pushEndpoint': pubsub_utils.get_app_endpoint_url()
                    }
                }
                self.client.projects().subscriptions().create(
                    name=subscription_name, body=body).execute()
            else:
                logging.exception(e)
                raise 
Example #8
Source File: cron_executor.py    From reliable-task-scheduling-compute-engine-sample with Apache License 2.0 6 votes vote down vote up
def get_subscription(self, deadline=60):
        sub = None
        log.debug("getting subscription")
        try:
            # note: subscriptions are a flat namespace in a project
            # we delete then recreate the subscription if it exists
            # so we don't execute old messages

            self.client.projects().subscriptions().delete(
                subscription='projects/{}/subscriptions/{}'.format(
                    self.project, self.subname)).execute()
            log.debug("deleted existing subscription")
        except HttpError as e:
            if e.resp.status == 404:
                sub = self.create_subscription(deadline=deadline)
            else:
                raise
        else:
            sub = self.create_subscription(deadline=deadline)
        log.debug("subscription %s" % sub)
        return sub 
Example #9
Source File: user_retriever.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def GetUserAttributes(self, user_email):
    """Helper to retrieve user attributes from the Admin SDK API.

    Args:
      user_email: String email address of the form user@domain.com.

    Returns:
      Dictionary of user_attributes discovered.

    Raises:
      MessageRecallError: If unable to execute the API call.
    """
    request = self._users_collection.get(userKey=user_email)
    try:
      return request.execute(
          http=credentials_utils.GetAuthorizedHttp(user_email))
    except (HttpError, httplib.HTTPException) as e:
      if e.resp.status == 403:  # If user is not an admin...
        return {}
      raise 
Example #10
Source File: protocol.py    From gglsbl with Apache License 2.0 6 votes vote down vote up
def autoretry(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        global _fail_count
        while True:
            try:
                r = func(*args, **kwargs)
                _fail_count = 0
                return r
            except HttpError as e:
                if not (hasattr(e, 'resp') and 'status' in e.resp
                        and e.resp['status'].isdigit and int(e.resp['status']) >= 500):
                    raise  # we do not want to retry auth errors etc.
                _fail_count += 1
                wait_for = min(2 ** (_fail_count - 1) * 15 * 60 * (1 + random.random()), 24 * 60 * 60)
                log.exception('Call Failed for %s time(s). Retrying in %s seconds: %s',
                              _fail_count, wait_for, str(e))
                time.sleep(wait_for)
            except socket.error:
                transient_error_wait = 2
                log.exception('Socket error, retrying in {} seconds.'.format(transient_error_wait))
                time.sleep(transient_error_wait)
    return wrapper 
Example #11
Source File: model_creator_test.py    From gcp-census with Apache License 2.0 6 votes vote down vote up
def test_should_propagate_table_500_error(self, _create_http):
        # given
        http_mock = Mock(wraps=HttpMockSequence([
            ({'status': '200'}, test_utils.content(
                'tests/json_samples/bigquery_v2_test_schema.json')),
            ({'status': '500'}, '')
        ]))
        _create_http.return_value = http_mock
        model_provider = Mock()
        model_provider.list_tables.return_value = [Table("group", "name1", {})]

        under_test = ModelCreator(model_provider)

        # when
        with self.assertRaises(HttpError) as context:
            under_test.create_missing_tables()

        # then
        calls = http_mock.mock_calls
        self.assertEqual(2, len(calls))
        self.assertEqual(500, context.exception.resp.status) 
Example #12
Source File: model_creator_test.py    From gcp-census with Apache License 2.0 6 votes vote down vote up
def test_should_propagate_dataset_500_error(self, _create_http):
        # given
        http_mock = Mock(wraps=HttpMockSequence([
            ({'status': '200'}, test_utils.content(
                'tests/json_samples/bigquery_v2_test_schema.json')),
            ({'status': '500'}, '')
        ]))
        _create_http.return_value = http_mock
        model_provider = Mock()
        model_provider.list_groups.return_value = ["missing_dataset1"]

        under_test = ModelCreator(model_provider)

        # when
        with self.assertRaises(HttpError) as context:
            under_test.create_missing_datasets()

        # then
        calls = http_mock.mock_calls
        self.assertEqual(2, len(calls))
        self.assertEqual(500, context.exception.resp.status) 
Example #13
Source File: model_creator.py    From gcp-census with Apache License 2.0 6 votes vote down vote up
def __create_dataset_if_missing(self, project_id, dataset_id, location):
        logging.info("Creating dataset %s:%s in %s location",
                     project_id, dataset_id, location)
        body = {
            'datasetReference': {
                'projectId': project_id,
                'datasetId': dataset_id
            },
            'location': location
        }
        try:
            self.service.datasets().insert(
                projectId=project_id, body=body
            ).execute()
        except HttpError as e:
            if e.resp.status == 409:
                logging.info("Dataset %s:%s already exists", project_id,
                             dataset_id)
            else:
                raise e 
Example #14
Source File: gdriveutils.py    From calibre-web with GNU General Public License v3.0 6 votes vote down vote up
def getChangeById (drive, change_id):
    # Print a single Change resource information.
    #
    # Args:
    # service: Drive API service instance.
    # change_id: ID of the Change resource to retrieve.
    try:
        change = drive.auth.service.changes().get(changeId=change_id).execute()
        return change
    except (errors.HttpError) as error:
        log.error(error)
        return None
    except Exception as e:
        log.error(e)
        return None


# Deletes the local hashes database to force search for new folder names 
Example #15
Source File: upload.py    From foos with GNU General Public License v3.0 5 votes vote down vote up
def resumable_upload(insert_request):
    response = None
    error = None
    retry = 0
    while response is None:
        try:
            status, response = insert_request.next_chunk()
            if 'id' in response:
                logger.info("Video id '%s' was successfully uploaded.", response['id'])
                return response['id']
            else:
                logger.error("The upload failed with an unexpected response: %s", response)
                return False
        except HttpError as e:
            if e.resp.status in RETRIABLE_STATUS_CODES:
                error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
            else:
                raise
        except RETRIABLE_EXCEPTIONS as e:
            error = "A retriable error occurred: %s" % e

        if error is not None:
            logger.error(error)
            retry += 1
            if retry > MAX_RETRIES:
                logger.error("No longer attempting to retry.")

            max_sleep = 2 ** retry
            sleep_seconds = random.random() * max_sleep
            logger.error("Sleeping %f seconds and then retrying...", sleep_seconds)
            time.sleep(sleep_seconds) 
Example #16
Source File: workers.py    From crmint with Apache License 2.0 5 votes vote down vote up
def _upload(self):
    with gcs.open(self._file_name, read_buffer_size=self._BUFFER_SIZE) as f:
      media = MediaIoBaseUpload(f, mimetype='application/octet-stream',
                                chunksize=self._BUFFER_SIZE, resumable=True)
      request = self._ga_client.management().uploads().uploadData(
          accountId=self._account_id,
          webPropertyId=self._params['property_id'],
          customDataSourceId=self._params['dataset_id'],
          media_body=media)
      response = None
      tries = 0
      milestone = 0
      while response is None and tries < 5:
        try:
          status, response = request.next_chunk()
        except HttpError, e:
          if e.resp.status in [404, 500, 502, 503, 504]:
            tries += 1
            delay = 5 * 2 ** (tries + random())
            self.log_warn('%s, Retrying in %.1f seconds...', e, delay)
            time.sleep(delay)
          else:
            raise WorkerException(e)
        else:
          tries = 0
        if status:
          progress = int(status.progress() * 100)
          if progress >= milestone:
            self.log_info('Uploaded %d%%.', int(status.progress() * 100))
            milestone += 20
      self.log_info('Upload Complete.') 
Example #17
Source File: youtube.py    From Zoom2Youtube with MIT License 5 votes vote down vote up
def _real_upload_video(self, insert_request):
        response = None
        error = None
        retry = 0
        print('File upload in progress...', end='')
        while response is None:
            try:
                status, response = insert_request.next_chunk()
                print('.', end='')
                if 'id' in response:
                    print()
                    return response['id']
            except HttpError as err:
                if err.resp.status in RETRIABLE_STATUS_CODES:
                    error = True
                else:
                    raise
            except RETRIABLE_EXCEPTIONS:
                error = True

            if error:
                retry += 1
                if retry > MAX_RETRIES:
                    raise Exception('Maximum retry are fail')

                sleep_seconds = random.random() * 2 ** retry
                time.sleep(sleep_seconds) 
Example #18
Source File: workers_tests.py    From crmint with Apache License 2.0 5 votes vote down vote up
def test_retry_raises_error_if_bad_request_error_in_apiclient(self):
    worker = workers.Worker({}, 1, 1)
    def _raise_value_error_exception(*args, **kwargs):
      raise HttpError(mock.Mock(status=400), '')
    fake_request = mock.Mock()
    fake_request.__name__ = 'foo'
    fake_request.side_effect = _raise_value_error_exception
    with self.assertRaises(HttpError):
      worker.retry(fake_request)()
    self.assertEqual(fake_request.call_count, 1) 
Example #19
Source File: gfiles.py    From df2gspread with GNU General Public License v3.0 5 votes vote down vote up
def delete_file(credentials, file_id):
    """DOCS..."""
    try:
        http = credentials.authorize(Http())
        service = discovery.build(
            'drive', 'v3', http=http, cache_discovery=False)
        service.files().delete(fileId=file_id).execute()
    except errors.HttpError as e:
        logr.error(e)
        raise 
Example #20
Source File: permissions.py    From api-samples with Apache License 2.0 5 votes vote down vote up
def call_back(request_id, response, exception):
  """Handle batched request responses."""
  print request_id
  if exception is not None:
    if isinstance(exception, HttpError):
      message = json.loads(exception.content)['error']['message']
      print ('Request %s returned API error : %s : %s ' %
             (request_id, exception.resp.status, message))
  else:
    print response 
Example #21
Source File: batchqueue.py    From api-samples with Apache License 2.0 5 votes vote down vote up
def call_back(request_id, response, exception):
    if exception is not None:
      if isinstance(exception, HttpError):
        message = json.loads(exception.content)['error']['message']
        print ('Request %s returned API error : %s : %s ' %
               (request_id, exception.resp.status, message))
    else:
      print response 
Example #22
Source File: gdriveutils.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def watchFile(drive, file_id, channel_id, channel_type, channel_address,
              channel_token=None, expiration=None):
    """Watch for any changes to a specific file.
    Args:
    service: Drive API service instance.
    file_id: ID of the file to watch.
    channel_id: Unique string that identifies this channel.
    channel_type: Type of delivery mechanism used for this channel.
    channel_address: Address where notifications are delivered.
    channel_token: An arbitrary string delivered to the target address with
                   each notification delivered over this channel. Optional.
    channel_address: Address where notifications are delivered. Optional.
    Returns:
    The created channel if successful
    Raises:
    apiclient.errors.HttpError: if http request to create channel fails.
    """
    body = {
        'id': channel_id,
        'type': channel_type,
        'address': channel_address
    }
    if channel_token:
        body['token'] = channel_token
    if expiration:
        body['expiration'] = expiration
    return drive.auth.service.files().watch(fileId=file_id, body=body).execute() 
Example #23
Source File: gdriveutils.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def stopChannel(drive, channel_id, resource_id):
    """Stop watching to a specific channel.
    Args:
    service: Drive API service instance.
    channel_id: ID of the channel to stop.
    resource_id: Resource ID of the channel to stop.
    Raises:
    apiclient.errors.HttpError: if http request to create channel fails.
    """
    body = {
        'id': channel_id,
        'resourceId': resource_id
    }
    return drive.auth.service.channels().stop(body=body).execute() 
Example #24
Source File: support.py    From mirandum with Apache License 2.0 5 votes vote down vote up
def GetMessage(service, user_id, msg_id):
  try:
    message = service.users().messages().get(userId=user_id, id=msg_id).execute()
    return message
  except errors.HttpError, error:
    print 'An error occurred: %s' % error 
Example #25
Source File: support.py    From mirandum with Apache License 2.0 5 votes vote down vote up
def ListRecentMessagesMatchingQuery(service, user_id, query=''):
  try:
    response = service.users().messages().list(userId=user_id,
                                               q=query, maxResults=10).execute()
    messages = []
    if 'messages' in response:
      messages.extend(response['messages'])

    return messages
  except errors.HttpError, error:
    print 'An error occurred: %s' % error 
Example #26
Source File: utils.py    From twitter-for-bigquery with Apache License 2.0 5 votes vote down vote up
def insert_table(dataset_id, table_id, schema=None):
        
        schema_file = None

        if config.MODE == QueryBuilder.GNIP:
            schema_file = "./schema/schema_gnip.json"
        else:
            schema_file = "./schema/schema_twitter.json"
        
        schema_str = Utils.read_file(schema_file)
        schema = json.loads(schema_str)
        
        body = {
            "tableReference" : {
                "projectId" : config.PROJECT_ID,
                "tableId" : table_id,
                "datasetId" : dataset_id
            },
            "schema" : {
                "fields" : schema
            }
        }

        response = None
        try:
            response = Utils.get_bq().tables().insert(projectId=config.PROJECT_ID, datasetId=dataset_id, body=body).execute()
        except HttpError, e:
            # HttpError 409 when requesting URI returned 
            # "Already Exists: Table twitter-for-bigquery:gnip.tweets_nbafinals"
            if e.resp.status == 409:
                response = True
            else:
                raise e 
Example #27
Source File: table_reader.py    From edx2bigquery with GNU General Public License v2.0 5 votes vote down vote up
def read_one_page(self, max_results=READ_CHUNK_SIZE):
    '''Reads one page from the table.'''

    while True:
      try:
        if self.rows_left is not None and self.rows_left < max_results:
          max_results = self.rows_left

        data = self.bq_service.tabledata().list(
            projectId=self.project_id,
            datasetId=self.dataset_id,
            tableId=self.get_table_id(),
            startIndex=self.next_index,
            pageToken=self.next_page_token,
            maxResults=max_results).execute()
        next_page_token = data.get('pageToken', None)
        rows = data.get('rows', [])
        print self.make_read_message(len(rows), max_results)
        is_done = self.advance(rows, next_page_token)
        return (is_done, rows)
      except HttpError, err:
        # If the error is a rate limit or connection error, wait and
        # try again.
        if err.resp.status in [403, 500, 503]:
          print '%s: Retryable error %s, waiting' % (
              self.thread_id, err.resp.status,)
          time.sleep(5)
        else: raise 
Example #28
Source File: upload.py    From foos with GNU General Public License v3.0 5 votes vote down vote up
def process_event(self, ev):
        if ev.name == 'score_changed':
            self.current_score = ev.data['yellow'], ev.data['black']
        elif ev.name == 'replay_start':
            self.replay_data = ev.data

        if ev.name != 'upload_request':
            return

        self.bus.notify('upload_start')
        text = 'Replay'
        if self.replay_data.get('type') == 'goal':
            text = '{} goal'.format(self.replay_data.get('team', '?').capitalize())

        title = "{}: {}-{}".format(text, self.current_score[0], self.current_score[1])
        logger.info("Uploading video: %s", title)

        try:
            in_file = os.path.join(config.replay_path, 'replay_long.h264')
            out_file = os.path.join(config.replay_path, 'replay_long.mp4')
            call_and_log(["video/convert.sh", in_file, out_file])
            video_id = initialize_upload(title, out_file)
            url = 'http://www.youtube.com/watch?v={}'.format(video_id)
            self.bus.notify('upload_ok', url)
            return
        except HttpError as e:
            logger.error("An HTTP error %d occurred:\n%s", e.resp.status, e.content)
        except Exception as e:
            logger.error("An error occurred: %s", e)

        self.bus.notify('upload_error') 
Example #29
Source File: model_creator.py    From gcp-census with Apache License 2.0 5 votes vote down vote up
def create_missing_tables(self):
        project_id = Configuration.get_project_id()
        for table in self.model_provider.list_tables():
            logging.debug("Creating BQ table %s:%s.%s",
                          project_id, table.group, table.name)
            body = {
                'tableReference': {
                    'projectId': project_id,
                    'datasetId': table.group,
                    'tableId': table.name
                }
            }
            body.update(table.json_dict)
            try:
                self.service.tables().insert(
                    projectId=project_id, datasetId=table.group,
                    body=body
                ).execute()
                logging.info("Table %s:%s.%s created successfully", project_id,
                             table.group, table.name)
            except HttpError as e:
                if e.resp.status == 409:
                    logging.info("Table %s:%s.%s already exists", project_id,
                                 table.group, table.name)
                else:
                    raise e 
Example #30
Source File: conftest.py    From gsheets with MIT License 5 votes vote down vote up
def spreadsheet_404(mocker, services):
    from apiclient.errors import HttpError
    http404 = HttpError(resp=mocker.NonCallableMock(status=404), content=b'')
    services.sheets.spreadsheets.return_value.get.return_value.execute.side_effect = http404

    yield http404