Git Product home page Git Product logo

api-client-template's Introduction

api-client-template

A template to consume rest-api in python code

Package structure

.
├── client.py       contains a client that exposes a generic request method and accepts `session` as instantiation parameter and api base-url and so on.
├── .env.example    contains envrionment variable declarations.
├── environment.py  contains environment variables load as language (Python|JS) objects
├── exceptions.py   contains custom exceptions related to the target api.
├── __init__.py     exports the client, the services and the exceptions.
├── README.md
├── services.py     contains queries and commands related to the target api.
└── session.py      contains transport layer configuration struff (max-retries, rate-limits, exponential-backoff, ..)
└── types.py        contains request and response declarations.

Depencencies graph

- session --use--> environment
- client --use--> session
- client --[use]--> exceptions
- services --use--> client, exceptions, types, environment

Guidelines

  • Always define custom exceptions for the target Api.

  • Always define a base exception for the target Api.

  • Always seperate exceptions into two groups techinal and business that will inherit from the base exception.

  • Always translate technical exceptions into business exceptions (when the client is business oriented, in this case use are less technical person, so they don't need to know about technical information)

  • Always translate transport layer exceptions into technical, but when doing so make sure not to loose the exception context, you have to chain the translated exception and it's cause.

  • Always throw an exception when the query or the command cannot be fullill, avoid returning error codes or boolean, you can't the task in your hand you just fail and fail fast.

  • Never handle an exception (you can translate but don't catch and log, cause you don't know how the user of library will want to handle exception).

  • Always write the happy part of the code, if you cannot do something, just throw an exception, avoid guessing.

  • Try not do validate input parameters inside the query or a command itself, define a wrapper that will do it before passing parameters to the wrappee.

  • Always translate(map) Http error-code to exception.

  • All Custom exception must have the shape below:

from __future__ import annotations
# definition

class APIException(Exception):
    def __init__(
        self,
        message: str = None,
        code: str = None,
        errors: dict = None,
        response: Response = None
    ):
        self.message = message
        self.code = code
        self.errors = errors or {}
        self.response = response
    
    @property
    def cause(self) -> Exception | None:
        return self.__cause__

# Usage
raise APIException(
    message="message",
    code="validation_error",
    errors={},
    response=response,
) from causeExceptionInstance # Chain the cause

Case studies

api-client-template's People

Contributors

aristidebm 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.