Giter VIP home page Giter VIP logo

obrequests's Introduction

Object-Based Requests

Object-Based Requests reinvents the wheel around how HTTP requests are interfaced by developers, its built around reusability & static typing.

Index

Install

pip3 install OBRequests>=2.0.0

Docs

obrequests.readthedocs.io

Features

  • Unique route typing
  • Documented
  • Supports sync & async with a flick of a boolean
  • Built on top of HTTPX for stability and security.
    • Supports all the amazing features of HTTPX

Example

from OBRequests import (
    OBRequests, Response, CallBack, Route,
    Get, json, raise_for_status,
    HTTPStatusError, AnyStatus, BasicAuth
)


def custom_response(resp: Response, is_get: bool = False,
                    **kwargs) -> None:
    if is_get:
        print(resp.status_code)
    else:
        raise NotImplementedError()


class Requests(OBRequests):
    posts_route = Route(
        "/posts/{post_id}",
        responses={
            AnyStatus: CallBack(raise_for_status)
        },
        path_params={
            "post_id": "404_error"
        },
        methods=[
            Get(
                responses={
                    200: CallBack(custom_response, is_get=True),
                    201: ConditionalCallBack(
                        awaiting=CallBack(custom_response, is_get=True),
                        blocking=CallBack(custom_response, is_get=False)
                    )
                },
                auth=BasicAuth("different", "password")
            ),
        ],
        auth=BasicAuth("username", "password")
    )


request = Requests(
    responses={
        200: CallBack(json)
    },
    base_url="https://jsonplaceholder.typicode.com",
    awaiting=False,
    globals_={
        "example": True
    }
)

try:
    request.posts_route.get()
    # The same as
    request.base_.get(url="/posts")
except HTTPStatusError as error:
    print(error)

# Prints status code
request.posts_route.get(path_params={
    "post_id": 1
})


# Only needed for async
request.close_()

Thanks to

obrequests's People

Contributors

wardpearce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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