Skip to main content

API-Common package for betagr-project.

Project description

BetAgr-common - base async api-clients for betagr-project wrote with aiohttp

A set of tools, utilities and base async api-classes for maintaining the infrastructure of betagr-project services.

Requirements

  • python>=3.6
  • aiohttp>=3.6

Usage

pip install betagr-common

# or only for user
python -m pip install --user betagr-common

# or for pipenv
pipenv install betagr-common

BetAgr-common ships with two base api-classes: BaseClient and BaseClientSSO.

BaseClient

It is a base class provides a basic REST API methods which are simple wrapper over a aiohttp.request. Сlass also implements basic logging of client requests and handling cookies within one client, since the connection session exists only during one request.

Api-clients of each services should be inherited from the BaseClient.

from common.rest_client.base_client import BaseClient

class BakeryClient(BaseClient):

    _host = 'www.some-bakery.com'
    _port = 8000

    def __init__(self, headers):
        super().__init__(headers=headers)

    async def bake_bread(self, bread_flour='first-grade'):
        body = {'flour': bread_flour}
        api_uri = 'api/bake-bread'

        return await self.post(api_uri, body=body)
import asyncio
import BakeryClient

db_orders = {'John Doe': {'flour': 'first-grade'},
             'Anderson': {'flour': 'second-grade'}}

async def main():
    async for client in db_orders:
        custom_headers = {'Content-Type': 'application/json'}

        bakery_client = BakeryClient(custom_headers)

        bread_flour_type = db_orders[client].get('flour')            

        response = await bakery_client.bake_bread(bread_flour_type)
        assert response.status == 200    # bread was bake!


loop = asyncio.get_event_loop()
loop.run_until_complete(main())  
BaseClientSSO

Api abstraction for sso-client with the release of basic methods:

  • sign_up
  • sign_in
  • sign_out
  • reset_password
import os
import asyncio
from common.rest_client.base_client_sso import BaseClientSSO


async def main():
    # env should contains 'SSO_API_HOST' and 'SSO_API_PORT' variables
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}

    sso_client = BaseClientSSO(headers)

    body = {'username': 'john doe',
            'password': 'qwerty'}

    response = await sso_client.sign_up(body)
    print(response)    # ClientResponse -> defaults
                       # {'json': {},
                       #  'status': 200,
                       #  'headers': {'Content-Type: "application/json"},
                       #  'raw_content': b''}

    body = {'username': 'john doe',
            'password': 'qwerty'}

    response = await sso_client.sign_in(body)
    print(response)

    body = {'username': 'john doe',
            'old_password': 'qwerty',
            'new_password': 'qwerty'}

    response = await sso_client.reset_password(body)
    print(response)

    response = await sso_client.sign_out()
    print(response)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Environment variables are used to determine url/connection parameters in base classes:

env variable value
SSO_API_HOST localhost, www.example.com
SSO_API_PORT 8080
PARSER_API_HOST localhost, www.example.com
PARSER_API_PORT 8080
AGGREGATOR_API_HOST localhost, www.example.com
AGGREGATOR_API_PORT 8080
COMMON_API_CLIENT_LOGGING_LEVEL 40

see numeric represents logging level here: https://docs.python.org/3/library/logging.html#logging-levels

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

betagr-common-0.1.8.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

betagr_common-0.1.8-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file betagr-common-0.1.8.tar.gz.

File metadata

  • Download URL: betagr-common-0.1.8.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

File hashes

Hashes for betagr-common-0.1.8.tar.gz
Algorithm Hash digest
SHA256 a293c0c2b86fcc5cd14fceb69886dd5b0824cb62f5cf28a41db609743c5291dd
MD5 dd678a90fed57a34e470247d07cb3028
BLAKE2b-256 449569b769cca00db885363a990df4e5320422c1de5d311835eaacea29043b63

See more details on using hashes here.

File details

Details for the file betagr_common-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: betagr_common-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

File hashes

Hashes for betagr_common-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 7a41b969f6c352e91b57da8855ebba62ecb7a7a449a6a9c8d20feb1d9607466e
MD5 c855f7603648ab18171306953bb07f11
BLAKE2b-256 32d798b825f6071f2579c25e54555e7b934ee4baa3fcade808cd5727f1e1387f

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