def _get_storage(storage, storage_path):
    if not storage:
        if not storage_path:
            raise GapyError(
                "Must provide either a storage object or a storage_path")
        storage = Storage(filename=storage_path)
    return storage
def from_private_key(account_name,
                     private_key=None,
                     private_key_path=None,
                     storage=None,
                     storage_path=None,
                     api_version="v3",
                     readonly=False,
                     http_client=None,
                     ga_hook=None):
    """Create a client for a service account.

    Create a client with an account name and a private key.

     Args:
      account_name: str, the account identifier (probably the account email).
      private_key: str, the private key as a string.
      private_key_path: str, path to a file with the private key in.
      storage: oauth2client.client.Storage, a Storage implementation to store
               credentials.
      storage_path: str, path to a file storage.
      readonly: bool, default False, if True only readonly access is requested
                from GA.
      http_client: httplib2.Http, Override the default http client used.
      ga_hook: function, a hook that is called every time a query is made
               against GA.
    """
    if not private_key:
        if not private_key_path:
            raise GapyError(
                "Must provide either a private_key or a private_key_file")
        if isinstance(private_key_path, basestring):
            private_key_path = open(private_key_path)
        private_key = private_key_path.read()

    storage = _get_storage(storage, storage_path)

    scope = GOOGLE_API_SCOPE_READONLY if readonly else GOOGLE_API_SCOPE
    credentials = SignedJwtAssertionCredentials(account_name, private_key,
                                                scope)
    credentials.set_store(storage)

    return Client(_build(credentials, api_version, http_client), ga_hook)
 def _item(self, response, id):
     for item in response:
         if item["id"] == id:
             return item
     raise GapyError("Id not found")