This is an abstract class which purpose is to interface any Rest API using python's standard functionnalities.
Project description
Paradi
Python Abstract Restful Api Dialoger Interface
This is an abstract class which purpose is to interface any Rest API using python's standard functionnalities.
It is meant to be very simple to allow a flexible implementation of the many ways to interact with any API, especially regarding the many different authentication methods.
You should also put in the child class any code that would parse the requests kwargs or read the output data format of the API and turn it into something more readable than a json (like a pandas dataframe).
Example Implementation
The API is hosted at https://exampleapiurl.com
.
To log in, you need to provide a username and a password in a post request at https://exampleapiurl.com/login
which will return you a temporary connection token in the response.
To log out, you need to do a get request at https://exampleapiurl.com/logout
with your credentials.
For any other request, to get your private data at https://exampleapiurl.com/values
you need to provide your credentials in the header.
class Dummy(Paradi):
def __init__(self):
super().__init__(entry=os.getenv('API_ENDPOINT')",
login_uri="login",
logout_uri="logout",
login_kwargs={"headers": {"Content-Type": r"application/x-www-form-urlencoded"},
"data": {"username":os.getenv("API_USERNAME"),
"password":os.getenv("API_PASSWORD")}})
def _save_auth(self,
response: requests.Response
):
self.access_token = json.loads(response.text)['access_token']
self.token_type = json.loads(response.text)['token_type']
self.session.headers.update({'Authorization': f'{self.token_type} {self.access_token}'})
def get_raw_data(self):
response = self.get("values")
return pd.json_normalize(json.loads(response.text), "data")
To use your newly made class you need to use the with
syntax provided by python:
with Dummy() as dummy:
raw_data = dummy.get_raw_data()
This will ensure that the connection with your API is closed as soon as you are finished with the data recovery.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file paradi-0.1.2.tar.gz
.
File metadata
- Download URL: paradi-0.1.2.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd7ad8c665c01d686e7e276be155dbde7fd104aaefe2d2913721aea9531cbfaf |
|
MD5 | 5c1999df9be4e61c4c1fdd9e643866c5 |
|
BLAKE2b-256 | 4fa9c78ff50a3e11cb5f3f9e699e54508559dfe29d6196ade1511100e82c0f94 |
File details
Details for the file paradi-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: paradi-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fae210fcfe44ff939f29e12756a15a953eff987b80861f4202c1c435e2dbebc |
|
MD5 | f1c6561ed9c07b6702c7c7bc9df4b5eb |
|
BLAKE2b-256 | 411299b2ea8c2ca5ea8b2dc68ec25361c548d9d485a06649736cea02ede9d76d |