apeye.slumber_url
¶
Subclass of URL
with support for interacting with
REST APIs with Slumber and
Requests.
New in version 0.2.0.
Classes:
|
Subclass of |
- class SlumberURL(url='', auth=None, format='json', append_slash=True, session=None, serializer=None, *, timeout=None, allow_redirects=True, proxies=None, verify=None, cert=None)[source]¶
Bases:
URL
Subclass of
URL
with support for interacting with REST APIs with Slumber and Requests.- Parameters
url (
Union
[str
,URL
]) – The url to construct theSlumberURL
object from. Default''
.auth (
Union
[None
,Tuple
[str
,str
],AuthBase
,Callable
[[PreparedRequest
],PreparedRequest
]]) – DefaultNone
.format (
str
) – Default'json'
.session – Default
None
.serializer (
Optional
[SerializerRegistry
]) – DefaultNone
.timeout (
Union
[None
,float
,Tuple
[float
,float
],Tuple
[float
,None
]]) – How long to wait for the server to send data before giving up. DefaultNone
.allow_redirects (
Optional
[bool
]) – Whether to allow redirects. DefaultTrue
.proxies (
Optional
[MutableMapping
[str
,str
]]) – Dictionary mapping protocol or protocol and hostname to the URL of the proxy. DefaultNone
.verify (
Union
[None
,bool
,str
]) – Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use. DefaultNone
.cert (
Union
[str
,Tuple
[str
,str
],None
]) – Either the path to the SSL client cert file (.pem
), or a tuple of('cert', 'key')
. DefaultNone
.
timeout
,allow_redirects
,proxies
,verify
andcert
are passed to Requests when making any HTTP requests, and are inherited by all children created from this URL.Changed in version 0.3.0: The
url
parameter can now be a string or aURL
.Changed in version 1.1.0: When a
RequestsURL
object is deleted or garbage collected, the underlyingrequests.Session
object it only closed if no objects hold references to the session. This prevents the session object of a global object from being inadvertently closed when one of its children is garbage collected.Methods:
__del__
()Attempt to close session when garbage collected to avoid leaving connections open.
delete
(**params)Perform a DELETE request using Slumber.
get
(**params)Perform a GET request using Slumber.
head
(**kwargs)Send a HEAD request using Requests.
options
(**kwargs)Send an OPTIONS request using Requests.
patch
([data, files])Perform a PATCH request using Slumber.
post
([data, files])Perform a POST request using Slumber.
put
([data, files])Perform a PUT request using Slumber.
url
()Returns the URL as a string.
Attributes:
Whether to allow redirects.
The path to ssl client cert file or a tuple of
('cert', 'key')
.Dictionary mapping protocol or protocol and hostname to the URL of the proxy.
The serializer used to (de)serialize the data when interacting with the API.
The underlying requests session.
How long to wait for the server to send data before giving up.
Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use.
- __del__()[source]¶
Attempt to close session when garbage collected to avoid leaving connections open.
- cert¶
Type:
Union
[str
,Tuple
[str
,str
],None
]The path to ssl client cert file or a tuple of
('cert', 'key')
.
- delete(**params)[source]¶
Perform a DELETE request using Slumber.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE
- Parameters
params – Parameters to send in the query string of the
requests.Request
.- Return type
- Returns
- get(**params)[source]¶
Perform a GET request using Slumber.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
- Parameters
params – Parameters to send in the query string of the
requests.Request
.- Return type
- head(**kwargs)[source]¶
Send a HEAD request using Requests.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
- Parameters
kwargs – Optional arguments that
requests.request()
takes. If allow_redirects is not provided, it will be set toFalse
(as opposed to the defaultrequests.request()
behavior).- Return type
CaseInsensitiveDict
- options(**kwargs)[source]¶
Send an OPTIONS request using Requests.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
- Parameters
kwargs – Optional arguments that
requests.request()
takes.- Return type
- patch(data=None, files=None, **params)[source]¶
Perform a PATCH request using Slumber.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH
- Parameters
data – Dictionary, list of tuples, bytes, or file-like object to send in the body of the
requests.Request
. DefaultNone
.files – Dictionary of
'name': file-like-objects
(or{'name': file-tuple}
) for multipart encoding upload.file-tuple
can be a 2-tuple('filename', fileobj)
, 3-tuple('filename', fileobj, 'content_type')
or a 4-tuple('filename', fileobj, 'content_type', custom_headers)
, where'content-type'
is a string defining the content type of the given file andcustom_headers
a dict-like object containing additional headers to add for the file. DefaultNone
.params – Parameters to send in the query string of the
requests.Request
.
- Return type
- post(data=None, files=None, **params)[source]¶
Perform a POST request using Slumber.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
- Parameters
data (
Union
[None
,str
,bytes
,MutableMapping
[str
,Any
],List
[Tuple
[str
,Optional
[str
]]],Tuple
[Tuple
[str
,Optional
[str
]]],IO
]) – Dictionary, list of tuples, bytes, or file-like object to send in the body of therequests.Request
. DefaultNone
.files – Dictionary of
'name': file-like-objects
(or{'name': file-tuple}
) for multipart encoding upload.file-tuple
can be a 2-tuple('filename', fileobj)
, 3-tuple('filename', fileobj, 'content_type')
or a 4-tuple('filename', fileobj, 'content_type', custom_headers)
, where'content-type'
is a string defining the content type of the given file andcustom_headers
a dict-like object containing additional headers to add for the file. DefaultNone
.params – Parameters to send in the query string of the
requests.Request
.
- Return type
- proxies¶
Type:
Optional
[MutableMapping
[str
,str
]]Dictionary mapping protocol or protocol and hostname to the URL of the proxy.
- put(data=None, files=None, **params)[source]¶
Perform a PUT request using Slumber.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT
- Parameters
data – Dictionary, list of tuples, bytes, or file-like object to send in the body of the
requests.Request
. DefaultNone
.files – Dictionary of
'name': file-like-objects
(or{'name': file-tuple}
) for multipart encoding upload.file-tuple
can be a 2-tuple('filename', fileobj)
, 3-tuple('filename', fileobj, 'content_type')
or a 4-tuple('filename', fileobj, 'content_type', custom_headers)
, where'content-type'
is a string defining the content type of the given file andcustom_headers
a dict-like object containing additional headers to add for the file. DefaultNone
.params – Parameters to send in the query string of the
requests.Request
.
- Return type
- serializer¶
Type:
SerializerRegistry
The serializer used to (de)serialize the data when interacting with the API.
New in version 0.6.0.
slumber_url.serializers
¶
JSON and YAML serializers for SlumberURL
.
New in version 0.6.0.
Classes:
Serializer for JSON data. |
|
Base class for serializers. |
|
|
Serializes and deserializes data for transfer to and from a REST API. |
Serializer for YAML data. |
Exceptions:
|
The chosen |
- class JsonSerializer[source]¶
Bases:
Serializer
Serializer for JSON data.
Changed in version 0.6.0: Moved to
apeye.slumber_url.serializers
Methods:
dumps
(data)Serialize data using this
Serializer
.loads
(data)Deserialize data using this
Serializer
.- dumps(data)[source]¶
Serialize data using this
Serializer
.
- loads(data)[source]¶
Deserialize data using this
Serializer
.- Parameters
data (
str
)- Return type
- class Serializer¶
Bases:
ABC
Base class for serializers.
Changed in version 0.6.0: Moved to
apeye.slumber_url.serializers
Attributes:
List of supported content types.
An identifier for the supported data type.
Methods:
dumps
(data)Serialize data using this
Serializer
.Returns the first value from
content_types
.loads
(data)Deserialize data using this
Serializer
.- abstract dumps(data)[source]¶
Serialize data using this
Serializer
.
- get_content_type()[source]¶
Returns the first value from
content_types
.- Return type
- abstract property key: str¶
An identifier for the supported data type.
For example, a YAML serializer would set this to
'yaml'
.- Return type
- abstract loads(data)[source]¶
Deserialize data using this
Serializer
.- Parameters
data (
str
)- Return type
- exception SerializerNotAvailable(content_type)[source]¶
Bases:
apeye.slumber_url.exceptions.SlumberBaseException
The chosen
Serializer
is not available.Changed in version 0.6.0: Moved to
apeye.slumber_url.serializers
- class SerializerRegistry(default='json', serializers=None)[source]¶
Bases:
object
Serializes and deserializes data for transfer to and from a REST API.
- Parameters
default (
str
) – The default serializer to use if none is specified. Corresponds to thekey
of aSerializer
. Default'json'
.serializers (
Optional
[List
[Serializer
]]) – List ofSerializer
objects to use. DefaultNone
.
Changed in version 0.6.0: Moved to
apeye.slumber_url.serializers
Attributes:
The default serializer to use if none is specified.
Mapping of formats to
Serializer
objects.Methods:
dumps
(data[, format])Serialize data of the given format.
get_content_type
([format])Returns the content type for the serializer that supports the given format.
get_serializer
([name, content_type])Returns the first
Serializer
that supports either the given format or the given content type.loads
(data[, format])Deserialize data of the given format.
- get_content_type(format=None)[source]¶
Returns the content type for the serializer that supports the given format.
- get_serializer(name=None, content_type=None)[source]¶
Returns the first
Serializer
that supports either the given format or the given content type.
- serializers¶
Type:
Dict
[str
,Serializer
]Mapping of formats to
Serializer
objects.
- class YamlSerializer[source]¶
Bases:
Serializer
Serializer for YAML data.
Changed in version 0.6.0: Moved to
apeye.slumber_url.serializers
Attention
Either PyYaml or ruamel.yaml must be installed to use this serializer.
Methods:
dumps
(data)Serialize data using this
Serializer
.loads
(data)Deserialize data using this
Serializer
.- dumps(data)[source]¶
Serialize data using this
Serializer
.
- loads(data)[source]¶
Deserialize data using this
Serializer
.- Parameters
data (
str
)- Return type
slumber_url.exceptions
¶
Exceptions for SlumberURL
.
New in version 0.6.0.
Exceptions:
|
Raised when the server tells us there was a client error (4xx). |
|
Raised when the server sends a 404 error. |
|
Raised when the server tells us there was a server error (5xx). |
All Slumber exceptions inherit from this exception. |
|
|
All Slumber HTTP Exceptions inherit from this exception. |
- exception HttpClientError(*args, **kwargs)[source]¶
Bases:
apeye.slumber_url.exceptions.SlumberHttpBaseException
Raised when the server tells us there was a client error (4xx).
Changed in version 0.6.0: Moved to
apeye.slumber_url.exceptions
- exception HttpNotFoundError(*args, **kwargs)[source]¶
Bases:
apeye.slumber_url.exceptions.HttpClientError
Raised when the server sends a 404 error.
Changed in version 0.6.0: Moved to
apeye.slumber_url.exceptions
- exception HttpServerError(*args, **kwargs)[source]¶
Bases:
apeye.slumber_url.exceptions.SlumberHttpBaseException
Raised when the server tells us there was a server error (5xx).
Changed in version 0.6.0: Moved to
apeye.slumber_url.exceptions
- exception SlumberBaseException[source]¶
Bases:
Exception
All Slumber exceptions inherit from this exception.
Changed in version 0.6.0: Moved to
apeye.slumber_url.exceptions
- exception SlumberHttpBaseException(*args, **kwargs)[source]¶
Bases:
apeye.slumber_url.exceptions.SlumberBaseException
All Slumber HTTP Exceptions inherit from this exception.
Changed in version 0.6.0: Moved to
apeye.slumber_url.exceptions