Skip to content

Commit 121fbd0

Browse files
committed
📝 update readme
1 parent 18e40e9 commit 121fbd0

File tree

1 file changed

+107
-15
lines changed

1 file changed

+107
-15
lines changed

README.md

Lines changed: 107 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,28 @@ from githubkit import GitHub, ActionAuthStrategy
148148
github = GitHub(ActionAuthStrategy())
149149
```
150150

151+
### Config
152+
153+
githubkit is highly configurable, you can change the default config by passing config options to `GitHub`:
154+
155+
```python
156+
from githubkit import GitHub
157+
158+
github = GitHub(
159+
base_url="https://api.github.com/",
160+
accept_format="full+json",
161+
previews=["starfox"],
162+
user_agent="GitHubKit/Python",
163+
follow_redirects=True,
164+
timeout=None,
165+
http_cache=True
166+
)
167+
```
168+
169+
The `accept_format` and `previews` are used to set the default `Accept` header, you can find more details in [GitHub API docs](https://docs.github.com/en/rest/overview/media-types).
170+
171+
The `http_cache` option enables the http caching feature powered by [Hishel](https://hishel.com/) for HTTPX. GitHub API limits the number of requests that you can make within a specific amount of time. This feature is useful to reduce the number of requests to GitHub API and avoid hitting the rate limit.
172+
151173
### Calling Rest API
152174

153175
> APIs are fully typed. Typing in the following examples is just for reference only.
@@ -156,7 +178,7 @@ Simple sync call:
156178

157179
```python
158180
from githubkit import Response
159-
from githubkit.rest import FullRepository
181+
from githubkit.versions.latest.models import FullRepository
160182

161183
resp: Response[FullRepository] = github.rest.repos.get(owner="owner", repo="repo")
162184
repo: FullRepository = resp.parsed_data
@@ -166,7 +188,7 @@ Simple async call:
166188

167189
```python
168190
from githubkit import Response
169-
from githubkit.rest import FullRepository
191+
from githubkit.versions.latest.models import FullRepository
170192

171193
resp: Response[FullRepository] = await github.rest.repos.async_get(owner="owner", repo="repo")
172194
repo: FullRepository = resp.parsed_data
@@ -176,7 +198,7 @@ Call API with context (reusing client):
176198

177199
```python
178200
from githubkit import Response
179-
from githubkit.rest import FullRepository
201+
from githubkit.versions.latest.models import FullRepository
180202

181203
with GitHub("<your_token_here>") as github:
182204
resp: Response[FullRepository] = github.rest.repos.get(owner="owner", repo="repo")
@@ -185,21 +207,68 @@ with GitHub("<your_token_here>") as github:
185207

186208
```python
187209
from githubkit import Response
188-
from githubkit.rest import FullRepository
210+
from githubkit.versions.latest.models import FullRepository
189211

190212
async with GitHub("<your_token_here>") as github:
191213
resp: Response[FullRepository] = await github.rest.repos.async_get(owner="owner", repo="repo")
192214
repo: FullRepository = resp.parsed_data
193215
```
194216

217+
### Data Validation
218+
219+
As shown above, the response data is parsed and validated by accessing the `response.parsed_data` property. This ensures that the data type returned by the API is as expected and your code is safe to use it (with static type checking). But sometimes you may want to get the raw data returned by the API, such as when the schema is not correct. You can use the `response.text` property or `response.json()` method to get the raw data:
220+
221+
```python
222+
from typing import Any, Dict
223+
from githubkit import Response
224+
225+
resp: Response[FullRepository] = github.rest.repos.get(owner="owner", repo="repo")
226+
repo: Dict[str, Any] = resp.json()
227+
```
228+
229+
### Rest API Versioning
230+
231+
> APIs are fully typed. Different versions of APIs are typed separately.
232+
233+
githubkit supports all versions of GitHub API, you can switch between versions as follows:
234+
235+
```python
236+
github.rest("2022-11-28").repos.get(owner="owner", repo="repo")
237+
```
238+
239+
The models of versions can be imported from `githubkit.versions.<version>.models`, for example:
240+
241+
```python
242+
from githubkit.versions.v2022_11_28.models import FullRepository
243+
```
244+
245+
Specially, the `latest` version is always linked to the latest version of GitHub API:
246+
247+
```python
248+
from githubkit.versions.latest.models import FullRepository
249+
```
250+
251+
> [!NOTE]
252+
> For backward compatibility, the `githubkit.rest` module is linked to the models of `latest` version by default.
253+
>
254+
> ```python
255+
> from githubkit.rest import FullRepository
256+
> ```
257+
258+
You can also get the latest version name of GitHub API and all versions mapping of GitHub API:
259+
260+
```python
261+
from githubkit.versions import LATEST_VERSION, VERSIONS
262+
```
263+
195264
### Pagination
196265

