Skip to content

Commit 323f375

Browse files
authored
📝 add quick start to readme
1 parent 320613a commit 323f375

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,123 @@ githubkit supports **both pydantic v1 and v2**, but pydantic v2 is recommended.
7979
> You may occasionally encounter **breaking changes** like model names or model field types changing when upgrading githubkit.
8080
> This is due to **upstream schema changes** and githubkit can not control this.
8181
82+
## Quick Start
83+
84+
Here is some common use cases to help you get started quickly. For more detailed usage, please refer to the [Usage](#usage) section.
85+
86+
### Use personal access token (PAT) to call GitHub API
87+
88+
```python
89+
from githubkit import GitHub
90+
from githubkit.versions.latest.models import User
91+
92+
github = GitHub("<your_token_here>")
93+
94+
# call GitHub rest api
95+
resp = github.rest.users.get_authenticated()
96+
user: User = resp.parsed_data
97+
98+
# call GitHub graphql api
99+
data: dict = github.graphql("{ viewer { login } }")
100+
```
101+
102+
### Develop a OAuth APP with web flow
103+
104+
```python
105+
from githubkit.versions.latest.models import User
106+
from githubkit import GitHub, OAuthAppAuthStrategy, OAuthTokenAuthStrategy
107+
108+
github = GitHub(OAuthAppAuthStrategy("<client_id>", "<client_secret>"))
109+
110+
# redirect user to github oauth page and get the code from callback
111+
112+
# one time usage
113+
user_github = github.with_auth(github.auth.as_web_user("<code>"))
114+
115+
# or, store the user token in a database
116+
auth: OAuthTokenAuthStrategy = github.auth.as_web_user("<code>").exchange_token(github)
117+
access_token = auth.token
118+
refresh_token = auth.refresh_token
119+
# restore the user token from database
120+
user_github = github.with_auth(
121+
OAuthTokenAuthStrategy(
122+
"<client_id>", "<client_secret>", refresh_token=refresh_token
123+
)
124+
)
125+
126+
# now you can act as the user
127+
resp = user_github.rest.users.get_authenticated()
128+
user: User = resp.parsed_data
129+
```
130+
131+
### Develop a OAuth APP with device flow
132+
133+
```python
134+
from githubkit.versions.latest.models import User
135+
from githubkit import GitHub, OAuthDeviceAuthStrategy, OAuthTokenAuthStrategy
136+
137+
# sync/async func for displaying user code to user
138+
def callback(data: dict):
139+
print(data["user_code"])
140+
141+
user_github = GitHub(OAuthDeviceAuthStrategy("<client_id>", callback))
142+
143+
# if you want to store the user token in a database
144+
auth: OAuthTokenAuthStrategy = user_github.auth.exchange_token(user_github)
145+
access_token = auth.token
146+
refresh_token = auth.refresh_token
147+
# restore the user token from database
148+
user_github = user_github.with_auth(
149+
OAuthTokenAuthStrategy(
150+
"<client_id>", None, refresh_token=refresh_token
151+
)
152+
)
153+
```
154+
155+
### Develop a GitHub APP
156+
157+
Authenticating as a repository installation to do something with the repository:
158+
159+
```python
160+
from githubkit import GitHub, AppAuthStrategy
161+
from githubkit.versions.latest.models import Issue, Installation
162+
163+
github = GitHub(
164+
AppAuthStrategy("your_app_id", "your_private_key", "client_id", "client_secret")
165+
)
166+
167+
resp = github.rest.apps.get_repo_installation("owner", "repo")
168+
repo_installation: Installation = resp.parsed_data
169+
170+
installation_github = github.with_auth(
171+
github.auth.as_installation(repo_installation.id)
172+
)
173+
174+
resp = installation_github.rest.issues.get("owner", "repo", 1)
175+
issue: Issue = resp.parsed_data
176+
```
177+
178+
Authenticating as a user installation to do something on behalf of the user:
179+
180+
```python
181+
from githubkit import GitHub, AppAuthStrategy
182+
from githubkit.versions.latest.models import Installation, IssueComment
183+
184+
github = GitHub(
185+
AppAuthStrategy("your_app_id", "your_private_key", "client_id", "client_secret")
186+
)
187+
188+
resp = github.rest.apps.get_user_installation("username")
189+
user_installation: Installation = resp.parsed_data
190+
191+
installation_github = github.with_auth(
192+
github.auth.as_installation(user_installation.id)
193+
)
194+
195+
resp = installation_github.rest.issues.create_comment("owner", "repo", 1, body="Hello")
196+
issue: IssueComment = resp.parsed_data
197+
```
198+
82199
## Usage
83200

84201
### Authentication
@@ -180,6 +297,8 @@ github = GitHub(
180297
)
181298
```
182299

300+
See [Switch between AuthStrategy](#switch-between-authstrategy-installation-oauth-webdevice-flow) for more detail about oauth flow.
301+
183302
or using GitHub Action authentication:
184303

185304
```python

0 commit comments

Comments
 (0)