1
- Base HTTP Client
2
- ================
1
+ Asyncly
2
+ =======
3
3
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 /
6
6
:alt: Latest Version
7
7
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 /
10
10
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 /
13
13
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 /
16
16
17
- Base HTTP client for your integrations based on aiohttp _.
17
+ Simple HTTP client and server for your integrations based on aiohttp _.
18
18
19
19
Installation
20
20
------------
@@ -26,13 +26,13 @@ Installing from PyPI_:
26
26
27
27
.. code-block :: bash
28
28
29
- pip install base-http-client
29
+ pip install asyncly
30
30
31
31
Installing from github.com:
32
32
33
33
.. code-block :: bash
34
34
35
- pip install git+https://github.com/andy-takker/base_http_client
35
+ pip install git+https://github.com/andy-takker/asyncly
36
36
37
37
The package contains several extras and you can install additional dependencies
38
38
if you specify them in this way.
@@ -41,37 +41,33 @@ For example, with msgspec_:
41
41
42
42
.. code-block :: bash
43
43
44
- pip3 install " base-http-client [msgspec]"
44
+ pip install " asyncly [msgspec]"
45
45
46
46
Complete table of extras below:
47
47
48
- +---------------------------------------------- +----------------------------------+
49
- | example | description |
50
- +==============================================+======== ==========================+
51
- | ``pip install "base-http-client [msgspec]" `` | For using msgspec _ structs |
52
- +---------------------------------------------- +----------------------------------+
53
- | ``pip install "base-http-client [orjson]" `` | For fast parsing json by orjson _ |
54
- +---------------------------------------------- +----------------------------------+
55
- | ``pip install "base-http-client [pydantic]" `` | For using pydantic _ models |
56
- +---------------------------------------------- +----------------------------------+
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
57
58
58
Quick start guide
59
59
-----------------
60
60
61
- BaseHttpClient
61
+ HttpClient
62
62
~~~~~~~~~~~~~~
63
63
64
- Simple HTTP Client for `https://catfact.ninja `. See full example in `examples/base_http_client .py `_
64
+ Simple HTTP Client for `https://catfact.ninja `. See full example in `examples/catfact_client .py `_
65
65
66
66
.. code-block :: python
67
67
68
- from base_http_client.client import (
69
- DEFAULT_TIMEOUT ,
70
- BaseHttpClient,
71
- ResponseHandlersType,
72
- )
73
- from base_http_client.handlers.pydantic import parse_model
74
- from base_http_client.timeout import TimeoutType
68
+ from asyncly import DEFAULT_TIMEOUT , BaseHttpClient, ResponseHandlersType
69
+ from asyncly.client.handlers.pydantic import parse_model
70
+ from asyncly.client.timeout import TimeoutType
75
71
76
72
77
73
class CatfactClient (BaseHttpClient ):
@@ -92,7 +88,69 @@ Simple HTTP Client for `https://catfact.ninja`. See full example in `examples/ba
92
88
timeout = timeout,
93
89
)
94
90
91
+ Test Async Server for client
92
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95
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 )
96
154
97
155
98
156
.. _PyPI : https://pypi.org/
@@ -101,4 +159,5 @@ Simple HTTP Client for `https://catfact.ninja`. See full example in `examples/ba
101
159
.. _orjson : https://github.com/ijl/orjson
102
160
.. _pydantic : https://github.com/pydantic/pydantic
103
161
104
- .. _examples/base_http_client.py : https://github.com/andy-takker/base_http_client/blob/master/examples/base_http_client.py
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