BaseClient¶
All service clients support the low level interface, provided by the
BaseClient, from which all client types inherit.
A client object contains a transport, an object responsible for sending
requests, encoding data, and handling potential retries. It also may include an
optional authorizer, an object responsible for handling token
authentication for requests.
Closing Resources via Clients¶
When used as context managers, clients automatically call their close()
method on exit.
Closing a client closes the transport object attached to it if the transport was created implicitly during init. This means a transport passed in from the outside will not be closed, but one which was created by the client will be.
For most cases, users are recommended to use the context manager form, and to allow clients to both create and close the transport:
from globus_sdk import SearchClient, UserApp
with UserApp("sample-app", client_id="FILL_IN_HERE") as app:
with SearchClient(app=app) as client:
... # any usage
# after the context manager, any transport is implicitly closed
However, if transports are created explicitly, they are not automatically closed, and the user becomes responsible for closing them. For example, in the following usage, the user must close the transport:
from globus_sdk import SearchClient, UserApp
from globus_sdk.transport import RequestsTransport
my_transport = RequestsTransport(http_timeout=120.0)
with UserApp("sample-app", client_id="FILL_IN_HERE") as app:
with SearchClient(app=app, transport=my_transport) as client:
... # any usage
# At this stage, the transport will still be open.
# It should be explicitly closed:
my_transport.close()
Note
The SDK cannot tell whether or not an explicitly passed transport was bound to a name before it was passed. Therefore, in usage like the following, the transport will not automatically be closed:
with SearchClient(app=app, transport=RequestsTransport()) as client:
...
In order to close the transport in such a case, you must explicitly close it.
Since transports are bound to client.transport, the following usage would be a valid
resolution:
with SearchClient(app=app, transport=RequestsTransport()) as client:
...
client.transport.close()
Reference¶
BaseClient¶
- class globus_sdk.BaseClient(*, environment=None, base_url=None, app=None, app_scopes=None, authorizer=None, app_name=None, transport=None, retry_config=None)[source]¶
Abstract base class for clients with error handling for Globus APIs.
- Parameters:
app (GlobusApp | None) – A
GlobusAppwhich will be used for handling authorization and storing and validating tokens. Passing anappwill automatically include a client’s default scopes in theapp’s scope requirements unless specificapp_scopesare given. Ifapp_nameis not given, theapp’sapp_namewill be used. Mutually exclusive withauthorizer.app_scopes (list[Scope] | None) – Optional list of
Scopeobjects to be added toapp’s scope requirements instead ofdefault_scope_requirements. Requiresapp.authorizer (GlobusAuthorizer | None) – A
GlobusAuthorizerwhich will generate Authorization headers. Mutually exclusive withapp.app_name (str | None) – Optional “nice name” for the application. Has no bearing on the semantics of client actions. It is just passed as part of the User-Agent string, and may be useful when debugging issues with the Globus Team. If both``app`` and
app_nameare given, this value takes priority.base_url (str) – The URL for the service. Most client types initialize this value intelligently by default. Set it when inheriting from BaseClient or communicating through a proxy. This value takes precedence over the class attribute of the same name.
transport (RequestsTransport | None) – A
RequestsTransportobject for sending and retrying requests. By default, one will be constructed by the client.retry_config (RetryConfig | None) – A
RetryConfigobject with parameters to control request retry behavior. By default, one will be constructed by the client.
- scopes: ScopeCollection | None = None¶
the scopes for this client may be present as a
ScopeCollection
- attach_globus_app(app, app_scopes=None)[source]¶
Attach a
GlobusAppto this client and, conversely, register this client with that app. The client’s default scopes will be added to the app’s scope requirements unlessapp_scopesis used to override this.If the
app_nameis not set on the client, it will be set to match that of the app.Note
This method is only safe to call once per client object. It is implicitly called if the client is initialized with an app.
- Parameters:
- Raises:
GlobusSDKUsageError – If the attachment appears to conflict with the state of the client. e.g., an app or authorizer is already in place.
- get(path, *, query_params=None, headers=None, automatic_authorization=True)[source]¶
Make a GET request to the specified path.
See
request()for details on the various parameters.- Return type:
- post(path, *, query_params=None, data=None, headers=None, encoding=None, automatic_authorization=True)[source]¶
Make a POST request to the specified path.
See
request()for details on the various parameters.- Return type:
- delete(path, *, query_params=None, headers=None, automatic_authorization=True)[source]¶
Make a DELETE request to the specified path.
See
request()for details on the various parameters.- Return type:
- put(path, *, query_params=None, data=None, headers=None, encoding=None, automatic_authorization=True)[source]¶
Make a PUT request to the specified path.
See
request()for details on the various parameters.- Return type:
- patch(path, *, query_params=None, data=None, headers=None, encoding=None, automatic_authorization=True)[source]¶
Make a PATCH request to the specified path.
See
request()for details on the various parameters.- Return type:
- request(method, path, *, query_params=None, data=None, headers=None, encoding=None, allow_redirects=True, stream=False, automatic_authorization=True)[source]¶
Send an HTTP request
- Parameters:
method (str) – HTTP request method, as an all caps string
path (str) – Path for the request, with or without leading slash
query_params (dict[str, Any] | None) – Parameters to be encoded as a query string
headers (dict[str, str] | None) – HTTP headers to add to the request. Authorization headers may be overwritten unless
automatic_authorizationis False.data (None | str | bytes | Dict[str, Any]) – Data to send as the request body. May pass through encoding.
encoding (str | None) – A way to encode request data. “json”, “form”, and “text” are all valid values. Custom encodings can be used only if they are registered with the transport. By default, strings get “text” behavior and all other objects get “json”.
allow_redirects (bool) – Follow Location headers on redirect response automatically. Defaults to
Truestream (bool) – Do not immediately download the response content. Defaults to
Falseautomatic_authorization (bool) – Use this client’s
apporauthorizerto automatically generate an Authorization header.
- Raises:
GlobusAPIError – a GlobusAPIError will be raised if the response to the request is received and has a status code in the 4xx or 5xx categories
- Return type: