Pyjo.GCP.Call - Call of a JSON API of Google Cloud¶
from Pyjo.GCP.Call import Call
from Pyjo.GCP.Client import Client
buckets = Client(service='storage').call(Call('GET', '/storage/v1/b', query={'project': 'myproject'}))
print([bucket['name'] for bucket in buckets.get('items', [])])
# Or import the module, as in Google Python Style Guide
import Pyjo.GCP.Call
import Pyjo.GCP.Client
iam = Pyjo.GCP.Client.new(service='iamcredentials')
call = Pyjo.GCP.Call.new(
'POST',
'/v1/projects/-/serviceAccounts/reader@myproject.iam.gserviceaccount.com:generateAccessToken',
{'scope': ['https://www.googleapis.com/auth/cloud-platform']},
)
iam.call_p(call).then(lambda token: print(token['expireTime'])).wait()
Pyjo.GCP.Call is a call of a JSON API of Google Cloud, the one
protocol of the APIs of Google over HTTP and JSON: a method and a path,
which is percent-encoded already, with the parameters of its query, its
headers and the JSON of its body. The result is the JSON of the response,
its body of another type, or None without a body, and the error is the
Pyjo.GCP.Error of google.rpc.Status of the response. The media
of a request with alt=media, as an object of Cloud Storage, is its body
of any type, also of JSON, as MediaModel of the client of the discovery
documents of Google has it:
data = storage.call(Call('GET', '/storage/v1/b/mybucket/o/config.json', query={'alt': 'media'}))
Without the discovery documents of the APIs, which the client libraries of
Google are generated from, a call takes the names and the structure of the
API as its documentation has them, and gives the result as the response
has it. A subclass changes the transaction and the result, as a call of a
method that gives its body without alt=media, as files.export of
Google Drive, or of the media of the XML API of Cloud Storage:
class Export(Call):
def __init__(self, file_id, mime_type):
super().__init__('GET', f'/drive/v3/files/{file_id}/export', query={'mimeType': mime_type})
def parse(self, tx):
return tx.res.body
See also¶
Pyjo.GCP.Client, Pyjo.GCP.Error, https://google.aip.dev/127.
Classes¶
- class Pyjo.GCP.Call.Pyjo_GCP_Call(method: str, path: str, json: Any = None, *, query: Mapping[str, Any] | None = None, headers: Mapping[str, str] | None = None)[source]¶
Pyjo.GCP.Callinherits all attributes and methods fromPyjo.Baseand implements the following new ones.- __init__(method: str, path: str, json: Any = None, *, query: Mapping[str, Any] | None = None, headers: Mapping[str, str] | None = None) None[source]¶
call = Pyjo.GCP.Call.new('GET', '/storage/v1/b/mybucket/o', query={'prefix': 'docs/'}) call = Pyjo.GCP.Call.new('POST', '/v1/projects/myproject/topics/mytopic:publish', {'messages': [{'data': 'SGVsbG8='}]})
Construct a new
Pyjo.GCP.Callobject.
- __weakref__¶
list of weak references to the object
- build_tx(ua: Pyjo_UserAgent, url: Pyjo_URL) Pyjo_Transaction_HTTP[source]¶
tx = call.build_tx(ua, Pyjo.URL.new('https://storage.googleapis.com'))
The transaction of the method and the path, after the path of the endpoint, of a new
Pyjo.URLobject, which it may change, with the JSON of the body.
- error(tx: Pyjo_Transaction_HTTP) Pyjo_GCP_Error[source]¶
e = call.error(tx)
The
Pyjo.GCP.Errorof a response with an error status, of the JSON ofgoogle.rpc.Statusinerror, or of the status of the response without it.
- headers¶
headers = call.headers
The headers of the request.
- json¶
json = call.json
The JSON of the body of the request, or
Nonefor none.
- method¶
method = call.method
The method of the request, as
GET.
- parse(tx: Pyjo_Transaction_HTTP) Any[source]¶
result = call.parse(tx)
The JSON of a response with a successful status, its body of a type that is not JSON or of a request with
alt=media, orNonewithout a body.
- path¶
path = call.path
The path of the request, which is percent-encoded already, as
/storage/v1/b/mybucket/o/docs%2Findex.html, with the parameters of a query too.
- query¶
query = call.query
The parameters of the query, without those of
None, of strings,trueorfalseof booleans, the strings of the others, and a parameter of each value of a list.
- result(tx: Pyjo_Transaction_HTTP) Any[source]¶
result = call.result(tx)
The result of the response of the call, of
parse(), or thePyjo.GCP.Erroroferror()raised for an error status.
- Pyjo.GCP.Call.url_for(url: Pyjo_URL, path: str, query: Mapping[str, Any] | None = None) Pyjo_URL[source]¶
url = Pyjo.GCP.Call.url_for(url, '/storage/v1/b', {'project': 'myproject', 'maxResults': 10})
The URL of the path of a call, after the path of an endpoint, with a path that is percent-encoded already, as
/a%2Fb, and the parameters of a query, whose values ofvalue_of()are added to those of the path, a parameter of each value of a list, and none ofNone.