diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5e0a8f0..a1e7682 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,12 +5,11 @@ on: branches: - main tags: - - '**' + - "**" pull_request: {} env: COLUMNS: 150 - PDM_DEPS: 'urllib3<2,git+https://github.com/adriangb/findpython.git@version-timeout-env' FINDPYTHON_GET_VERSION_TIMEOUT: 30 jobs: @@ -20,7 +19,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ['3.9', '3.10', '3.11'] + python-version: ["3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v4 @@ -46,7 +45,7 @@ jobs: fail-fast: false matrix: os: [ubuntu, macos, windows] - python-version: ['3.9', '3.10', '3.11'] + python-version: ["3.9", "3.10", "3.11"] env: PYTHON: ${{ matrix.python-version }} diff --git a/.gitignore b/.gitignore index 364c2b6..1ec6ecf 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,4 @@ test.py /worktrees/ .pdm-python tmp/ +.pdm.toml diff --git a/garth/__init__.py b/garth/__init__.py index 08eeafe..7e12d79 100644 --- a/garth/__init__.py +++ b/garth/__init__.py @@ -30,7 +30,9 @@ "connectapi", "login", "resume", + "loads", "save", + "dumps", ] configure = client.configure diff --git a/garth/http.py b/garth/http.py index b22111c..8ec961e 100644 --- a/garth/http.py +++ b/garth/http.py @@ -1,3 +1,4 @@ +import base64 import json import os from typing import Dict, Optional, Tuple, Union @@ -166,6 +167,13 @@ def dump(self, dir_path: str): if self.oauth2_token: json.dump(asdict(self.oauth2_token), f, indent=4) + def dumps(self) -> str: + r = [] + r.append(asdict(self.oauth1_token)) + r.append(asdict(self.oauth2_token)) + s = json.dumps(r) + return base64.b64encode(s.encode()).decode() + def load(self, dir_path: str): dir_path = os.path.expanduser(dir_path) with open(os.path.join(dir_path, "oauth1_token.json")) as f: @@ -174,5 +182,12 @@ def load(self, dir_path: str): oauth2 = OAuth2Token(**json.load(f)) self.configure(oauth1_token=oauth1, oauth2_token=oauth2) + def loads(self, s: str): + oauth1, oauth2 = json.loads(base64.b64decode(s)) + self.configure( + oauth1_token=OAuth1Token(**oauth1), + oauth2_token=OAuth2Token(**oauth2), + ) + client = Client() diff --git a/tests/conftest.py b/tests/conftest.py index 31d2d0f..49326c3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -67,9 +67,8 @@ def authed_client( client.load(os.environ["GARTH_HOME"]) except KeyError: client.configure(oauth1_token=oauth1_token, oauth2_token=oauth2_token) - else: - assert client.oauth2_token - assert not client.oauth2_token.expired + assert client.oauth2_token + assert not client.oauth2_token.expired return client diff --git a/tests/test_http.py b/tests/test_http.py index fc93ad7..9a5b21d 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -19,6 +19,14 @@ def test_dump_and_load(authed_client: Client): assert new_client.oauth2_token == authed_client.oauth2_token +def test_dumps_and_loads(authed_client: Client): + s = authed_client.dumps() + new_client = Client() + new_client.loads(s) + assert new_client.oauth1_token == authed_client.oauth1_token + assert new_client.oauth2_token == authed_client.oauth2_token + + def test_configure_oauth2_token(client: Client, oauth2_token: OAuth2Token): assert client.oauth2_token is None client.configure(oauth2_token=oauth2_token)