apeye¶
Handy tools for working with URLs and APIs.
Overview¶
apeye
provides:
apeye.url
:pathlib.Path
-like objects to represent URLsCache
: A JSON-backed cache decorator for functionsRateLimitAdapter
: A CacheControl adapter to limit the rate of requests
Installation¶
python3 -m pip install apeye --user
First add the required channels
conda config --add channels https://conda.anaconda.org/conda-forge
conda config --add channels https://conda.anaconda.org/domdfcoding
Then install
conda install apeye
python3 -m pip install git+https://github.com/domdfcoding/apeye@master --user
Attention
In v0.9.0 and above the rate_limiter
module requires the limiter
extra to be installed:
python -m pip install apeye[limiter]
API Reference¶
apeye.url
¶
Source code: apeye_core/__init__.py
pathlib
-like approach to URLs.
Changed in version 0.2.0: SlumberURL
and RequestsURL
moved to apeye.slumber_url
and apeye.requests_url
respectively.
Note
The classes in this module can instead be imported from the apeye_core module instead.
Classes:
|
|
|
|
|
Represents the path part of a URL. |
Data:
Invariant |
|
Invariant |
- URLType = TypeVar(URLType, bound=URL)¶
Type:
TypeVar
Invariant
TypeVar
bound toapeye.url.URL
.
- URLPathType = TypeVar(URLPathType, bound=URLPath)¶
Type:
TypeVar
Invariant
TypeVar
bound toapeye.url.URLPath
.
- class URL(url='')[source]¶
Bases:
PathLike
pathlib
-like class for URLs.Changed in version 0.3.0: The
url
parameter can now be a string or aURL
.Changed in version 1.1.0: Added support for sorting and rich comparisons (
<
,<=
,>
and>=
).Methods:
__eq__
(other)Return
self == other
.Returns the file system path representation of the
URL
.__repr__
()Returns the string representation of the
URL
.__str__
()Returns the
URL
as a string.__truediv__
(key)from_parts
(scheme, netloc, path[, query, …])Construct a
URL
from a scheme, netloc and path.joinurl
(*args)Construct a new
URL
object by combining the given arguments with this instance’s path part.relative_to
(other)Returns a version of this URL’s path relative to
other
.strict_compare
(other)Return
self ≡ other
, comparing the scheme, netloc, path, fragment and query parameters.with_name
(name[, inherit])Return a new
URL
with the file name changed.with_suffix
(suffix[, inherit])Returns a new
URL
with the file suffix changed.Attributes:
Returns a
apeye.url.URL
object representing the URL without query strings or URL fragments.Returns a
apeye.url.Domain
object representing the domain part of the URL.Returns the Fully Qualified Domain Name of the
URL
.The URL fragment, used to identify a part of the document.
The final path component, if any.
Network location part of the URL
The logical parent of the
URL
.An immutable sequence providing access to the logical ancestors of the
URL
.An object providing sequence-like access to the components in the URL.
The hierarchical path of the URL
The port of number of the URL as an integer, if present.
The query parameters of the URL, if present.
URL scheme specifier
The final path component, minus its last suffix.
The final component’s last suffix, if any.
A list of the final component’s suffixes, if any.
- __class_getitem__ = <bound method GenericAlias of <class 'apeye.url.URL'>>¶
Type:
MethodType
- __eq__(other)[source]¶
Return
self == other
.Attention
URL fragments and query parameters are not compared.
See also
URL.strict_compare()
, which does consider those attributes.- Return type
- __fspath__()[source]¶
Returns the file system path representation of the
URL
.This is comprised of the
netloc
andpath
attributes.- Return type
- __truediv__(key)[source]¶
Construct a new
URL
object for the given child of thisURL
.- Return type
Changed in version 0.7.0:Added support for division by integers.
Now officially supports the new path having a URL fragment and/or query parameters. Any URL fragment or query parameters from the parent URL are not inherited by its children.
- property base_url: apeye.url.URLType¶
Returns a
apeye.url.URL
object representing the URL without query strings or URL fragments.New in version 0.7.0.
- Return type
- property domain: apeye.url.Domain¶
Returns a
apeye.url.Domain
object representing the domain part of the URL.- Return type
- fragment¶
-
The URL fragment, used to identify a part of the document.
None
if absent from the URL.New in version 0.7.0.
- classmethod from_parts(scheme, netloc, path, query=None, fragment=None)[source]¶
Construct a
URL
from a scheme, netloc and path.- Parameters
scheme (
str
) – The scheme of the URL, e.g'http'
.netloc (
str
) – The netloc of the URl, e.g.'bbc.co.uk:80'
.path (
Union
[str
,Path
,PathLike
]) – The path of the URL, e.g.'/news'
.query (
Optional
[Mapping
[Any
,List
]]) – The query parameters of the URL, if present. DefaultNone
.fragment (
Optional
[str
]) – The URL fragment, used to identify a part of the document.None
if absent from the URL. DefaultNone
.
Put together, the resulting path would be
'http://bbc.co.uk:80/news'
- Return type
Changed in version 0.7.0: Added the
query
andfragment
arguments.
- joinurl(*args)[source]¶
Construct a new
URL
object by combining the given arguments with this instance’s path part.New in version 1.1.0.
Except for the final path element any queries and fragments are ignored.
- property parents: Tuple[apeye.url.URLType, ...]¶
An immutable sequence providing access to the logical ancestors of the
URL
.
- property parts: Tuple[str, ...]¶
An object providing sequence-like access to the components in the URL.
To retrieve only the parts of the path, use
URL.path.parts
.
- property port: Optional[int]¶
The port of number of the URL as an integer, if present. Default
None
.New in version 0.7.0.
- query¶
-
The query parameters of the URL, if present.
New in version 0.7.0.
- relative_to(other)[source]¶
Returns a version of this URL’s path relative to
other
.New in version 1.1.0.
- strict_compare(other)[source]¶
Return
self ≡ other
, comparing the scheme, netloc, path, fragment and query parameters.New in version 0.7.0.
- Return type
- property suffix: str¶
The final component’s last suffix, if any.
This includes the leading period. For example:
'.txt'
.- Return type
- property suffixes: List[str]¶
A list of the final component’s suffixes, if any.
These include the leading periods. For example:
['.tar', '.gz']
.
- with_name(name, inherit=True)[source]¶
Return a new
URL
with the file name changed.- Parameters
- Return type
Changed in version 0.7.0: Added the
inherit
parameter.
- class URLPath(*args)¶
Bases:
PurePosixPath
Represents the path part of a URL.
Subclass of
pathlib.PurePosixPath
that provides a subset of its methods.Changed in version 1.1.0: Implemented
is_absolute()
,joinpath()
,relative_to()
,match()
,anchor
,drive
, and support for rich comparisons (<
,<=
,>
and>=
), which previously raisedNotImplementedError
.Methods:
Return the bytes representation of the path.
__eq__
(other)Return
self == other
.__repr__
()Return a string representation of the
URLPath
.__rtruediv__
(key)Return
value / self
.__str__
()Return the string representation of the path, suitable for passing to system calls.
__truediv__
(key)Return
self / value
.Returns whether the path is absolute (i.e.
is_relative_to
(*other)Return True if the path is relative to another path or False.
Return True if the path contains one of the special names reserved by the system, if any.
joinpath
(*args)Combine this
URLPath
with one or several arguments.relative_to
(*other)Returns the relative path to another path identified by the passed arguments.
with_name
(name)Return a new path with the file name changed.
with_stem
(stem)Return a new path with the stem changed.
with_suffix
(suffix)Return a new path with the file suffix changed.
Attributes:
The final path component, if any.
The logical parent of the path.
A sequence of this path’s logical parents.
An object providing sequence-like access to the components in the filesystem path.
The root of the path, if any.
The final path component, minus its last suffix.
The final component’s last suffix, if any.
A list of the final component’s suffixes, if any.
- __bytes__()¶
Return the bytes representation of the path. This is only recommended to use under Unix.
- __rtruediv__(key)¶
Return
value / self
.
- __str__()[source]¶
Return the string representation of the path, suitable for passing to system calls.
- Return type
- __truediv__(key)¶
Return
self / value
.
- is_absolute()[source]¶
Returns whether the path is absolute (i.e. starts with
/
).New in version 1.1.0: previously raised
NotImplementedError
.- Return type
- is_relative_to(*other)¶
Return True if the path is relative to another path or False.
- is_reserved()¶
Return True if the path contains one of the special names reserved by the system, if any.
- joinpath(*args)[source]¶
Combine this
URLPath
with one or several arguments.New in version 1.1.0: previously raised
NotImplementedError
.- Return type
- Returns
A new
URLPath
representing either a subpath (if all arguments are relative paths) or a totally different path (if one of the arguments is absolute).
- property name¶
The final path component, if any.
- property parent¶
The logical parent of the path.
- property parents¶
A sequence of this path’s logical parents.
- property parts¶
An object providing sequence-like access to the components in the filesystem path.
- relative_to(*other)[source]¶
Returns the relative path to another path identified by the passed arguments.
The arguments are joined together to form a single path, and therefore the following behave identically:
>>> URLPath("/news/sport").relative_to("/", "news") URLPath('sport') >>> URLPath("/news/sport").relative_to("/news") URLPath('sport')
New in version 1.1.0: previously raised
NotImplementedError
.- Parameters
- Raises
ValueError – if the operation is not possible (because this is not a subpath of the other path)
See also
relative_to()
, which is recommended when constructing a relative path from aURL
. This method cannot correctly handle some cases, such as:>>> URL("https://github.com/domdfcoding").path.relative_to(URL("https://github.com").path) Traceback (most recent call last): ValueError: '/domdfcoding' does not start with ''
Since
URL("https://github.com").path
isURLPath('')
.Instead, use:
>>> URL("https://github.com/domdfcoding").relative_to(URL("https://github.com")) URLPath('domdfcoding')
- Return type
- property root¶
The root of the path, if any.
- property stem¶
The final path component, minus its last suffix.
- property suffix¶
The final component’s last suffix, if any.
This includes the leading period. For example: ‘.txt’
- property suffixes¶
A list of the final component’s suffixes, if any.
These include the leading periods. For example: [‘.tar’, ‘.gz’]
- with_name(name)¶
Return a new path with the file name changed.
- with_stem(stem)¶
Return a new path with the stem changed.
- with_suffix(suffix)¶
Return a new path with the file suffix changed. If the path has no suffix, add given suffix. If the given suffix is an empty string, remove the suffix from the path.
- namedtuple Domain(subdomain, domain, suffix)¶
typing.NamedTuple
of a URL’s subdomain, domain, and suffix.- Fields
- property fqdn¶
Returns a Fully Qualified Domain Name, if there is a proper domain/suffix.
>>> URL('https://forums.bbc.co.uk/path/to/file').domain.fqdn 'forums.bbc.co.uk' >>> URL('https://localhost:8080').domain.fqdn ''
- property ipv4: Optional[ipaddress.IPv4Address]¶
Returns the ipv4 if that is what the presented domain/url is.
>>> URL('https://127.0.0.1/path/to/file').domain.ipv4 IPv4Address('127.0.0.1') >>> URL('https://127.0.0.1.1/path/to/file').domain.ipv4 >>> URL('https://256.1.1.1').domain.ipv4
- Return type
- property registered_domain¶
Joins the domain and suffix fields with a dot, if they’re both set.
>>> URL('https://forums.bbc.co.uk').domain.registered_domain 'bbc.co.uk' >>> URL('https://localhost:8080').domain.registered_domain ''
apeye.requests_url
¶
Extension of URL
with support for interacting with the website using the Requests library.
New in version 0.2.0.
Classes:
|
Extension of |
|
Extension of |
Data:
Invariant |
- class RequestsURL(url='')[source]¶
Bases:
URL
Extension of
URL
with support for interacting with the website using the Requests library.The
requests.Session
used for this object – and all objects created using the/
or.parent
operations – can be accessed using thesession
attribute. If desired, this can be replaced with a different session object, such as one using caching.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
(**kwargs)Send a DELETE request using Requests.
get
([params])Perform a GET request using Requests.
head
(**kwargs)Send a HEAD request using Requests.
options
(**kwargs)Send an OPTIONS request using Requests.
patch
([data, json])Send a PATCH request using Requests.
post
([data, json])Send a POST request using Requests.
put
([data, json])Send a PUT request using Requests.
resolve
([timeout])Resolves the URL into its canonical form.
Attributes:
The underlying requests session.
- __del__()[source]¶
Attempt to close session when garbage collected to avoid leaving connections open.
- delete(**kwargs)[source]¶
Send a DELETE request using Requests.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE
- Parameters
**kwargs – Optional arguments that
requests.request()
takes.- Return type
- get(params=None, **kwargs)[source]¶
Perform a GET request using Requests.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
- Parameters
params (
Union
[Mapping
[Union
[str
,bytes
,int
,float
],Union
[str
,bytes
,int
,float
,Iterable
[Union
[str
,bytes
,int
,float
]]]],str
,bytes
,Tuple
[Union
[str
,bytes
,int
,float
],Union
[str
,bytes
,int
,float
,Iterable
[Union
[str
,bytes
,int
,float
]]]],None
]) – Dictionary, list of tuples or bytes to send in the query string for therequests.Request
. DefaultNone
.**kwargs – Optional arguments that
requests.request()
takes.
Changed in version 0.7.0: If
params
isNone
but the URL has a query string, the query string will be parsed and used forparams
.- 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 to False (as opposed to the defaultrequests.request()
behavior).- Return type
- 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, json=None, **kwargs)[source]¶
Send a PATCH request using Requests.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH
- 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
.json – json data to send in the body of the
requests.Request
. DefaultNone
.**kwargs – Optional arguments that
requests.request()
takes.
- Return type
- post(data=None, json=None, **kwargs)[source]¶
Send a POST request using Requests.
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
.json – json data to send in the body of the
requests.Request
. DefaultNone
.**kwargs – Optional arguments that
requests.request()
takes.
- Return type
- put(data=None, json=None, **kwargs)[source]¶
Send a PUT request using Requests.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT
- 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
.json – json data to send in the body of the
requests.Request
. DefaultNone
.**kwargs – Optional arguments that
requests.request()
takes.
- Return type
- class TrailingRequestsURL(url='')[source]¶
Bases:
RequestsURL
Extension of
RequestsURL
which adds a trailing slash to the end of the URL.New in version 0.5.0.
- __str__()[source]¶
Returns the
TrailingRequestsURL
as a string.- Return type
- _R = TypeVar(_R, bound=RequestsURL)¶
Type:
TypeVar
Invariant
TypeVar
bound toapeye.requests_url.RequestsURL
.
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
apeye.cache
¶
Caching functions for functions.
See also
- class Cache(app_name)[source]¶
Bases:
object
Cache function arguments to and in-memory dictionary and a JSON file.
- Parameters
app_name (
str
) – The name of the app. This dictates the name of the cache directory.
Methods:
__call__
(func)Decorator to cache the return values of a function based on its inputs.
clear
([func])Clear the cache.
load_cache
(func)Loads the cache for the given function.
Attributes:
The name of the app.
The location of the cache directory on disk.
Mapping of function names to their caches.
- __call__(func)[source]¶
Decorator to cache the return values of a function based on its inputs.
- Parameters
func (
Callable
)
apeye.email_validator
¶
Source code: apeye_core/email_validator.py
Email address validation functions.
New in version 1.0.0.
This module is a subset of https://pypi.org/project/email-validator
Note
The classes in this module can instead be imported from the apeye_core.email_validator module instead.
Exceptions:
Exception raised when an email address fails validation because of its form. |
Classes:
|
Represents the return type of the |
Functions:
|
Validates an email address. |
|
Validate the domain part of an email address (the part after the @-sign). |
|
Validates the local part of an email address (the part before the @-sign). |
- exception EmailSyntaxError[source]¶
Bases:
ValueError
Exception raised when an email address fails validation because of its form.
- class ValidatedEmail(original_email, email, local_part, domain, *, ascii_email=None, ascii_local_part=None, ascii_domain=None, smtputf8=None)¶
Bases:
object
Represents the return type of the
validate_email()
function.This class holds the normalized form of the email address alongside other information.
- Parameters
original_email (
str
) – The original, unnormalized email address.email (
str
) – The normalized email address, which should always be used in preference to the original address.local_part (
str
) – The local part of the email address after Unicode normalization.domain (
str
) – The domain part of the email address after Unicode normalization or conversion to Unicode from IDNA ascii.ascii_email (
Optional
[str
]) – If notNone
, a form of the email address that uses 7-bit ASCII characters only. DefaultNone
.ascii_local_part (
Optional
[str
]) – If notNone
, the local part of the email address using 7-bit ASCII characters only. DefaultNone
.ascii_domain (
Optional
[str
]) – If notNone
, a form of the domain name that uses 7-bit ASCII characters only. DefaultNone
.smtputf8 (
Optional
[bool
]) – Indicates whether SMTPUTF8 will be required to transmit messages to this address. DefaultNone
.
Methods:
__eq__
(other)Return
self == other
.__repr__
()Return a string representation of the
ValidatedEmail
object.__str__
()Return a string representation of the
ValidatedEmail
object.as_dict
()Convenience method for accessing the
ValidatedEmail
as a dict.- __repr__()[source]¶
Return a string representation of the
ValidatedEmail
object.- Return type
- __str__()[source]¶
Return a string representation of the
ValidatedEmail
object.- Return type
- validate_email(email, allow_smtputf8=True, allow_empty_local=False)[source]¶
Validates an email address.
- Parameters
- Raises
EmailSyntaxError – if the address is not valid
- Return type
- validate_email_domain_part(domain)[source]¶
Validate the domain part of an email address (the part after the @-sign).
apeye.rate_limiter
¶
Rate limiters for making calls to external APIs in a polite manner.
Attention
This module has the following additional requirements:
cachecontrol[filecache]>=0.12.6 filelock>=3.8.0; python_version >= "3.7" lockfile>=0.12.2; python_version < "3.7"
These can be installed as follows:
python -m pip install apeye[limiter]
Classes:
|
Cache HTTP requests for up to 28 days and limit the rate of requests to no more than 5/second. |
|
Custom |
Functions:
|
Decorator to force a function to run no less than |
- class HTTPCache(app_name, expires_after=datetime.timedelta(days=28))[source]¶
Cache HTTP requests for up to 28 days and limit the rate of requests to no more than 5/second.
- Parameters
Attributes:
The name of the app.
The location of the cache directory on disk.
Mapping of function names to their caches.
Methods:
clear
()Clear the cache.
- rate_limit(min_time=0.2, logger=None)[source]¶
Decorator to force a function to run no less than
min_time
seconds after it last ran. Used for rate limiting.- Parameters
- Return type
- class RateLimitAdapter(cache=None, cache_etags=True, controller_class=None, serializer=None, heuristic=None, cacheable_methods=None, *args, **kw)[source]¶
Bases:
CacheControlAdapter
Custom
cachecontrol.adapter.CacheControlAdapter
to limit the rate of requests to 5 per second.- Parameters
Methods:
rate_limited_send
(*args, **kwargs)Wrapper around
CacheControlAdapter.send
to limit the rate of requests.send
(request[, cacheable_methods])Send a request.
- rate_limited_send(*args, **kwargs)[source]¶
Wrapper around
CacheControlAdapter.send
to limit the rate of requests.- Return type
- send(request, cacheable_methods=None, **kwargs)[source]¶
Send a request.
Use the request information to see if it exists in the cache and cache the response if we need to and can.
- Parameters
request (
PreparedRequest
) – Therequests.PreparedRequest
being sent.cacheable_methods (
Optional
[Collection
[str
]]) – DefaultNone
.**kwargs – Additional arguments taken by
requests.adapters.HTTPAdapter.send()
(e.g.timeout
).
- Return type
Contributing¶
apeye
uses tox to automate testing and packaging,
and pre-commit to maintain code quality.
Install pre-commit
with pip
and install the git hook:
python -m pip install pre-commit
pre-commit install
Coding style¶
formate is used for code formatting.
It can be run manually via pre-commit
:
pre-commit run formate -a
Or, to run the complete autoformatting suite:
pre-commit run -a
Automated tests¶
Tests are run with tox
and pytest
.
To run tests for a specific Python version, such as Python 3.6:
tox -e py36
To run tests for all Python versions, simply run:
tox
Build documentation locally¶
The documentation is powered by Sphinx. A local copy of the documentation can be built with tox
:
tox -e docs
Downloading source code¶
The apeye
source code is available on GitHub,
and can be accessed from the following URL: https://github.com/domdfcoding/apeye
If you have git
installed, you can clone the repository with the following command:
git clone https://github.com/domdfcoding/apeye
Cloning into 'apeye'...
remote: Enumerating objects: 47, done.
remote: Counting objects: 100% (47/47), done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 173 (delta 16), reused 17 (delta 6), pack-reused 126
Receiving objects: 100% (173/173), 126.56 KiB | 678.00 KiB/s, done.
Resolving deltas: 100% (66/66), done.

