Skip to content

Commit d76a190

Browse files
committed
Add auto token to gist
1 parent 7269840 commit d76a190

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

.github/workflows/main.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ jobs:
2222

2323
env:
2424
CIRUN: true
25+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
27+
permissions:
28+
contents: read
29+
gists: read
2530

2631
steps:
2732
- name: Checkout

fsspec/implementations/gist.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class GistFileSystem(AbstractFileSystem):
2323
If provided, fetch a particular revision of the gist. If omitted,
2424
the latest revision is used.
2525
username : str (optional)
26-
GitHub username for authentication (required if token is given).
26+
GitHub username for authentication .
2727
token : str (optional)
28-
GitHub personal access token (required if username is given).
28+
GitHub personal access token (required if username is given), or .
2929
timeout : (float, float) or float, optional
3030
Connect and read timeouts for requests (default 60s each).
3131
kwargs : dict
@@ -51,10 +51,8 @@ def __init__(
5151
self.gist_id = gist_id
5252
self.filenames = filenames
5353
self.sha = sha # revision of the gist (optional)
54-
if (username is None) ^ (token is None):
55-
# Both or neither must be set
56-
if username or token:
57-
raise ValueError("Auth requires both username and token, or neither.")
54+
if username is not None and token is None:
55+
raise ValueError("User auth requires a token")
5856
self.username = username
5957
self.token = token
6058
self.request_kw = kwargs
@@ -69,6 +67,8 @@ def kw(self):
6967
"""Auth parameters passed to 'requests' if we have username/token."""
7068
if self.username is not None and self.token is not None:
7169
return {"auth": (self.username, self.token), **self.request_kw}
70+
elif self.token is not None:
71+
return {"headers": {"Authorization": f"Bearer {self.token}"}}
7272
return self.request_kw
7373

7474
def _fetch_gist_metadata(self):

fsspec/implementations/tests/test_gist.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import sys
23

34
import pytest
@@ -8,13 +9,15 @@
89
if sys.version_info[:2] != (3, 12):
910
pytest.skip("Too many tests bust rate limit", allow_module_level=True)
1011

12+
token = os.getenv("GH_TOKEN", None)
13+
1114

1215
@pytest.mark.parametrize(
1316
"gist_id,sha",
1417
[("2656908684d3965b80c2", "2fb2f12f332f7e242b1a2af1f41e30ddf99f24c7")],
1518
)
1619
def test_gist_public_all_files(gist_id, sha):
17-
fs = fsspec.filesystem("gist", gist_id=gist_id, sha=sha)
20+
fs = fsspec.filesystem("gist", gist_id=gist_id, sha=sha, token=token)
1821
# Listing
1922
all_files = fs.ls("")
2023
assert len(all_files) == 2
@@ -36,7 +39,9 @@ def test_gist_public_all_files(gist_id, sha):
3639
],
3740
)
3841
def test_gist_public_one_file(gist_id, sha, file):
39-
fs = fsspec.filesystem("gist", gist_id=gist_id, sha=sha, filenames=[file])
42+
fs = fsspec.filesystem(
43+
"gist", gist_id=gist_id, sha=sha, filenames=[file], token=token
44+
)
4045
# Listing
4146
all_files = fs.ls("")
4247
assert len(all_files) == 1
@@ -59,7 +64,9 @@ def test_gist_public_one_file(gist_id, sha, file):
5964
)
6065
def test_gist_public_missing_file(gist_id, sha, file):
6166
with pytest.raises(FileNotFoundError):
62-
fsspec.filesystem("gist", gist_id=gist_id, sha=sha, filenames=[file])
67+
fsspec.filesystem(
68+
"gist", gist_id=gist_id, sha=sha, filenames=[file], token=token
69+
)
6370

6471

6572
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)