Skip to main content

General HTTP requests library for Python applications running in Keboola Connection environment

Project description

Python HTTP Client

Introduction

Build & Test Code Climate PyPI version

This library serves as tool to work effectively when sending requests to external services. The library wraps on top of the requests library and implements a couple useful method, such as in-built retry, exception raising, etc.

It is being developed by the Keboola Data Services team and officially supported by Keboola. It aims to simplify the Keboola Component creation process, by removing the necessity to write complicated code to work with the APIs effectively.

Links

Quick Start

Installation

The package may be installed via PIP:

pip install keboola.http-client

Structure and Functionality

The package contains a single core module:

  • keboola.http_client - Contains the HttpClient class for easy manipulation with APIs and external services

HttpClient

The core class that serves as a tool to communicate with external services. The class is a wrapper around the requests library with implemented retry mechanism, and automatic error handling in case of HTTP error returned.

For each HTTP method, following methods are implemented in the HttpClient:

  • GET - get(), get_raw()
  • POST - post(), post_raw()
  • PATCH - patch(), patch_raw()
  • UPDATE - update(), update_raw()
  • PUT - put(), put_raw()
  • DELETE - delete(), delete_raw()

The difference between _raw() methods and their non-_raw() counterparts is, that raw methods will return requests.Response object, while non-raw methods will return a json body if the request is successful and raise an error if an HTTP error is encountered.

All abovementioned methods support all parameters supported by requests.request() functions - as described in the documentation.

Initialization

The core class is keboola.http_client.HttpClient, which can be initialized by specifying the base_url parameter:

from keboola.http_client import HttpClient

BASE_URL = 'https://connection.keboola.com/v2/storage/'
cl = HttpClient(BASE_URL)

Default arguments

For HttpClient, it is possible to define default arguments, which will be sent with every request. It's possible to define default_http_header, auth_header and default_params - a default header, a default authentication header and default parameters, respectively.

from keboola.http_client import HttpClient

BASE_URL = 'https://connection.keboola.com/v2/storage/'
AUTH_HEADER = {
    'x-storageapi-token': '1234-STORAGETOKENSTRING'
}
DEFAULT_PARAMS = {
    'include': 'columns'
}
DEFAULT_HEADER = {
    'Content-Type': 'application/json'
}

cl = HttpClient(BASE_URL, default_http_header=DEFAULT_HEADER,
                auth_header=AUTH_HEADER, default_params=DEFAULT_PARAMS)

Basic authentication

By specifying the auth argument, the HttpClient will utilize the basic authentication.

from keboola.http_client import HttpClient

BASE_URL = 'https://connection.keboola.com/v2/storage/'
USERNAME = 'TestUser'
PASSWORD = '@bcd1234'

cl = HttpClient(BASE_URL, auth=(USERNAME, PASSWORD))

Simple POST request

Making a simple POST request using post_raw() method.

from keboola.http_client import HttpClient

BASE_URL = 'https://www.example.com/change'
cl = HttpClient(BASE_URL)

data = {'attr_1': 'value_1', 'attr_2': 'value_2'}
header = {'content-type': 'application/json'}
response = cl.post_raw(data=data, headers=header)

if response.ok is not True:
    raise ValueError(response.json())
else:
    print(response.json())

Making a simple POST request using post() method.

from keboola.http_client import HttpClient

BASE_URL = 'https://www.example.com/change'
cl = HttpClient(BASE_URL)

data = {'attr_1': 'value_1', 'attr_2': 'value_2'}
header = {'content-type': 'application/json'}
response = cl.post(data=data, headers=header)

Working with URL paths

Each of the methods takes an optional positional argument endpoint_path. If specified, the value of the endpoint_path will be appended to the URL specified in the base_url parameter, when initializing the class. When appending the endpoint_path, the urllib.parse.urljoin() function is used.

The below code will send a POST request to the URL https://example.com/api/v1/events:

from keboola.http_client import HttpClient

BASE_URL = 'https://example.com/api/v1'
cl = HttpClient(BASE_URL)

header = {'token': 'token_value'}
cl.post_raw('events', headers=header)

It is also possible to override this behavior by using parameter is_absolute_path=True. If specified, the value of endpoint_path will not be appended to the base_url parameter, but will rather be used as an absolute URL to which the HTTP request will be made.

In the below code, the base_url parameter is set to https://example.com/api/v1, but the base URL will be overriden by specifying is_absolute_path=True and the HTTP request will be made to the URL specified in the post() request - https://anothersite.com/v2.

from keboola.http_client import HttpClient

BASE_URL = 'https://example.com/api/v1'
cl = HttpClient(BASE_URL)

header = {'token': 'token_value'}
cl.post_raw('https://anothersite.com/v2', headers=header, is_absolute_path=True)

Raw request Example

A simple request made with default authentication header and parameters.

import os
from keboola.http_client import HttpClient

BASE_URL = 'https://connection.keboola.com/v2/'
TOKEN = os.environ['TOKEN']

cl = HttpClient(BASE_URL, auth_header={'x-storageapi-token': TOKEN})

request_params = {'exclude': 'components'}
response = cl.get_raw('storage', params=request_params)

if response.ok is True:
    print(response.json())

Building HTTP client based on HTTPClient Example

This example demonstrates the default use of the HTTPClient as a base for REST API clients.

from keboola.http_client import HttpClient

BASE_URL = 'https://connection.eu-central-1.keboola.com/v2/storage'
MAX_RETRIES = 10


class KBCStorageClient(HttpClient):

    def __init__(self, storage_token):
        HttpClient.__init__(self, base_url=BASE_URL, max_retries=MAX_RETRIES, backoff_factor=0.3,
                            status_forcelist=(429, 500, 502, 504),
                            auth_header={"X-StorageApi-Token": storage_token})

    def get_files(self, show_expired=False):
        params = {"showExpired": show_expired}
        return self.get('files', params=params)

cl = KBCStorageClient("my_token")

print(cl.get_files())

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

keboola.http_client-1.0.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

keboola.http_client-1.0.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file keboola.http_client-1.0.1.tar.gz.

File metadata

  • Download URL: keboola.http_client-1.0.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for keboola.http_client-1.0.1.tar.gz
Algorithm Hash digest
SHA256 58f828c61a709ac484e85e2acb5e78eae2db402e7846769b739a6c336384fa81
MD5 08d42fa770483e6c08dc96713d071d67
BLAKE2b-256 bd4681805cea9f7eff8af310a43e39b312794c9143b89fdcfdc5f502ed1818c6

See more details on using hashes here.

File details

Details for the file keboola.http_client-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for keboola.http_client-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5d570789433c65325937f6d466cc6b8edc0558f984f03846f0200ae5c5b4b140
MD5 8c78e1530cfda53ff10ab8dff8761ee2
BLAKE2b-256 2718293557d9245a1ec0e0826202ca341bb769033121d1bd1991226db908a5df

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