Skip to main content

Helpful WebClient class to interact with APIs on the web

Project description

Install

$ pip3 install webclient-helper

Usage

import webclient_helper as wh

Create an instance of WebClient and use the HTTP methods (OPTIONS, HEAD, GET, POST, PUT, PATCH, DELETE) to interact with an API.

WebClient(username=None, password=None, token=None, token_type=None, base_url='',
          user_agent=None, content_type='application/json', extra_headers={})

    Interact with an API on the web

    If you need to obtain a token from a login endpoint, define a "login"
    method when you subclass WebClient and set self._token and self._token_type

    Example:

        def login(self):
            headers = {'Content-Type': 'application/json'}
            data = {'email': self._username, 'password': self._password}
            response = self.session.post(
                self._base_url + '/api/login',
                headers=headers,
                json=data
            )
            self._token = response.json().get('access_token')
            self._token_type = 'Bearer'


    __init__(self, username=None, password=None, token=None, token_type=None,
             base_url='', user_agent=None, content_type='application/json',
             extra_headers={})
        - username: if specified, set auth on session (requires password)
        - password: if specified, set auth on session (requires username)
        - token: if specified, use this token in the "Authorization" header
          (requires token_type)
        - token_type: if specified, use as part of the value in the
          "Authorization" header
        - base_url: base url for service/API that a subclass would interact with
        - user_agent: if specified, set "User-Agent" header
        - content_type: content type for requests
        - extra_headers: a dict of extra headers to set on the session

        If no login method is defined, any supplied username/password will be
        passed to new_requests_session (for basic auth)

Example (GitHub)

Note: To use the GitHub API, first generate a “personal access token” at https://github.com/settings/tokens/new

from os import getenv

access_token = getenv('GITHUB_ACCESS_TOKEN')
gh_client = wh.WebClient(token=access_token, token_type='token')
data = gh_client.GET('https://api.github.com/user/repos')

Example (subclass with custom login)

class SomeClient(wh.WebClient):
    def login(self):
        headers = {'Content-Type': 'application/json'}
        data = {'email': self._username, 'password': self._password}
        response = self.session.post(
            self._base_url + '/api/login',
            headers=headers,
            json=data
        )
        self._token = response.json().get('access_token')
        self._token_type = 'Bearer'

    def get_something(self, params=None, debug=False):
        return self.GET(
            self._base_url + '/api/something',
            params=params,
            debug=debug
        )


some_client = SomeClient(
    username='myuser',
    password='mypass',
    base_url='https://somewhere.com',
)

something1 = some_client.get_something(params={'x': 1, 'y': 5})
something2 = some_client.get_something(params={'x': 2, 'y': 10})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

webclient_helper-0.0.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file webclient_helper-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: webclient_helper-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for webclient_helper-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 85a4b409cc41bf9e9f4a368ea1ee4b4c01092dc1c8cbd00f6f5a0cd0543cb76f
MD5 b726604f20e90cdf26af77e2e82ba3b9
BLAKE2b-256 55a2862145936b8706586bc1d118587a3e35ec1f851420c60bf6d377902b075c

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