common - Common code used by other classes

Central code for handling connections to a RestAuth service.

class RestAuthClient.common.RestAuthConnection(host, user, passwd, content_handler='application/json')[source]

An instance of this class represents a connection to a RestAuth service.

Parameters:
  • host (str) – The hostname of the RestAuth service
  • user (str) – The service name to use for authenticating with RestAuth (passed to set_credentials()).
  • passwd (str) – The password to use for authenticating with RestAuth (passed to set_credentials()).
  • content_handler (str or subclass of RestAuthCommon.handlers.content_handler.) – Directly passed to set_content_handler().
delete(url, headers={})[source]

Perform a DELETE request on the connection. This method internally calls the send() function to perform service authentication.

Parameters:
  • url (str) – The URL to perform the GET request on. The URL must not include a query string.
  • headers (dict) – Additional headers to send with this request.
Returns:

The response to the request

Return type:

HTTPResponse

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler() ).
  • InternalServerError – When the server has some internal error.
get(url, params={}, headers={})[source]

Perform a GET request on the connection. This method takes care of escaping parameters and assembling the correct URL. This method internally calls the send() function to perform service authentication.

Parameters:
  • url (str) – The URL to perform the GET request on. The URL must not include a query string.
  • params (dict) – The query parameters for this request. A dictionary of key/value pairs that is passed to urllib.parse.quote().
  • headers (dict) – Additional headers to send with this request.
Returns:

The response to the request

Return type:

HTTPResponse

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler()).
  • InternalServerError – When the server has some internal error.
post(url, params={}, headers={})[source]

Perform a POST request on the connection. This method takes care of escaping parameters and assembling the correct URL. This method internally calls the send() function to perform service authentication.

Parameters:
  • url (str) – The URL to perform the GET request on. The URL must not include a query string.
  • params (dict) – A dictionary that will be wrapped into the request body.
  • headers (dict) – Additional headers to send with this request.
Returns:

The response to the request

Return type:

HTTPResponse

Raises:
  • BadRequest – If the server was unable to parse the request body.
  • Unauthorized – When the connection uses wrong credentials.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler()).
  • UnsupportedMediaType – The server does not support the content type used by this connection.
  • InternalServerError – When the server has some internal error.
put(url, params={}, headers={})[source]

Perform a PUT request on the connection. This method takes care of escaping parameters and assembling the correct URL. This method internally calls the send() function to perform service authentication.

Parameters:
  • url (str) – The URL to perform the GET request on. The URL must not include a query string.
  • params (dict) – A dictionary that will be wrapped into the request body.
  • headers (dict) – Additional headers to send with this request.
Returns:

The response to the request

Return type:

HTTPResponse

Raises:
  • BadRequest – If the server was unable to parse the request body.
  • Unauthorized – When the connection uses wrong credentials.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler()).
  • UnsupportedMediaType – The server does not support the content type used by this connection.
  • InternalServerError – When the server has some internal error.
send(method, url, body=None, headers={})[source]

Send an HTTP request to the RestAuth service. This method is called by the get(), post(), put() and delete() methods. This method takes care of service authentication, encryption and sets Content-Type and Accept headers.

Parameters:
  • method (str) – The HTTP method to use. Must be either “GET”, “POST”, “PUT” or “DELETE”.
  • url (str) – The URL path of the request. This does not include the domain, which is configured by the constructor. The path is assumed to be URL escaped.
  • body (str) – The request body. This (should) only be used by POST and PUT requests. The body is assumed to be URL escaped.
  • headers – A dictionary of key/value pairs of headers to set.
  • headers – dict
Returns:

The response to the request

Return type:

HTTPResponse

Raises:
  • Unauthorized – When the connection uses wrong credentials.
  • NotAcceptable – When the server cannot generate a response in the content type used by this connection (see also: set_content_handler()).
  • InternalServerError – When the server has some internal error.
set_content_handler(content_handler='application/json')[source]

Set the content type used by this connection. The default value is ‘json’, which is supported by the reference server implementation.

Parameters:content_handler (str or content_handler) – Either a self-implemented handler, which must be a subclass of content_handler or a str, which must be one of the MIME types specified in CONTENT_HANDLERS.
set_credentials(user, passwd)[source]

Set the password for the given user. This method is also automatically called by the constructor.

Parameters:
  • user (str) – The user for whom the password should be changed.
  • passwd (str) – The password to use
class RestAuthClient.common.RestAuthResource[source]

Superclass for User and Group objects. The private methods of this class do nothing but prefix all request URLs with the prefix of that class (i.e. /users/).

SSL options

Note

This features requires that you use python 3.2 or later.

A RestAuthConnection instance also has the property ‘context’, which is an ssl.SSLContext instance used for SSL connections. You can directly call methods on this instance if you want to set different SSL options. By default, verify_mode is set to CERT_REQUIRED.

Table Of Contents

Previous topic

Guide

Next topic

restauth_user - user handling

This Page