-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use GitHub API to get user email
fix: missing items in bind_user & fix logger Signed-off-by: jingfelix <[email protected]> feat: get user email by GitHub API Signed-off-by: jingfelix <[email protected]>
- Loading branch information
Showing
5 changed files
with
98 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import httpx | ||
from app import app | ||
|
||
|
||
def get_user_info(access_token: str) -> dict | None: | ||
"""Get user info by access token. | ||
Args: | ||
access_token (str): The user access token. | ||
Returns: | ||
dict: User info. | ||
""" | ||
|
||
with httpx.Client() as client: | ||
response = client.get( | ||
"https://api.github.com/user", | ||
headers={ | ||
"Accept": "application/vnd.github.v3+json", | ||
"Authorization": f"token {access_token}", | ||
}, | ||
) | ||
if response.status_code != 200: | ||
app.logger.debug(f"Failed to get user info. {response.text}") | ||
return None | ||
|
||
user_info = response.json() | ||
return user_info | ||
|
||
app.logger.debug("Failed to get user info.") | ||
return None | ||
|
||
|
||
def get_email(access_token: str) -> str | None: | ||
"""Get user email by access token. | ||
Args: | ||
access_token (str): The user access token. | ||
Returns: | ||
str: User email. | ||
""" | ||
|
||
with httpx.Client() as client: | ||
response = client.get( | ||
"https://api.github.com/user/emails", | ||
headers={ | ||
"Accept": "application/vnd.github.v3+json", | ||
"Authorization": f"Bearer {access_token}", | ||
"X-GitHub-Api-Version": "2022-11-28", | ||
}, | ||
) | ||
if response.status_code != 200: | ||
app.logger.debug(f"Failed to get user email. {response.text}") | ||
return None | ||
|
||
user_emails = response.json() | ||
if len(user_emails) == 0: | ||
app.logger.debug("Failed to get user email.") | ||
return None | ||
|
||
for user_email in user_emails: | ||
if user_email["primary"]: | ||
return user_email["email"] | ||
|
||
return user_emails[0]["email"] | ||
|
||
app.logger.debug("Failed to get user email.") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters