© 2022 Alice Bevan-McGregor and contributors.
An HTTP API client interface with a declarative approach to functional interface definition. With an intuitive and native, Python object-attribute driven interface, no longer do you need to manually muck about constructing URI to pass to HTTPX, Requests, Client/Session middleware, proxy, or your preferred Requests-alike implementation.
Have an API endpoint base path? You're good to go.
from web.api.client import Interface
api = Interface('https://httpbin.org')
print(api.headers.get())
The response will be automatically negotiated and the data returned deserialized as appropriate.
Any access to an otherwise unknown attribute of an Interface instance, or to dereference an item (list- or dict-like) from one, will result in a new instance of the same class specialized to a nested path below the base path. In the above example, the final HTTP GET request issued would be against:
https://httpbin.org/headers
Dereferencing is supported to permit access to child paths that match the names of attributes that do exist, such as the methods for each of the HTTP verbs that may be issued, and to more easily facilitate variable path element substitution without unwieldy use of getattr
.
print(api['get'].get())
code = 304 # Use this way will automatically attempt to cast to a string.
print(api.status[code].get())
No releases have been made available yet. Open source project is in the planning phase.
Installing web.api
is easy, just execute the following in a terminal:
pip install web.api
Note: We strongly recommend always using a container, virtualization, or sandboxing environment of some kind when developing using Python. We highly recommend use of the Python standard venv
("virtual environment") mechanism.
If you add web.api
to the install_requires
argument of the call to setup()
in your application's setup.py
or setup.cfg
files, this project will be automatically installed and made available when your own application or library is installed. Use web.api ~= 1.0.0
to get all bug fixes for the current release while ensuring that large breaking changes are not installed by limiting to the same major/minor, >= the given patch level.
This package has the following dependencies:
- A Python interpreter compatible with CPython 3.7 and or newer, i.e. official CPython or Pypy.
Development takes place on GitHub in the web.api project. Issue tracking, documentation, and downloads are provided there.
Installing the current development version requires Git), a distributed source code management system. If you have Git you can run the following to download and link the development version into your Python runtime:
git clone https://github.com/marrow/web.api.git
pip install -e 'web.api[development]'
You can then upgrade to the latest version at any time, from within that source folder:
git pull
pip install -e '.[development]'
If you would like to make changes and contribute them back to the project, fork the GitHub project, make your changes, and submit a pull request. This process is beyond the scope of this documentation; for more information see GitHub's documentation.
To begin exploring web-based APIs, import the base Interface
or a specialization, and optionally mix-in behaviours, then construct an instance by passing the base path ("root") URI of the API tree.
from web.api.client import Interface
api = Interface('https://httpbin.org')
This base path may be provided as a string or URI
instance. Technically, due to the use of URI
internally, any object providing a __link__
method returning a URI
or string, or that is castable to a URI-like string can be provided.
The result is an API interface: (this programmer's representation is somewhat obvious)
Interface('https://httpbin.org')
Keyword arguments other than uri
, accept
, language
, or ua
will be passed through to the construction of a new user agent instance, if ua
was not provided. If accept
or language
are defined, the Accept
and Accept-Language
headers will be explicitly overridden, respectively, even if an existing user agent was provided.
Once you have the base API interface constructed, you can now descend through attributes and mapping dereferences to locate the endpoint you wish to actually request:
code = 514
endpoint = api.status[code]
This results in the following endpoint
object:
Interface('https://httpbin.org/status/514')
Interface
instances are invokable directly, accepting a string verb as the only positional argument, a _raw
boolean keyword argument which bypasses response processing by returning the underlying Response
instance from the user agent, and will pass along all other keyword arguments down to that user agent.
Utility methods are provided to streamline utilization of certain HTTP verbs. Each of these will issue a request using the specific verb:
options
head
get
post
put
patch
delete
A few of these cases are most frequently used to pass data between client and server, and utilize differing approaches in doing so.
With these methods, keyword arguments are automatically utilized as query string arguments by passing them through as the params
argument in the ultimate call to the user agent. None of these HTTP verbs permit use of the HTTP request body to encode additional information.
Additionally, query string arguments would be critical in determining the result of requests such as these.
When more authoritatively demanding information be persisted through the submission of an encoded request body, keyword arguments are interpreted by default as form-encoded POST data. This is passed through to the user agent call as the data
argument.
This library would not exist without the support and motivation of working on commercial solutions for CEGID, Inc., Alice's employer. The core implementation was written for the RITA Sourcer job offer distribution platform, to facilitate integration of external APIs as data sources. Thank you to CEGID for being willing to, and allowing Alice to open-source components that are not business-critical trade secrets.
This project has yet to make any releases. When it does, each release should be documented here with a sub-section for the version, and a bulleted list of itemized changes tagged with the kind of change, e.g. fixed, added, removed, or deprecated.
The Marrow web.api project has been released under the MIT Open Source license.
Copyright © 2022 Alice Bevan-McGregor and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.