1- Base HTTP Client
2- ================
1+ Asyncly
2+ =======
33
4- .. image :: https://img.shields.io/pypi/v/base-http-client .svg
5- :target: https://pypi.python.org/pypi/base-http-client /
4+ .. image :: https://img.shields.io/pypi/v/asyncly .svg
5+ :target: https://pypi.python.org/pypi/asyncly /
66 :alt: Latest Version
77
8- .. image :: https://img.shields.io/pypi/wheel/base-http-client .svg
9- :target: https://pypi.python.org/pypi/base-http-client /
8+ .. image :: https://img.shields.io/pypi/wheel/asyncly .svg
9+ :target: https://pypi.python.org/pypi/asyncly /
1010
11- .. image :: https://img.shields.io/pypi/pyversions/base-http-client .svg
12- :target: https://pypi.python.org/pypi/base-http-client /
11+ .. image :: https://img.shields.io/pypi/pyversions/asyncly .svg
12+ :target: https://pypi.python.org/pypi/asyncly /
1313
14- .. image :: https://img.shields.io/pypi/l/base-http-client .svg
15- :target: https://pypi.python.org/pypi/base-http-client /
14+ .. image :: https://img.shields.io/pypi/l/asyncly .svg
15+ :target: https://pypi.python.org/pypi/asyncly /
1616
17- Base HTTP client for your integrations based on aiohttp _.
17+ Simple HTTP client and server for your integrations based on aiohttp _.
1818
1919Installation
2020------------
@@ -26,13 +26,13 @@ Installing from PyPI_:
2626
2727.. code-block :: bash
2828
29- pip3 install base-http-client
29+ pip install asyncly
3030
3131 Installing from github.com:
3232
3333.. code-block :: bash
3434
35- pip3 install git+https://github.com/andy-takker/base_http_client
35+ pip install git+https://github.com/andy-takker/asyncly
3636
3737 The package contains several extras and you can install additional dependencies
3838if you specify them in this way.
@@ -41,28 +41,123 @@ For example, with msgspec_:
4141
4242.. code-block :: bash
4343
44- pip3 install " base-http-client [msgspec]"
44+ pip install " asyncly [msgspec]"
4545
4646 Complete table of extras below:
4747
48- +-----------------------------------------------+----------------------------------+
49- | example | description |
50- +===============================================+==================================+
51- | ``pip3 install "base-http-client[msgspec]" `` | For using msgspec _ structs |
52- +-----------------------------------------------+----------------------------------+
53- | ``pip3 install "base-http-client[orjson]" `` | For fast parsing json by orjson _ |
54- +-----------------------------------------------+----------------------------------+
55- | ``pip3 install "base-http-client[pydantic]" `` | For using pydantic _ models |
56- +-----------------------------------------------+----------------------------------+
57-
58- Using
59- ~~~~~
60-
61-
48+ +-------------------------------------+----------------------------------+
49+ | example | description |
50+ +========================================================================+
51+ | ``pip install "asyncly[msgspec]" `` | For using msgspec _ structs |
52+ +-------------------------------------+----------------------------------+
53+ | ``pip install "asyncly[orjson]" `` | For fast parsing json by orjson _ |
54+ +-------------------------------------+----------------------------------+
55+ | ``pip install "asyncly[pydantic]" `` | For using pydantic _ models |
56+ +-------------------------------------+----------------------------------+
57+
58+ Quick start guide
59+ -----------------
60+
61+ HttpClient
62+ ~~~~~~~~~~~~~~
63+
64+ Simple HTTP Client for `https://catfact.ninja `. See full example in `examples/catfact_client.py `_
65+
66+ .. code-block :: python
67+
68+ from asyncly import DEFAULT_TIMEOUT , BaseHttpClient, ResponseHandlersType
69+ from asyncly.client.handlers.pydantic import parse_model
70+ from asyncly.client.timeout import TimeoutType
71+
72+
73+ class CatfactClient (BaseHttpClient ):
74+ RANDOM_CATFACT_HANDLERS : ResponseHandlersType = MappingProxyType(
75+ {
76+ HTTPStatus.OK : parse_model(CatfactSchema),
77+ }
78+ )
79+
80+ async def fetch_random_cat_fact (
81+ self ,
82+ timeout : TimeoutType = DEFAULT_TIMEOUT ,
83+ ) -> CatfactSchema:
84+ return await self ._make_req(
85+ method = hdrs.METH_GET ,
86+ url = self ._url / " fact" ,
87+ handlers = self .RANDOM_CATFACT_HANDLERS ,
88+ timeout = timeout,
89+ )
90+
91+ Test Async Server for client
92+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93+
94+ For the HTTP client, we create a server to which he will go and simulate real
95+ responses. You can dynamically change the responses from the server in
96+ a specific test.
97+
98+ Let's prepare the fixtures:
99+
100+ .. code-block :: python
101+
102+ @pytest.fixture
103+ async def catafact_service () -> AsyncIterator[MockService]:
104+ routes = [
105+ MockRoute(" GET" , " /fact" , " random_catfact" ),
106+ ]
107+ async with start_service(routes) as service:
108+ service.register(
109+ " random_catfact" ,
110+ JsonResponse({" fact" : " test" , " length" : 4 }),
111+ )
112+ yield service
113+
114+
115+ @pytest.fixture
116+ def catfact_url (catafact_service : MockService) -> URL :
117+ return catafact_service.url
118+
119+
120+ @pytest.fixture
121+ async def catfact_client (catfact_url : URL ) -> AsyncIterator[CatfactClient]:
122+ async with ClientSession() as session:
123+ client = CatfactClient(
124+ client_name = " catfact" ,
125+ session = session,
126+ url = catfact_url,
127+ )
128+ yield client
129+
130+ Now we can use them in tests. See full example in `examples/test_catfact_client.py `_
131+
132+ .. code-block :: python
133+
134+ async def test_fetch_random_catfact (catfact_client : CatfactClient) -> None :
135+ # use default registered handler
136+ fact = await catfact_client.fetch_random_cat_fact()
137+ assert fact == CatfactSchema(fact = " test" , length = 4 )
138+
139+
140+ async def test_fetch_random_catfact_timeout (
141+ catfact_client : CatfactClient,
142+ catafact_service : MockService,
143+ ) -> None :
144+ # change default registered handler to time error handler
145+ catafact_service.register(
146+ " random_catfact" ,
147+ LatencyResponse(
148+ wrapped = JsonResponse({" fact" : " test" , " length" : 4 }),
149+ latency = 1.5 ,
150+ ),
151+ )
152+ with pytest.raises(asyncio.TimeoutError):
153+ await catfact_client.fetch_random_cat_fact(timeout = 1 )
62154
63155
64156 .. _PyPI : https://pypi.org/
65157.. _aiohttp : https://pypi.org/project/aiohttp/
66158.. _msgspec : https://github.com/jcrist/msgspec
67159.. _orjson : https://github.com/ijl/orjson
68- .. _pydantic : https://github.com/pydantic/pydantic
160+ .. _pydantic : https://github.com/pydantic/pydantic
161+
162+ .. _examples/catfact_client.py : https://github.com/andy-takker/asyncly/blob/master/examples/catfact_client.py
163+ .. _examples/test_catfact_client.py : https://github.com/andy-takker/asyncly/blob/master/examples/test_catfact_client.py
0 commit comments