Pyjo.GCP.Client - Client of a JSON API of Google Cloud

from Pyjo.GCP.Call import Call
from Pyjo.GCP.Client import Client

storage = Client(service='storage')
print(storage.call(Call('GET', '/storage/v1/b', query={'project': 'myproject'})))

# Or import the module, as in Google Python Style Guide

import Pyjo.GCP.Call
import Pyjo.GCP.Client

storage = Pyjo.GCP.Client.new(service='storage', scopes=['https://www.googleapis.com/auth/devstorage.read_only'])
call = Pyjo.GCP.Call.new('GET', '/storage/v1/b/mybucket/o', query={'prefix': 'docs/', 'delimiter': '/'})
storage.call_p(call).then(lambda objects: print(objects.get('prefixes'))).wait()

Pyjo.GCP.Client calls a JSON API of Google Cloud, of a service, with the calls of Pyjo.GCP.Call: it authorizes the request of a call with the access token of Pyjo.GCP.Credentials, which Pyjo.GCP.Credentials.Chain finds by default, as the Application Default Credentials, starts it with Pyjo.UserAgent, blocking or non-blocking, and gives the result of its response, or raises the Pyjo.GCP.Error that Google Cloud responds with.

The endpoint of the service is endpoint, or the one of endpoint_url(), as https://storage.googleapis.com, of the universe domain, or of the emulator of <SERVICE>_EMULATOR_HOST, as STORAGE_EMULATOR_HOST, which the client calls without a token, as the client libraries of Google do.

# fake-gcs-server, without credentials
os.environ['STORAGE_EMULATOR_HOST'] = 'localhost:4443'
storage = Pyjo.GCP.Client.new(service='storage')

A provider of credentials calls an API of Google Cloud with steps(), which Pyjo.Steps runs with its own steps, as an impersonated service account with the credentials of its source:

async def fetch(self):
    source = await self.source.steps()
    token = await iam.steps(Call('POST', f'/v1/projects/-/serviceAccounts/{email}:generateAccessToken', ...),
                            source)

See also

Pyjo.GCP.Call, Pyjo.GCP.Error, Pyjo.GCP.Credentials, https://google.aip.dev/auth/4110.

Classes

class Pyjo.GCP.Client.Pyjo_GCP_Client(*, service: str, endpoint: str | None = None, anonymous: bool | None = None, config: Pyjo_GCP_Config | None = None, credentials: Pyjo_GCP_Credentials_Provider | None = None, scopes: Sequence[str] | None = None, quota_project: str | Literal[False] | None = None, ua: Pyjo_UserAgent | None = None)[source]

Pyjo.GCP.Client inherits all attributes and methods from Pyjo.Base and implements the following new ones.

__init__(*, service: str, endpoint: str | None = None, anonymous: bool | None = None, config: Pyjo_GCP_Config | None = None, credentials: Pyjo_GCP_Credentials_Provider | None = None, scopes: Sequence[str] | None = None, quota_project: str | Literal[False] | None = None, ua: Pyjo_UserAgent | None = None) → None[source]
client = Pyjo.GCP.Client.new(service='storage')
client = Pyjo.GCP.Client.new(service='iamcredentials', credentials=provider)
client = Pyjo.GCP.Client.new(service='storage', endpoint='http://127.0.0.1:4443', anonymous=True)

Construct a new Pyjo.GCP.Client object.

__weakref__

list of weak references to the object

anonymous
boolean = client.anonymous

Send the requests without a token, as to an emulator of <SERVICE>_EMULATOR_HOST, which it is by default, or to a public bucket of Cloud Storage.

call(call: Pyjo_GCP_Call) → Any[source]
result = client.call(Pyjo.GCP.Call.new('GET', '/storage/v1/b/mybucket'))

Call a method of the API, blocking, and give its result, or raise its Pyjo.GCP.Error.

call_p(call: Pyjo_GCP_Call) → Pyjo_Promise[source]
promise = client.call_p(Pyjo.GCP.Call.new('GET', '/storage/v1/b/mybucket'))

Same as call(), but non-blocking, and returns a Pyjo.Promise object. The credentials come of credentials_p(), so that the calls while the provider gets them wait for them once.

client_certificate
cert, key = client.client_certificate or (None, None)

The files of the certificate and of the private key that the client presents, of mTLS, of client_certificate(), which ua gets, unless it has a certificate of its own, or None, also of an emulator.

config
config = client.config

The configuration of the endpoint and the default credentials, a new Pyjo.GCP.Config object by default.

credentials: Pyjo_GCP_Credentials_Provider
provider = client.credentials

The provider of the credentials, a new Pyjo.GCP.Credentials.Chain object of config and of the scopes of the argument by default.

endpoint
url = client.endpoint
client.endpoint = 'http://127.0.0.1:4443'

The URL of the endpoint, which comes over the one of config, also a relative one, as an empty one of the server of ua, or None.

endpoint_url() → str[source]
url = client.endpoint_url()

The URL of the endpoint, endpoint, or the one of config, of mTLS too, as https://storage.mtls.googleapis.com.

headers(credentials: Pyjo_GCP_Credentials) → dict[str, str][source]
headers = client.headers(credentials)

The headers of a request with credentials, Authorization of the token, and x-goog-user-project of the quota project of quota_project or of the credentials, as google-auth has them, or a Pyjo.Exception of credentials of another universe domain than the one of the endpoint.

quota_project
project = client.quota_project
client.quota_project = False

The quota project of x-goog-user-project, over the one of the credentials, False for none, or None, for the one of the credentials, as client_options.quota_project_id of the client libraries of Google. A client of Cloud Storage that serves the objects of its own bucket needs none: Cloud Storage bills a request with one to that project, as the one of a bucket of requester pays, and gives its response Cache-Control: no-cache then.

service
service = client.service

The name of the service, as storage, the prefix of its endpoint.

steps(call: Pyjo_GCP_Call, credentials: Pyjo_GCP_Credentials | None = None) → Pyjo_Steps[Any][source]
result = await client.steps(call)
result = await client.steps(call, credentials)

The Pyjo.Steps of a call, of a provider of credentials, with credentials, or those of credentials by default.

ua
ua = client.ua

The user agent of the requests, a new Pyjo.UserAgent object by default.