Skip to content

Commit

Permalink
📝 Docs: add model dump guide (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Nov 22, 2024
1 parent 1f89565 commit 7702126
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/usage/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,40 @@ resp: Response[FullRepository] = github.rest.repos.get("owner", "repo")
repo: dict[str, Any] = resp.json()
```

=== "Pydantic v1"

If you have already got the parsed data and want to dump it into a dict object or JSON string, you can use the pydantic model's `dict` or `json` method:

```python hl_lines="8-9"
from typing import Any
from githubkit import Response
from githubkit.versions.latest.models import FullRepository

resp: Response[FullRepository] = github.rest.repos.get("owner", "repo")
repo: FullRepository = resp.parsed_data

repo_dict: dict[str, Any] = repo.dict(by_alias=True, exclude_unset=True)
repo_json: str = repo.json(by_alias=True, exclude_unset=True)
```

=== "Pydantic v2"

If you have already got the parsed data and want to dump it into a dict object or JSON string, you can use the pydantic model's `model_dump` or `model_dump_json` method:

```python hl_lines="8-11"
from typing import Any
from githubkit import Response
from githubkit.versions.latest.models import FullRepository

resp: Response[FullRepository] = github.rest.repos.get("owner", "repo")
repo: FullRepository = resp.parsed_data

repo_dict: dict[str, Any] = repo.model_dump(
mode="json", by_alias=True, exclude_unset=True
)
repo_json: str = repo.model_dump_json(by_alias=True, exclude_unset=True)
```

## REST API Versioning

githubkit supports multiple versions of GitHub API, you can switch between versions as follows:
Expand Down

0 comments on commit 7702126

Please sign in to comment.