Skip to main content

API Client

Project description

REST-CLIENT

Tests

Setup a client in minutes


Installation

pip install rapid-rest-client

Usage

There are multiple options to auto-create a client. Here's using a swagger definition:

from rest_client.base import BaseUrlConfig, SwaggerApiConfiguration, Client

petstore_endpoint_config: BaseUrlConfig = BaseUrlConfig('https://petstore.swagger.io/v2/')

@SwaggerApiConfiguration(url='https://petstore.swagger.io/v2/swagger.json', base_url_config=petstore_endpoint_config)
class SwaggerUrlConfiguredClient(Client):
    pass

swagger_url_configured_client = SwaggerUrlConfiguredClient()

# Client now has all paths available, as defined in the json file at url

r = swagger_url_configured_client.find_pets_by_status(status='pending')
print(r)

You can also pass the swagger definition as a dict:

@SwaggerApiConfiguration(definition={<swagger dict>}, base_url_config=petstore_endpoint_config)

Manually creating a client:

from rest_client.base import BaseUrlConfig, ApiConfiguration, RequestConfig, Client

endpoint_config: BaseUrlConfig = BaseUrlConfig('https://reqres.in/api/', 'https://sandbox.reqres.in/api/')

@ApiConfiguration(endpoints=[
    RequestConfig('users', 'list_users'),
    RequestConfig('users/{}', 'get_user'),
    RequestConfig('register', 'register_user', 'POST')
], base_url_config=endpoint_config)
class ExampleClient(Client):
    pass

This makes these methods available on ExampleClient

  • list_users
  • get_user
  • register_user
client = ExampleClient()
list_users_response = client.list_users()
get_user_response = client.get_user(3)
register_user_response = client.register_user(email="eve.holt@reqres.in", password="pistol")

If you want to customize a method, you can simply add methods to a client. Use the endpoint decorator to do that:

from rest_client.base import endpoint, Client

class ExampleClient(Client):
    @endpoint('users', 'list_users')
    def list_users(self, **kwargs):
        return self._request(kwargs)

You can also create the client from json, or a dict:

from rest_client.base import DictApiConfiguration, BaseUrlConfig, Client

endpoint_config: BaseUrlConfig = BaseUrlConfig('https://reqres.in/api/')

@DictApiConfiguration(endpoints=[{
    'path': 'users',
    'name': 'list_users',
}], base_url_config=endpoint_config)
class MyClient(Client):
    pass

Authentication

You can pass custom authentication through the authentication_handler property. Authentication handlers should extend AuthBase from requests.auth

For example:

from rest_client.base import BaseUrlConfig, ApiConfiguration, RequestConfig, Client, BearerTokenAuth

auth_endpoint_config: BaseUrlConfig = BaseUrlConfig('https://gorest.co.in/public/v1/')

@ApiConfiguration(
    endpoints=[
        RequestConfig('users', 'register_user', 'POST'),
    ],
    base_url_config=auth_endpoint_config)
class AuthClient(Client):
    pass

auth_client = AuthClient(
    authentication_handler=BearerTokenAuth('token')
)

Please note:

This is a work in progress and completely new. Contributions are very welcome.


Quality gate

Maintainability Rating Reliability Rating Coverage

Bugs Code Smells Technical Debt

Security Rating Lines of Code Duplicated Lines (%)

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

rapid_rest_client-0.0.5.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rapid_rest_client-0.0.5-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file rapid_rest_client-0.0.5.tar.gz.

File metadata

  • Download URL: rapid_rest_client-0.0.5.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for rapid_rest_client-0.0.5.tar.gz
Algorithm Hash digest
SHA256 99354a634c032d3d98c3b15bde74ba6e92b0c36043b8a8ef2dee7f3033bc0520
MD5 37f012e660cab6c113698b8edf9869bd
BLAKE2b-256 cf3c20e13d358a410d0c7a2c8cfc4418b5429394f22ffc2cd571264768c77b4e

See more details on using hashes here.

File details

Details for the file rapid_rest_client-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for rapid_rest_client-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 293313535c20d06b4e2f313b4b5df902aaa2441acef5d93ee8d2f0a72b19d021
MD5 3c645d662d7b16e24061201b83851ffe
BLAKE2b-256 97fd3a67eb151c5a0ff951f6fcf7008ed382eaed21b973fbb77691415bb7c839

See more details on using hashes here.

Supported by

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