Core

Config

class ucloud.core.auth.Credential(public_key, private_key, **kwargs)[source]

credential is the object to store credential information

the keys can be found on APIKey documentation

it can calculate signature for OpenAPI:

>>> cred = Credential('my_public_key', 'my_private_key')
>>> cred.verify_ac({"foo": "bar"})
'd4411ab30953fb0bbcb1e7313081f05e4e91a394'
Parameters:
  • public_key
  • private_key
class ucloud.core.client.Config(region=None, project_id=None, base_url='https://api.ucloud.cn', user_agent=None, timeout=30, max_retries=3, log_level=20, ssl_verify=True, ssl_cacert=None, ssl_cert=None, ssl_key=None, **kwargs)[source]

Config is the config of ucloud sdk, use for setting up

Parameters:
  • region (str) – Region is the region of backend service, See also Region list Documentation
  • project_id (str) – ProjectId is the unique identify of project, used for organize resources, Most of resources should belong to a project. Sub-Account must have an project id. See also Project list Documentation
  • base_url (str) – BaseUrl is the url of backend api
  • user_agent – UserAgent is an attribute for sdk client, used for distinguish who is using sdk. See also User Agent It will be appended to the end of sdk user-agent. eg. “MyAPP/0.10.1” -> “Python/3.7.0 Python-SDK/0.1.0 MyAPP/0.10.1”
  • timeout (int) – Timeout is timeout for every request.
  • max_retries (int) – MaxRetries is the number of max retry times. Set MaxRetries more than 0 to enable auto-retry for network and service availability problem if auto-retry is enabled, it will enable default retry policy using exponential backoff.
  • log_level (int) – LogLevel is equal to builtin logging level, if logLevel not be set, use INFO level as default.

Client

class ucloud.core.client.Client(config, transport=None, middleware=None, logger=None)[source]
invoke(action, args=None, **options)[source]

invoke will invoke the action with arguments data and options

Parameters:
  • action (str) – the api action, like CreateUHostInstance
  • args (dict) – arguments of api(action), see doc: UCloud API Documentation
Returns:

Transport

class ucloud.core.transport.RequestsTransport(max_retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504))[source]

transport is the implementation of http client, use for send a request and return a http response

Parameters:
  • max_retries (int) – max retries is the max number of transport request when occur http error
  • backoff_factor (float) – backoff factor will calculate the backoff delay during retrying, the backoff delay = {backoff factor} * (2 ^ ({number of total retries} - 1))
  • status_forcelist (tuple) – the status code list that could be retried
middleware

the middleware object, see :mod:

Returns:the transport middleware
send(req, **options)[source]

send request and return the response

Parameters:req – the full http request descriptor
Returns:the response of http request

Middleware

class ucloud.core.utils.middleware.Middleware[source]

middleware is the object to store request/response handlers

>>> middleware = Middleware()

Add a request handler to prepare the request

>>> @middleware.request
... def prepare(req):
...     req['Region'] = 'cn-bj2'
...     return req

Add a response handler to log the response detail

>>> @middleware.response
... def logged(resp):
...     print(resp)
...     return resp
>>> len(middleware.request_handlers), len(middleware.response_handlers)
(1, 1)
exception(handler, index=-1)[source]

exception is the exception handler register to add exception handler.

Parameters:
  • handler – exception handler function, receive exception object and raise a new exception or ignore it
  • index (int) – the position of handler in the handler list, default is append it to end
Returns:

request(handler, index=-1)[source]

request is the request handler register to add request handler.

Parameters:
  • handler – request handler function, receive request object and return a new request
  • index (int) – the position of request in the handler list, default is append it to end
Returns:

response(handler, index=-1)[source]

response is the response handler register to add response handler.

Parameters:
  • handler – response handler function, receive response object and return a new response
  • index (int) – the position of response in the handler list, default is append it to end
Returns:

Testing