Downloading a ‘zip’ file of the source code¶
Building from source¶
The recommended way to build apeye
is to use tox:
tox -e build
The source and wheel distributions will be in the directory dist
.
If you wish, you may also use pep517.build or another PEP 517-compatible build tool.
License¶
apeye
is licensed under the GNU Lesser General Public License v3.0 or later.
Permissions of this copyleft license are conditioned on making available complete source code of licensed works and modifications under the same license or the GNU GPLv3. Copyright and license notices must be preserved. Contributors provide an express grant of patent rights. However, a larger work using the licensed work through interfaces provided by the licensed work may be distributed under different terms and without source code for the larger work.
Permissions | Conditions | Limitations |
---|---|---|
|
|
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies of this license
document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates the terms
and conditions of version 3 of the GNU General Public License, supplemented
by the additional permissions listed below.
0. Additional Definitions.
As used herein, “this License” refers to version 3 of the GNU Lesser General
Public License, and the “GNU GPL” refers to version 3 of the
GNU General Public License.
“The Library” refers to a covered work governed by this License, other than
an Application or a Combined Work as defined below.
An “Application” is any work that makes use of an interface provided by the
Library, but which is not otherwise based on the Library. Defining a subclass
of a class defined by the Library is deemed a mode of using an interface
provided by the Library.
A “Combined Work” is a work produced by combining or linking an Application
with the Library. The particular version of the Library with which the
Combined Work was made is also called the “Linked Version”.
The “Minimal Corresponding Source” for a Combined Work means the Corresponding
Source for the Combined Work, excluding any source code for portions of the
Combined Work that, considered in isolation, are based on the Application,
and not on the Linked Version.
The “Corresponding Application Code” for a Combined Work means the object code
and/or source code for the Application, including any data and utility programs
needed for reproducing the Combined Work from the Application, but excluding
the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License without
being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a facility
refers to a function or data to be supplied by an Application that uses the
facility (other than as an argument passed when the facility is invoked),
then you may convey a copy of the modified version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the function or
data, the facility still operates, and performs whatever part of its
purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of this
License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from a header
file that is part of the Library. You may convey such object code under terms
of your choice, provided that, if the incorporated material is not limited to
numerical parameters, data structure layouts and accessors, or small macros,
inline functions and templates (ten or fewer lines in length),
you do both of the following:
a) Give prominent notice with each copy of the object code that the Library
is used in it and that the Library and its use are covered by this License.
b) Accompany the object code with a copy of the GNU GPL
and this license document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that, taken together,
effectively do not restrict modification of the portions of the Library
contained in the Combined Work and reverse engineering for debugging such
modifications, if you also do each of the following:
a) Give prominent notice with each copy of the Combined Work that the
Library is used in it and that the Library and its use are covered
by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and
this license document.
c) For a Combined Work that displays copyright notices during execution,
include the copyright notice for the Library among these notices, as well
as a reference directing the user to the copies of the GNU GPL
and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form suitable
for, and under terms that permit, the user to recombine or relink
the Application with a modified version of the Linked Version to
produce a modified Combined Work, in the manner specified by section 6
of the GNU GPL for conveying Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time a
copy of the Library already present on the user's computer system,
and (b) will operate properly with a modified version of the Library
that is interface-compatible with the Linked Version.
e) Provide Installation Information, but only if you would otherwise be
required to provide such information under section 6 of the GNU GPL, and
only to the extent that such information is necessary to install and
execute a modified version of the Combined Work produced by recombining
or relinking the Application with a modified version of the Linked Version.
(If you use option 4d0, the Installation Information must accompany the
Minimal Corresponding Source and Corresponding Application Code. If you
use option 4d1, you must provide the Installation Information in the
manner specified by section 6 of the GNU GPL for
conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the Library side by
side in a single library together with other library facilities that are not
Applications and are not covered by this License, and convey such a combined
library under terms of your choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based on
the Library, uncombined with any other library facilities, conveyed under
the terms of this License.
b) Give prominent notice with the combined library that part of it is a
work based on the Library, and explaining where to find the accompanying
uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions of the
GNU Lesser General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Library as you
received it specifies that a certain numbered version of the GNU Lesser
General Public License “or any later version” applies to it, you have the
option of following the terms and conditions either of that published version
or of any later version published by the Free Software Foundation. If the
Library as you received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser General
Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide whether
future versions of the GNU Lesser General Public License shall apply, that
proxy's public statement of acceptance of any version is permanent
authorization for you to choose that version for the Library.
View the Function Index or browse the Source Code.