Skip to main content

Joining json API calls together

Project description

APIBridge

Joining json API calls together


Build Status codecov PyPI version MIT License


Installation

pip install api_bridge

Usage

The API-class

API(url: str,
    method: Method = Method.GET,
    result_filter: Optional[Callable[[Dict[str, Any]], Dict[str, Any]]] = None,
    validate: Optional[Callable[[Dict[str, Any]], bool]] = None,
    post_data: Optional[Dict[str, Any]] = None
    )

url The url to run a request with

method The Enum from api_bridge.methods.Method [either Method.GET or .POST; defaults to .GET]

result_filter A filter for the retrieved json data api_bridge.filter.Filter [defaults to None]


Filter(**out: str)

out The items of the filtered data

examples:

Filter(lat='latitude', long='longitude')  # {'lat': data['latitude'], 'long': data['longitude']}

Filter(sunrise='results sunrise')  # spaces represent {'sunrise': data['results]['sunrise']}

validate An additional test based on the received data in addition to the http-status-code [defaults to None]

example:

validate=lambda data: data['status'] == 'successful'

post_data Data if Method.POST was provided

Requests

There are two ways to run a request

from api_bridge.api import API
from api_bridge.filter import Filter
from api_bridge.container import Container

LOCATION_URL = 'https://ipapi.co/json/'
TIME_URL = 'https://api.sunrise-sunset.org/json?lat={lat}&lng={long}&date=today'

The chain-method

result = API.chain(
            API(LOCATION_URL, result_filter=Filter(lat='latitude', long='longitude')),
            API(TIME_URL, validate=lambda response: response['status'] == 'OK', result_filter=Filter(sunrise='results sunrise'))
         )

result == {'sunrise': '4:08:10 AM'}

The (esoteric) operator-method > or >>

result = API('https://ipapi.co/json/', result_filter=Filter(lat='latitude', long='longitude')) > \
                API('https://api.sunrise-sunset.org/json?lat={lat}&lng={long}&date=today', validate=lambda response:
                    response['status'] == 'OK', result_filter=Filter(sunrise='results sunrise')) > \
                Container()

result == {'sunrise': '4:08:10 AM'}

Important the gt and rshift operator mehtod must terminate with an Operator(), which can be treated a dict afterwards

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

api_bridge-0.0.6.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

api_bridge-0.0.6-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file api_bridge-0.0.6.tar.gz.

File metadata

  • Download URL: api_bridge-0.0.6.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for api_bridge-0.0.6.tar.gz
Algorithm Hash digest
SHA256 a02f6ffc954da3418222ec7c4c538c4453e46af7a8dc43172591f3af8dcb38cb
MD5 130f8da083c47a6a1dfe0a901d217038
BLAKE2b-256 87d9d3f61ba719cef26971bff4ecb6db9ca5b6ae9163cab991262c3e78d92022

See more details on using hashes here.

File details

Details for the file api_bridge-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: api_bridge-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for api_bridge-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c5b5f9f43e6f02d77247c07ee9f7ab65203abaace99decfb02a793be70339316
MD5 54a50505809234b8ccb848f61d9d50b9
BLAKE2b-256 86a68a78819a561dddfa6166fa6d8598dc3c2a3391f2ff57bb21516f8d95690b

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