197266
Pagination type checking is also supported:
198267

199268
> Typing is tested with Pylance (Pyright).
200269
201270
```python
202-
from githubkit.rest import Issue
271+
from githubkit.versions.latest.models import Issue
203272

204273
for issue in github.paginate(
205274
github.rest.issues.list_for_repo, owner="owner", repo="repo", state="open"
@@ -209,7 +278,7 @@ for issue in github.paginate(
209278
```
210279

211280
```python
212-
from githubkit.rest import Issue
281+
from githubkit.versions.latest.models import Issue
213282

214283
async for issue in github.paginate(
215284
github.rest.issues.async_list_for_repo, owner="owner", repo="repo", state="open"
@@ -241,11 +310,13 @@ data: Dict[str, Any] = github.graphql(query, variables={"foo": "bar"})
241310
Simple async call:
242311

243312
```python
244-
data: Dict[str, Any] = github.async_graphql(query, variables={"foo": "bar"})
313+
data: Dict[str, Any] = await github.async_graphql(query, variables={"foo": "bar"})
245314
```
246315

247316
### Webhook Verification
248317

318+
> `githubkit.webhooks` module contains some shortcut functions to help you verify and parse webhook payload.
319+
249320
Simple webhook payload verification:
250321

251322
```python
@@ -264,29 +335,41 @@ signature: str = sign(secret, payload, method="sha256")
264335

265336
### Webhook Parsing
266337

338+
> `githubkit.webhooks` module contains some shortcut functions to help you verify and parse webhook payload.
339+
267340
Parse the payload with event name:
268341

269342
```python
270-
from githubkit.webhooks import parse, WebhookEvent
343+
from githubkit.webhooks import parse
271344

272-
event: WebhookEvent = parse(request.headers["X-GitHub-Event"], request.body)
345+
event = parse(request.headers["X-GitHub-Event"], request.body)
273346
```
274347

275-
Parse the payload without event name (may cost longer time):
348+
(NOT RECOMMENDED) Parse the payload without event name (may cost longer time and more memory):
276349

277350
```python
278-
from githubkit.webhooks import parse_without_name, WebhookEvent
351+
from githubkit.webhooks import parse_without_name
279352

280-
event: WebhookEvent = parse_without_name(request.body)
353+
event = parse_without_name(request.body)
281354
```
282355

283356
Parse dict like payload:
284357

285358
```python
286-
from githubkit.webhooks import parse_obj, parse_obj_without_name, WebhookEvent
359+
from githubkit.webhooks import parse_obj, parse_obj_without_name
360+
361+
event = parse_obj(request.headers["X-GitHub-Event"], request.json())
362+
event = parse_obj_without_name(request.json()) # NOT RECOMMENDED
363+
```
364+
365+
The `parse` and `parse_obj` function supports type overload, if you provide static value for the `event_name` parameter, the return type will be inferred automatically.
366+
367+
Webhook also supports versioning, you can switch between versions as follows:
368+
369+
```python
370+
from githubkit import GitHub
287371

288-
event: WebhookEvent = parse_obj(request.headers["X-GitHub-Event"], request.json())
289-
event: WebhookEvent = parse_obj_without_name(request.json())
372+
event = GitHub.webhooks("2022-11-28").parse(request.headers["X-GitHub-Event"], request.body)
290373
```
291374

292375
### Switch between AuthStrategy
@@ -321,10 +404,19 @@ Open in Codespaces (Dev Container):
321404

322405
Generate latest models and apis:
323406

407+
> [!WARNING]
408+
> This may use a lot of memory (**16G+** RAM) and take a long time.
409+
324410
```bash
325411
python -m codegen && isort . && black .
326412
```
327413

414+
Run tests:
415+
416+
```bash
417+
pytest -n auto tests
418+
```
419+
328420
## Contributors
329421

330422
Thanks to the following people who have contributed to this project:

0 commit comments

Comments
 (0)