Giter Site home page Giter Site logo

baseapi's Introduction

BaseAPI

Easily create maintainable API clients.

Rationale

Building other Python based API clients I found that there was a tendency to end up with a "mega-class", containing most of the definitions of my API. BaseAPI tries to keep unrelated API concepts separate, hopefully making for an easier maintenance experience.

Installation

PyPi is the easiest way to install:

pip install baseapi

Usage

Creating a client

Normally the Client class is inherited to create your own client class:

from baseapi import Client


class MyClient(Client):
    DEFAULT_URL = 'https://my-api.com'

Here we've set our default API URL. This can also be set during the creation of the client:

client = MyClient(url='https://localhost')

Creating APIs

To populate your client with functions to access your API use individual API classes. These reflect an isolated part of your overall API.

As an example, you may have an authorization component to your API. To add authorization to your client library, you may create a file called auth.py:

from baseapi.apis import GraphqlApi


class AuthApi(GraphqlApi):
    def login(self, username, password):
        login_query = '...'
        data = {
            'username': username,
            'password': password
        }
        return self.perform_query(login_query, data)

    def logout(self):
        logout_query = '...'
        return self.perform_query(logout_query)

Once you have this slice of your API ready, you can add it to your client by specifying it during the client class definition:

from baseapi import Client


class MyClient(Client):
    DEFAULT_URL = 'https://my-api.com'
    DEFAULT_APIS = (
        'auth',
    )

In this case, auth.py must be placed in your PYTHONPATH, most likely alongside your client class file. Now, you may access the APIs methods on your client as such:

client = MyClient()
client.login('username', 'password')

There are currently two API types supported, GraphQL and Rest. The same auth API as above, but using Rest instead:

from baseapi.apis import RestApi


class AuthApi(RestApi):
    def login(self, username, password):
        data = {
            'username': username,
            'password': password
        }
        return self.post('/login', data)

    def logout(self):
        return self.post('/logout')

Exposing methods to the client

The methods in an API that get exposed to a client are chosen based on a leading underscore. Those without an underscore are automatically added to the client class, while those with an underscore are treated as private.

So, as an example, a local validation method could be added to an API as such:

from baseapi.apis import RestApi


class MyApi(RestApi):
    def get_something(self, type):
        self._validate_type(type)
        return self.get('/api/something, data={'type': type})

    def _validate_type(self, type):
        # Do validation.
        pass

baseapi's People

Contributors

slurms avatar furious-luke avatar tabby-or-not avatar martinn avatar matmunn avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.