Skip to content

Commit

Permalink
perf: cache authentication headers (#104)
Browse files Browse the repository at this point in the history
* perf(github): cache login headers

* perf(gitlab): cache login headers
  • Loading branch information
cmdoret authored Dec 20, 2023
1 parent 192ec7d commit f34d8e3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
12 changes: 5 additions & 7 deletions gimie/extractors/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def list_files(self) -> List[RemoteResource]:
file = RemoteResource(
path=item["name"],
url=f'{repo_url}/raw/{defaultbranchref}/{item["path"]}',
headers=self._set_auth(),
headers=self._headers,
)
file_list.append(file)
return file_list
Expand Down Expand Up @@ -233,9 +233,7 @@ def _repo_data(self) -> Dict[str, Any]:
}
}
"""
response = send_graphql_query(
GH_API, repo_query, data, self._set_auth()
)
response = send_graphql_query(GH_API, repo_query, data, self._headers)

if "errors" in response:
raise ValueError(response["errors"])
Expand All @@ -246,14 +244,14 @@ def _fetch_contributors(self) -> List[Person]:
"""Queries the GitHub GraphQL API to extract contributors through the commit list.
NOTE: This is a workaround for the lack of a contributors field in the GraphQL API.
"""
headers = self._set_auth()
contributors = []
resp = query_contributors(self.url, headers)
resp = query_contributors(self.url, self._headers)
for user in resp:
contributors.append(self._get_user(user))
return list(contributors)

def _set_auth(self) -> Any:
@cached_property
def _headers(self) -> Any:
"""Set authentication headers for GitHub API requests."""
try:
if not self.token:
Expand Down
9 changes: 5 additions & 4 deletions gimie/extractors/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def list_files(self) -> List[RemoteResource]:
file = RemoteResource(
path=item["name"],
url=f'{self.url}/-/raw/{defaultbranchref}/{item["name"]}',
headers=self._set_auth(),
headers=self._headers,
)
file_list.append(file)
return file_list
Expand Down Expand Up @@ -226,14 +226,15 @@ def _repo_data(self) -> Dict[str, Any]:
}
"""
response = send_graphql_query(
self.graphql_endpoint, project_query, data, self._set_auth()
self.graphql_endpoint, project_query, data, self._headers
)
if "errors" in response:
raise ValueError(response["errors"])

return response["data"]["project"]

def _set_auth(self) -> Any:
@cached_property
def _headers(self) -> Any:
"""Set authentication headers for Gitlab API requests."""
try:
if not self.token:
Expand Down Expand Up @@ -280,7 +281,7 @@ def _user_from_rest(self, username: str) -> Person:
author = send_rest_query(
self.rest_endpoint,
f"/users?username={username}",
self._set_auth(),
self._headers,
)
if isinstance(author, list):
author = author[0]
Expand Down

0 comments on commit f34d8e3

Please sign in to comment.