Skip to content

Commit 88886e2

Browse files
committed
Add doc
1 parent 68b19d7 commit 88886e2

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ We use it at [Bixoto](https://bixoto.com/) as a basis for JSON API clients such
77
It aims at factoring the common parts of these clients while staying very lightweight (<100 SLOC).
88

99
[PyMagento]: https://github.com/Bixoto/PyMagento
10+
1011
[PyBigBuy]: https://github.com/Bixoto/PyBigBuy
1112

1213
## Features
@@ -40,3 +41,35 @@ client = APISession("https://httpbin.org")
4041
client.get_json_api("/get")
4142
# => {...}
4243
```
44+
45+
A typical usage is to inherit from the session to implement an API client class:
46+
47+
```python3
48+
from typing import Any, cast
49+
50+
from api_session import APISession
51+
52+
53+
class FooApiClient(APISession):
54+
def __init__(
55+
self,
56+
token: str,
57+
**kwargs: Any,
58+
):
59+
super().__init__(base_url="https://foo-api.example.com/v1", **kwargs)
60+
self.headers["Authorization"] = f"Bearer {token}"
61+
62+
def get_stuff(self) -> list[dict[str, Any]]:
63+
return cast(list[dict[str, Any]],
64+
self.get_json_api("/get-stuff"))
65+
66+
def create_stuff(self, stuff: dict[str, Any]) -> bool:
67+
return cast(bool, self.post_json_api("/create-stuff", json=stuff))
68+
69+
70+
# Then use it:
71+
my_client = FooApiClient("my-token")
72+
my_client.create_stuff({"foo": "bar"}) # => True
73+
for stuff in my_client.get_stuff():
74+
print(stuff)
75+
```

0 commit comments

Comments
 (0)