Skip to main content

Python class that allow to create client for REST API with dynamic code generation

Project description

FitRequest

This module allows you to write REST api client with automatic code generation.

Skillcorner Coverage Build PyPI - Version Versions License Twitter Follow

Example of implementation: SkillCorner Python client for SkillCorner API.

Help

See documentation for more details.

Installation

pip install fitrequest

A simple example

from fitrequest.client import FitRequest


class RestApiClient(FitRequest):
    base_url = "http://base_api_url"
    base_client_name="rest_api"
    _docstring_template = 'GET request on endpoint: {endpoint}\nDocs URL anchor: http://base_api_url/docs/{docs_url_anchor}'
    _methods_binding = [
        {
            'name': 'get_item',
            'endpoint': '/items/{}',
            'docs_url_anchor': 'items/items_list',
            'resource_name': 'item_id',
        }
    ]

This is a simple definition of a subclass of FitRequest object. By instanciating this class, we can do the following:

my_client = RestApiClient()
response = my_client.get_item(item_id=3)

response variable will hold the value of the JSON response of the following HTTP request:

GET http://base_api_url/items/3

Subclass definition

MethodDetails and methods_binding

MethodDetails is a dataclass that holds the following attributes:

  • name: the name of the method that will be created in the subclass of FitRequest
  • endpoint: the endpoint of the request
  • docstring: the docstring of the method that will be created in the subclass of FitRequest
  • exec_method: the name of the method that will be executed to retrieve the response
  • extra_params: the list of named arguments to add to the method signature
  • raise_for_status: wether to raise exception for response status code >= 400 and < 600
  • response_key: the key of the JSON response that will be returned by the method
  • resource_name: the name of the arg that will be used in the definition of the python method and sent as a path or query parameter in the request

_methods_binding is almost a list of MethodDetails (as dict) that will be used to create the methods in the subclass of FitRequest.

The methods can be extended with a docstring template in which you can use any other method attribute. You can also have all the methods starting with 'get' duplicated as a save method starting with 'get_and_save' taking a filepath argument and dumping the data into a file.

For the moment, the generation of methods supports only one query or path parameters. If an endpoint requires more variables in its definition, you will need to actually write the method. For example, it is not possible to send a request to the following endpoint with the current version of FitRequest:

GET http://base_api_url/path/to/resource/{resource_id}/subresource/{subresource_id}

It will be a topic for a future release.

_build_final_url method

This method is used to build the final url of the request. Sometimes, you may need to add some extra information in the url before making the query. For example, you may need to add a token in the url. Here is a simple example:

    def _build_final_url(self, endpoint: str, **kwargs) -> str:
            return f'{self.base_url}{endpoint}?token={self.token}'

Authentication

Basic authentication

Basic authentication is supported by FitRequest. You just need to override the following method to your subclass of FitRequest:

from requests.auth import HTTPBasicAuth

    def _authenticate(self) -> HTTPBasicAuth:
        return HTTPBasicAuth(self.username, self.password)

This will set the auth attribute of the request's session.

Header based authentication

Some API requires extra headers to be set in the request. If you need to do so, you will have to override the session property in your subclass of FitRequest:

from requests import Session

    @property
    def session(self) -> Session:
        session = super().session
        session.headers['APIKey'] = self.api_key
        return session

Contact

You can contact Skillcorner Team at: support@skillcorner.com.

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

fitrequest-0.7.4.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

fitrequest-0.7.4-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file fitrequest-0.7.4.tar.gz.

File metadata

  • Download URL: fitrequest-0.7.4.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.0 Linux/5.10.219-208.866.amzn2.x86_64

File hashes

Hashes for fitrequest-0.7.4.tar.gz
Algorithm Hash digest
SHA256 2ccd64ddea7784d71166343e57a30f3932be926cf806ff0c77bdcf23258f7963
MD5 deb67cd94320935695026773e1147da5
BLAKE2b-256 aba42bb4ff3ad633db330fd17f9ba84b1983fe2574ca2d508b5281a9292db513

See more details on using hashes here.

File details

Details for the file fitrequest-0.7.4-py3-none-any.whl.

File metadata

  • Download URL: fitrequest-0.7.4-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.0 Linux/5.10.219-208.866.amzn2.x86_64

File hashes

Hashes for fitrequest-0.7.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5a8b2ec68b1b905fb739519b7c66cd6a81894d2ccb4cd95cefb97772120fba77
MD5 ba00526481142fe2d4bdd78047fa162a
BLAKE2b-256 b100406716eceb5922c5e1b2bdf4581f86d2e67b1ddce18adb758c3c735f6994

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