Skip to main content

Brand new way to test your API

Project description

Build Status PyPI version Downloads

python-apitist

Brand new way to test your API

Installation

Actually project is not published on PyPi, so the only way:

pip install python-apitist

Default hooks

  • RequestDebugLoggingHook
  • RequestInfoLoggingHook
  • PrepRequestDebugLoggingHook
  • PrepRequestInfoLoggingHook
  • ResponseDebugLoggingHook
  • ResponseInfoLoggingHook

Example usage

from apitist.hooks import PrepRequestInfoLoggingHook, ResponseInfoLoggingHook
from apitist.requests import session


s = session()
PrepRequestInfoLoggingHook.formatter = "Best formatter {req.method} {req.url}"

s.add_prep_request_hook(PrepRequestInfoLoggingHook)
s.add_response_hook(ResponseInfoLoggingHook)

s.post("https://httpbin.org/post", params={"q": "test"})

Custom Hooks

from requests import Request, PreparedRequest, Response

from apitist.requests import session, RequestHook, PreparedRequestHook, ResponseHook

s = session()

class ReqHook(RequestHook):

    def run(self, request: Request) -> Request:
        print(request.url)
        return request

class PrepReqHook(PreparedRequestHook):

    def run(self, request: PreparedRequest) -> PreparedRequest:
        print(request.url)
        return request


class RespHook(ResponseHook):

    def run(self, response: Response) -> Response:
        print(response.url)
        return response

s.add_request_hook(ReqHook)
s.add_prep_request_hook(PrepReqHook)
s.add_response_hook(RespHook)

s.get("https://ya.ru", params={"q": "test"})

Working with constructor

import attr
import typing

from apitist.constructor import converter
from apitist.hooks import RequestConverterHook, ResponseConverterHook
from apitist.requests import session


class ExampleType:
    test = None

@attr.s
class ExampleStructure:
    test: ExampleType = attr.ib()

@attr.s
class TestResponse:
    args: typing.Dict = attr.ib()
    data: str = attr.ib()
    files: typing.Dict = attr.ib()
    form: typing.Dict = attr.ib()
    headers: typing.Dict = attr.ib()
    json: ExampleStructure = attr.ib()
    origin: str = attr.ib()
    url: str = attr.ib()

s = session()
s.add_hook(RequestConverterHook)
s.add_hook(ResponseConverterHook)

def structure_example_type(data, type_):
    example = ExampleType()
    example.test = data
    return example

def unstructure_example_type(data):
    return data.test

converter.register_hooks(
    ExampleType, structure_example_type, unstructure_example_type
)

t = ExampleType()
t.test = "test"

struc = ExampleStructure(t)

res = s.post("https://httpbin.org/post", data=struc).structure(TestResponse)
print(res.structured.json.test.test) # test

Using random data generator

First of all create an instance of random class:

from apitist.random import Randomer
rand = Randomer()

Now, you can add custom hooks for different types:

rand.add_type(str, lambda: str(random.random()))
rand.add_type(float, lambda: random.random())

Or using add_types:

types = {
    str: lambda: str(random.random()),
    float: lambda: random.random()
}
rand.add_types(types)

Now you can create random object for given type or any attrs class with defined types:

import attr
import typing

rand.object(str) # '0.6147789314561384'
rand.object(float) # 0.4664297665239271

@attr.s
class Data:
    value1: str = attr.ib()
    value2: typing.List[str] = attr.ib()
    value3: typing.Tuple[float] = attr.ib()

print(rand.object(Data))
# Data(
#   value1='0.491058956716827',
#   value2=['0.6568036485871975'],
#   value3=(0.8603579349502298,)
# )

It is better to use it with Faker. Just define different subclasses for str and add different hooks for them. By this you could create different data for different str fields.

Also, using with RequestConverterHook and ResponseConverterHook you could easily create random json objects which would be send to server.

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

python-apitist-0.4.0.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

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

python_apitist-0.4.0-py2.py3-none-any.whl (21.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file python-apitist-0.4.0.tar.gz.

File metadata

  • Download URL: python-apitist-0.4.0.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for python-apitist-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7faff6c89e50a52857039bd750aef4ad1e20dd6a7695dbc07ca673b4bf27ae10
MD5 873f9e1429b8d9de17b890618ada2b86
BLAKE2b-256 69133dd261a4f7b19f072503b1c766ef91726e72d1a67749add36db3077bf92f

See more details on using hashes here.

File details

Details for the file python_apitist-0.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: python_apitist-0.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for python_apitist-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 24d7e03b6f63e9399d61110dc0537e6938f8ab7443251417168935e59d94e9e7
MD5 5bda100d883b28e06114d9728ea85e82
BLAKE2b-256 a1177588019281143a4e07e8fc37f04e8ebb11f5b6bac20d6fc5da1df1c85109

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