Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

paradi-0.1.3.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

paradi-0.1.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file paradi-0.1.3.tar.gz.

File metadata

  • Download URL: paradi-0.1.3.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for paradi-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ced384c0ff85bee72f189573a8b1f045dcfc134e7250f5f439ab16dba96c3270
MD5 385baab489d36c0b91ddf087594b40ef
BLAKE2b-256 b47552759220b450352acdac0643cc77a90a81a9895bc7baaf1915b5e7b36a14

See more details on using hashes here.

File details

Details for the file paradi-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: paradi-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for paradi-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5e2f612ed370d3f512fb93773557c7d7124d8cdecc3d251bd9e01c7ad99b5ec6
MD5 2529c3b1795351ad3dbced8bd88e566a
BLAKE2b-256 84bf1e9d62f2244421c3b5f75717ff9b41792fd7dee5b6305ea0c5ced9e08556

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page