Python apiclient.errors.HttpError() Examples

The following are 30 code examples for showing how to use apiclient.errors.HttpError(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module apiclient.errors , or try the search function .

Example 1
Project: calibre-web   Author: janeczku   File: gdriveutils.py    License: 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
Project: calibre-web   Author: janeczku   File: gdriveutils.py    License: 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 3
Project: gcp-census   Author: ocadotechnology   File: model_creator.py    License: 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 4
Project: gcp-census   Author: ocadotechnology   File: model_creator_test.py    License: 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 5
Project: gcp-census   Author: ocadotechnology   File: model_creator_test.py    License: 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 6
Project: gglsbl   Author: afilipovich   File: protocol.py    License: 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 7
Project: googleapps-message-recall   Author: google   File: user_retriever.py    License: 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 8
Project: reliable-task-scheduling-compute-engine-sample   Author: GoogleCloudPlatform   File: cron_executor.py    License: 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
Project: cloud-pubsub-samples-python   Author: GoogleCloudPlatform   File: main.py    License: 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 10
Project: healthcare-deid   Author: GoogleCloudPlatform   File: inspect_dicom.py    License: 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 11
Project: google-drive-trash-cleaner   Author: cfbao   File: cleaner.py    License: 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 12
Project: api-samples   Author: googleanalytics   File: batchqueue.py    License: 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 13
Project: api-samples   Author: googleanalytics   File: batchqueue.py    License: 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 14
Project: crmint   Author: google   File: workers.py    License: 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 15
Project: Zoom2Youtube   Author: Welltory   File: youtube.py    License: 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 16
Project: calibre-web   Author: janeczku   File: gdriveutils.py    License: 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 17
Project: calibre-web   Author: janeczku   File: gdriveutils.py    License: 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 18
Project: edx2bigquery   Author: mitodl   File: table_reader.py    License: 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 19
Project: pajbot   Author: pajbot   File: pleblist.py    License: MIT License 5 votes vote down vote up
def create_pleblist_song_info(youtube_id):
        import isodate
        from apiclient.errors import HttpError

        if PleblistManager.youtube is None:
            log.warning("youtube was not initialized")
            return False

        try:
            video_response = (
                PleblistManager.youtube.videos().list(id=str(youtube_id), part="snippet,contentDetails").execute()
            )
        except HttpError as e:
            log.exception("Youtube HTTPError")
            log.info(e.content)
            log.info(e.resp)
            log.info(e.uri)
            return False
        except:
            log.exception("uncaught exception in videos().list()")
            return False

        if not video_response.get("items", []):
            log.warning(f"Got no valid responses for {youtube_id}")
            return False

        video = video_response["items"][0]

        title = video["snippet"]["title"]
        duration = int(isodate.parse_duration(video["contentDetails"]["duration"]).total_seconds())
        default_thumbnail = video["snippet"]["thumbnails"]["default"]["url"]

        return PleblistSongInfo(youtube_id, title, duration, default_thumbnail) 
Example 20
Project: keyrotator   Author: GoogleCloudPlatform   File: iam_service.py    License: Apache License 2.0 5 votes vote down vote up
def retry_if_500_error(exception):
  """Allow retry if we get a 500 error from IAM API."""
  logging.info("Received %s, retrying...", exception)
  return (isinstance(exception, errors.HttpError)
          and exception.resp.status >= 500
          and exception.resp.status < 600) 
Example 21
Project: gcp-census   Author: ocadotechnology   File: model_creator.py    License: 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 22
Project: gcp-census   Author: ocadotechnology   File: model_creator.py    License: Apache License 2.0 5 votes vote down vote up
def create_missing_views(self):
        project_id = Configuration.get_project_id()
        for view in self.model_provider.list_views():
            logging.debug("Creating BQ view %s:%s.%s",
                          project_id, view.group, view.name)
            body = {
                'tableReference': {
                    'projectId': project_id,
                    'datasetId': view.group,
                    'tableId': view.name
                },
                "view": {
                    "query": view.query
                },
                "description": view.description
            }
            try:
                self.service.tables().insert(
                    projectId=project_id, datasetId=view.group,
                    body=body
                ).execute()
                logging.info("View %s:%s.%s created successfully", project_id,
                             view.group, view.name)
            except HttpError as e:
                if e.resp.status == 409:
                    logging.info("View %s:%s.%s already exists", project_id,
                                 view.group, view.name)
                else:
                    raise e 
Example 23
Project: 52-Weeks-of-Pi   Author: grantwinney   File: Gmail.py    License: MIT License 5 votes vote down vote up
def refresh():
    global unread_count
    try:
        messages = service.users().messages().list(userId='me', q='is:inbox + is:unread').execute()
        unread_count = messages['resultSizeEstimate']
    except errors.HttpError as error:
        print('An error occurred: {0}'.format(error)) 
Example 24
Project: gosync   Author: hschauhan   File: GoSyncModel.py    License: GNU General Public License v2.0 5 votes vote down vote up
def RenameFile(self, file_object, new_title):
        try:
            file = {'name': new_title}

            updated_file = self.drive.files().update( body= file,
                                                         fileId=file_object['id'],
                                                         fields='id, appProperties').execute()
            return updated_file
        except errors.HttpError as error:
            self.SendlToLog(1,'An error occurred while renaming file: %s' % error)
            return None
        except:
            self.logger.exception('An unknown error occurred file renaming file\n')
            return None 
Example 25
Project: gosync   Author: hschauhan   File: GoSyncModel.py    License: GNU General Public License v2.0 5 votes vote down vote up
def TrashFile(self, file_object):
        try:
            file_metadata = {'trashed':True}
            self.drive.files().update(body=file_metadata,fileId=file_object['id']).execute()
            self.SendlToLog(3,{"TRASH_FILE: File %s deleted successfully.\n" % file_object['name']})
        except errors.HttpError as error:
            self.SendlToLog(1,"TRASH_FILE: HTTP Error\n")
            raise RegularFileTrashFailed() 
Example 26
Project: reliable-task-scheduling-compute-engine-sample   Author: GoogleCloudPlatform   File: cron_executor.py    License: Apache License 2.0 5 votes vote down vote up
def run_task(self, msg):
        proc = Popen(self.task_cmd, stdout=PIPE, shell=True)
        stdout_reader = AsynchronousFileReader(proc.stdout, self.io_queue)
        stdout_reader.start()
        while not stdout_reader.eof():
            # read line without blocking
            while True:
                try:
                    # line = self.io_queue.get_nowait() # or q.get(timeout=.1)
                    line = self.io_queue.get_nowait()  # could do timeout=.1
                except Empty:
                    break
                else:
                    self.job_log.info(line)

            lease_age = datetime.now() - self.lease_start
            if lease_age.seconds > (self.ackdeadline - 20):
                # 10 seconds left in lease, renew
                log.debug("extending lease")
                try:
                    resp = self.extend_lease(msg)
                    self.extend_error_ct = 0
                    self.lease_start = datetime.now()
                except HttpError as e:
                    if e.resp.status == 503:
                        # service might return intermitant 503
                        log.warning("PubSub returned 503")
                        self.extend_error_ct += 1
                        if self.extend_error_ct > 5:
                            log.critical(
                                "Too many error responses to extend request")
                            raise
            time.sleep(1)

        retcode = proc.poll()
        if retcode is not None:
            # TODO if error - expire lease immediately?
            # process exited
            log.debug("process ended")
        return retcode 
Example 27
Project: cloud-pubsub-samples-python   Author: GoogleCloudPlatform   File: main.py    License: Apache License 2.0 5 votes vote down vote up
def _setup_topic(self):
        """Creates a topic if it does not exist."""
        topic_name = pubsub_utils.get_full_topic_name()
        try:
            self.client.projects().topics().get(
                topic=topic_name).execute()
        except errors.HttpError as e:
            if e.resp.status == 404:
                self.client.projects().topics().create(
                    name=topic_name, body={}).execute()
            else:
                logging.exception(e)
                raise 
Example 28
Project: gsheets   Author: xflr6   File: conftest.py    License: 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 
Example 29
Project: foos   Author: swehner   File: upload.py    License: 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 30
Project: foos   Author: swehner   File: upload.py    License: 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')