adam.rest_proxy module

rest_proxy.py

Exposes an interface for making calls to the REST API.

Implementations:
  • RestRequests: makes simple calls to REST API.

  • AuthenticatingRestProxy: wraps a RestProxy and adds the auth token to all calls.

  • _RestProxyForTest: mocks methods and exposes extra functionality to add expectations.

class adam.rest_proxy.AccessTokenRefresher

Bases: object

Performs token refresh if the user’s access token has expired.

static is_expired_access_token(response_body)

Determine whether the response indicates an expired token.

The error format is https://github.com/cloudendpoints/endpoints-java/blob/aa4914e66592767bbb0590cb80da75acf2c55db2/endpoints-framework/src/main/java/com/google/api/server/spi/response/RestResponseResultWriter.java#L48-L60 # noqa: E501

Parameters

response_body (dict) – The response body json as a dict.

static refresh_access_token(func)

Decorator for methods that should attempt to refresh the access token.

This mainly applies to REST calls made with authentication information. Firebase automatically expires the access token (aka the Firebase ID token) after an hour. The ADAM server and client will leave the token verification up to Firebase. When an access token expires, ADAM will request a new one from Firebase, given the user’s refresh token.

After an access token is refreshed, it will be saved to the user’s ADAM configuration for the current configuration profile. The configuration profile corresponds to the environments saved by adamctl.

class adam.rest_proxy.AuthenticatingRestProxy(rest_proxy)

Bases: adam.rest_proxy.RestProxy

Rest proxy implementation that wraps another rest proxy and adds the authentication token to every method call.

__init__(rest_proxy)

Initialize self. See help(type(self)) for accurate signature.

delete(path, **kwargs)

Send DELETE request to the server

This function is intended to be overridden by derived classes to DELETE a resource from a real or proxy server

Parameters
  • path (str) – the path to send the DELETE request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Http code

Raises

NotImplementedError – if this does not get overriden by the derived classes

get(path, **kwargs)

Send GET request to the server

This function is intended to be overridden by derived classes to GET a resource from a real or proxy server

Parameters
  • path (str) – the path to send the GET request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (when overridden)

Raises

NotImplementedError – if this does not get overridden by the derived classes

post(path, data_dict, **kwargs)

Send POST request to the server

This function is intended to be overridden by derived classes to POST a resource to a real or proxy server

Parameters
  • path (str) – the path to send the POST to

  • data_dict (dict) – dictionary to be sent in the body of the POST

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (when overridden)

Raises

NotImplementedError – if this does not get overridden by the derived classes

class adam.rest_proxy.BearerAuthc(token)

Bases: requests.auth.AuthBase

Attaches bearer token to request.

__init__(token)

Initialize. :param token: the bearer token :type token: str

class adam.rest_proxy.LoggingRestProxy(rest_proxy)

Bases: adam.rest_proxy.RestProxy

Rest proxy implementation that wraps another rest proxy and adds logging of interesting information such as timing and request size to each call.

__init__(rest_proxy)

Initialize self. See help(type(self)) for accurate signature.

delete(path, **kwargs)

Send DELETE request to the server

This function is intended to be overridden by derived classes to DELETE a resource from a real or proxy server

Parameters
  • path (str) – the path to send the DELETE request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Http code

Raises

NotImplementedError – if this does not get overriden by the derived classes

get(path, **kwargs)

Send GET request to the server

This function is intended to be overridden by derived classes to GET a resource from a real or proxy server

Parameters
  • path (str) – the path to send the GET request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (when overridden)

Raises

NotImplementedError – if this does not get overridden by the derived classes

post(path, data_dict, **kwargs)

Send POST request to the server

This function is intended to be overridden by derived classes to POST a resource to a real or proxy server

Parameters
  • path (str) – the path to send the POST to

  • data_dict (dict) – dictionary to be sent in the body of the POST

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (when overridden)

Raises

NotImplementedError – if this does not get overridden by the derived classes

class adam.rest_proxy.RestProxy

Bases: object

Interface for accessing the server

delete(path, **kwargs)

Send DELETE request to the server

This function is intended to be overridden by derived classes to DELETE a resource from a real or proxy server

Parameters
  • path (str) – the path to send the DELETE request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Http code

Raises

NotImplementedError – if this does not get overriden by the derived classes

get(path, **kwargs)

Send GET request to the server

This function is intended to be overridden by derived classes to GET a resource from a real or proxy server

Parameters
  • path (str) – the path to send the GET request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (when overridden)

Raises

NotImplementedError – if this does not get overridden by the derived classes

post(path, data_dict, **kwargs)

Send POST request to the server

This function is intended to be overridden by derived classes to POST a resource to a real or proxy server

Parameters
  • path (str) – the path to send the POST to

  • data_dict (dict) – dictionary to be sent in the body of the POST

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (when overridden)

Raises

NotImplementedError – if this does not get overridden by the derived classes

class adam.rest_proxy.RestRequests

Bases: adam.rest_proxy.RestProxy

Implementation using requests package

This class is used to send requests to the server.

__init__()

Initialize client with some ADAM configuration.

base_url()
delete(path, **kwargs)

Send DELETE request to the server

This function is used to DELETE a resource from the server

Parameters
  • path (str) – the path to send the DELETE request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data

get(path, **kwargs)

Send GET request to the server

This function is used to GET a resource from the server

Parameters
  • path (str) – the path to send the GET request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data

post(path, data_dict, **kwargs) → Tuple[int, str]

Send POST request to the server

This function is used to POST a resource to the server

Parameters
  • path (str) – the path to send the POST to

  • data_dict (dict) – dictionary to be sent in the body of the POST

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (actual from server)

class adam.rest_proxy.RetryingRestProxy(rest_proxy, num_tries=5)

Bases: adam.rest_proxy.RestProxy

Rest proxy implementation that wraps another rest proxy and retries calls for some errors known to be retryable.

__init__(rest_proxy, num_tries=5)

Initialize self. See help(type(self)) for accurate signature.

delete(path, **kwargs)

Send DELETE request to the server

This function is intended to be overridden by derived classes to DELETE a resource from a real or proxy server

Parameters
  • path (str) – the path to send the DELETE request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Http code

Raises

NotImplementedError – if this does not get overriden by the derived classes

get(path, **kwargs)

Send GET request to the server

This function is intended to be overridden by derived classes to GET a resource from a real or proxy server

Parameters
  • path (str) – the path to send the GET request to

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (when overridden)

Raises

NotImplementedError – if this does not get overridden by the derived classes

post(path, data_dict, **kwargs)

Send POST request to the server

This function is intended to be overridden by derived classes to POST a resource to a real or proxy server

Parameters
  • path (str) – the path to send the POST to

  • data_dict (dict) – dictionary to be sent in the body of the POST

  • kwargs (dict) – Additional arguments to configure requests method calls

Returns

Pair of code and json data (when overridden)

Raises

NotImplementedError – if this does not get overridden by the derived classes