Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply token via auth adapter #98

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions github_activity/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from requests.auth import AuthBase


class TokenAuth(AuthBase):
"""Apply Bearer token auth to request

Requests doesn't respect Authorization header reliably
unless applied via `auth` adapter.
"""

def __init__(self, token):
self.token = token

def __call__(self, r):
# add token to auth
r.headers["Authorization"] = f"Bearer {self.token}"
return r
8 changes: 5 additions & 3 deletions github_activity/github_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pytz
import requests

from .auth import TokenAuth
from .cache import _cache_data
from .graphql import GitHubGraphQlQuery

Expand Down Expand Up @@ -781,11 +782,12 @@ def _get_datetime_and_type(org, repo, datetime_or_git_ref, auth):
)


def _get_datetime_from_git_ref(org, repo, ref, auth):
def _get_datetime_from_git_ref(org, repo, ref, token):
"""Return a datetime from a git reference."""
headers = {"Authorization": "Bearer %s" % auth}
auth = TokenAuth(token)
url = f"https://api.github.com/repos/{org}/{repo}/commits/{ref}"
response = requests.get(url, headers=headers)
# prevent requests from using netrc
response = requests.get(url, auth=auth)
response.raise_for_status()
return dateutil.parser.parse(response.json()["commit"]["committer"]["date"])

Expand Down
10 changes: 6 additions & 4 deletions github_activity/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import requests
from tqdm.auto import tqdm

from .auth import TokenAuth

comments_query = """\
comments(last: 100) {
edges {
Expand Down Expand Up @@ -136,8 +138,8 @@ def __init__(self, query, display_progress=True, auth=None):
self.query = query

# Authentication
auth = auth or os.environ.get("GITHUB_ACCESS_TOKEN")
if not auth:
token = auth or os.environ.get("GITHUB_ACCESS_TOKEN")
if not token:
raise ValueError(
"Either the environment variable GITHUB_ACCESS_TOKEN or the "
"--auth flag or must be used to pass a Personal Access Token "
Expand All @@ -146,7 +148,7 @@ def __init__(self, query, display_progress=True, auth=None):
"working with a public repository, you don’t need to set any "
"scopes on the token you create."
)
self.headers = {"Authorization": "Bearer %s" % auth}
self.auth = TokenAuth(token)

self.gql_template = gql_template
self.display_progress = display_progress
Expand Down Expand Up @@ -183,7 +185,7 @@ def request(self, n_pages=100, n_per_page=50):
ii_request = requests.post(
"https://api.github.com/graphql",
json={"query": ii_gql_query},
headers=self.headers,
auth=self.auth,
)
if ii_request.status_code != 200:
raise Exception(
Expand Down
Loading