@@ -34,21 +34,30 @@ class GitHubURL:
34
34
def default_branch (self ) -> str :
35
35
"""Default GitHub branch."""
36
36
# get_default_branch() is memoized
37
- return get_default_branch (self .api_url .url )
37
+ return get_default_branch (self .api_url .url , token = self . token )
38
38
39
39
@property
40
- def credentials (self ) -> tuple [ str , str ] | tuple [()] :
41
- """Credentials encoded in this URL.
40
+ def token (self ) -> str | None :
41
+ """Token encoded in this URL.
42
42
43
- A tuple of ``(api_token, '')`` if present, or empty tuple otherwise. If
44
- the value of ``api_token`` begins with ``$``, it will be replaced with
45
- the value of the environment corresponding to the remaining part of the
43
+ If present and it starts with a ``$``, it will be replaced with the
44
+ value of the environment corresponding to the remaining part of the
46
45
string.
46
+
47
47
"""
48
48
token = self .auth_token
49
49
if token is not None and token .startswith ("$" ):
50
50
token = os .getenv (token [1 :])
51
+ return token
51
52
53
+ @property
54
+ def credentials (self ) -> tuple [str , str ] | tuple [()]:
55
+ """Credentials encoded in this URL.
56
+
57
+ A tuple of ``(api_token, '')`` if present, or empty tuple otherwise.
58
+
59
+ """
60
+ token = self .token
52
61
return (token , "" ) if token else ()
53
62
54
63
@property
@@ -128,18 +137,18 @@ def _build_url(self, scheme: str) -> furl:
128
137
129
138
130
139
@lru_cache ()
131
- def get_default_branch (api_url : str ) -> str :
140
+ def get_default_branch (api_url : str , * , token : str | None = None ) -> str :
132
141
"""Get the default branch from the GitHub repo using the API.
133
142
134
- For now, the request is not authenticated on GitHub, so it might hit a rate limit with:
143
+ For now, for URLs without an authorization token embedded, the request is
144
+ not authenticated on GitHub, so it might hit a rate limit with:
135
145
``requests.exceptions.HTTPError: 403 Client Error: rate limit exceeded for url``
136
146
137
147
This function is using ``lru_cache()`` as a simple memoizer, trying to avoid this rate limit error.
138
148
139
- Another option for the future: perform an authenticated request to GitHub.
140
- That would require some user credentials.
141
149
"""
142
- response = API_SESSION .get (api_url )
150
+ headers = {"Authorization" : f"token { token } " } if token else None
151
+ response = API_SESSION .get (api_url , headers = headers )
143
152
response .raise_for_status ()
144
153
145
154
return response .json ()["default_branch" ]
0 commit comments