The Transport is a subclass of requests.Session that adds some features that are common in OpenStack APIs or can be globally controlled by an application. Its use is incredibly similar to requests.Session such that we only will cover the differences in detail here.
The common OpenStack functionality added include:
Making a basic HTTP GET call is very simple:
from openstack import transport
trans = transport.Transport()
versions = trans.get('http://cloud.example.com:5000').json()
will retrieve the version data served by the Identity API into a Python dict.
Creating a new object in an OpenStack service is similarly simple:
from openstack import transport
trans = transport.Transport()
new_record = {'name': 'The White Albumn', 'artist': 'The Beatles'}
resp = trans.post('http://cloud.example.com:4999/record', json=new_record)
Passing in the new_record dict with the json keyword argument performs the json.dumps() prior to the request being sent. This is an addition to the capabilities of requests.Session.
Just as in requests.Session, all of the HTTP verbs have corresponding methods in the Transport object.
The verify argument to Transport.request() can now be set when the Transport object is created. It can still be overwritten during any individual call to request() or the HTTP verb methods.
To set the default hostname verification for the Transport to use a custom CA certificate file:
from openstack import transport
trans = transport.Transport(verify='/etc/tls/local-ca-certs.crt')
The same usage from requests is still available. To use the default CA certificate file for a single request:
versions = trans.get('https://cloud.example.com:5000', verify=True)
Or hit on a host with a self-signed certificate:
versions = trans.get('https://cloud.example.com:5000', verify=None)
Redirection handling differs from requests by default as this module is expected to be primarily used for querying REST API servers. The redirection model differs in that requests follows some browser patterns where it will redirect POSTs as GETs for certain statuses which is not want we want for an API.
User-Agent handling as constructed by this class follows RFC 7231 Section 5.5.3. A well-formed user-agent is constructed on name/version product identifiers, such that MyProgram/1.0 is a proper user-agent.
Default value for the HTTP User-Agent header. The default includes the version information of the SDK as well as requests, Python, and the operating system.
Create a new Transport object.
In addition to those listed below, all arguments available to requests.Session are available here:
Parameters: |
|
---|
Send a request
Perform an HTTP request. The following arguments differ from requests.Session:
Parameters: |
|
---|
The following additional kw args are supported:
Parameters: |
|
---|
Remaining kw args from requests.Session.request() supported