diff --git a/fsspec/implementations/gist.py b/fsspec/implementations/gist.py index 74117b544..ad9ac0b6a 100644 --- a/fsspec/implementations/gist.py +++ b/fsspec/implementations/gist.py @@ -14,21 +14,21 @@ class GistFileSystem(AbstractFileSystem): Parameters ---------- - gist_id : str + gist_id: str The ID of the gist you want to access (the long hex value from the URL). - filenames : list[str] (optional) + filenames: list[str] (optional) If provided, only make a file system representing these files, and do not fetch the list of all files for this gist. - sha : str (optional) + sha: str (optional) If provided, fetch a particular revision of the gist. If omitted, the latest revision is used. - username : str (optional) - GitHub username for authentication (required if token is given). - token : str (optional) - GitHub personal access token (required if username is given). - timeout : (float, float) or float, optional + username: str (optional) + GitHub username for authentication. + token: str (optional) + GitHub personal access token (required if username is given), or. + timeout: (float, float) or float, optional Connect and read timeouts for requests (default 60s each). - kwargs : dict + kwargs: dict Stored on `self.request_kw` and passed to `requests.get` when fetching Gist metadata or reading ("opening") a file. """ @@ -51,10 +51,8 @@ def __init__( self.gist_id = gist_id self.filenames = filenames self.sha = sha # revision of the gist (optional) - if (username is None) ^ (token is None): - # Both or neither must be set - if username or token: - raise ValueError("Auth requires both username and token, or neither.") + if username is not None and token is None: + raise ValueError("User auth requires a token") self.username = username self.token = token self.request_kw = kwargs @@ -67,9 +65,18 @@ def __init__( @property def kw(self): """Auth parameters passed to 'requests' if we have username/token.""" - if self.username is not None and self.token is not None: - return {"auth": (self.username, self.token), **self.request_kw} - return self.request_kw + kw = { + "headers": { + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + } + } + kw.update(self.request_kw) + if self.username and self.token: + kw["auth"] = (self.username, self.token) + elif self.token: + kw["headers"]["Authorization"] = f"Bearer {self.token}" + return kw def _fetch_gist_metadata(self): """ @@ -229,4 +236,6 @@ def cat(self, path, recursive=False, on_error="raise", **kwargs): pass # skip else: out[p] = e + if len(paths) == 1 and paths[0] == path: + return out[path] return out diff --git a/fsspec/implementations/tests/test_gist.py b/fsspec/implementations/tests/test_gist.py index dbd64a7cc..6ca31a454 100644 --- a/fsspec/implementations/tests/test_gist.py +++ b/fsspec/implementations/tests/test_gist.py @@ -1,17 +1,18 @@ -import sys - import pytest import fsspec from fsspec.implementations.gist import GistFileSystem -if (3, 12) < sys.version_info < (3, 14): - pytest.skip("Too many tests bust rate limit", allow_module_level=True) +# if sys.version_info[:2] != (3, 12): +# pytest.skip("Too many tests bust rate limit", allow_module_level=True) +pytest.skip( + "github requires a token right now, even for public gists", allow_module_level=True +) @pytest.mark.parametrize( "gist_id,sha", - [("2656908684d3965b80c2", "2fb2f12f332f7e242b1a2af1f41e30ddf99f24c7")], + [("863f2f06782788e349f2acfff31d77ed", None)], ) def test_gist_public_all_files(gist_id, sha): fs = fsspec.filesystem("gist", gist_id=gist_id, sha=sha) @@ -29,9 +30,9 @@ def test_gist_public_all_files(gist_id, sha): "gist_id,sha,file", [ ( - "2656908684d3965b80c2", - "2fb2f12f332f7e242b1a2af1f41e30ddf99f24c7", - "distributed_error_logs_PY3_7-3-2016", + "863f2f06782788e349f2acfff31d77ed", + None, + "ex1.ipynb", ) ], ) @@ -51,8 +52,8 @@ def test_gist_public_one_file(gist_id, sha, file): "gist_id,sha,file", [ ( - "2656908684d3965b80c2", - "2fb2f12f332f7e242b1a2af1f41e30ddf99f24c7", + "863f2f06782788e349f2acfff31d77ed", + None, "file-that-doesnt-exist.py", ) ], diff --git a/fsspec/implementations/tests/test_github.py b/fsspec/implementations/tests/test_github.py index 15bde1da0..800f1a3f0 100644 --- a/fsspec/implementations/tests/test_github.py +++ b/fsspec/implementations/tests/test_github.py @@ -4,7 +4,7 @@ import fsspec -if (3, 11) < sys.version_info < (3, 13): +if sys.version_info[:2] != (3, 13): pytest.skip("Too many tests bust rate limit", allow_module_level=True)