Skip to content

Commit

Permalink
feat: can resume and dump from string (#5)
Browse files Browse the repository at this point in the history
* feat: can resume and dump from string

Signed-off-by: yihong0618 <[email protected]>

* fix: by review and open py3.8

* fix: open package bug

Signed-off-by: yihong0618 <[email protected]>

* fix: ci

Signed-off-by: yihong0618 <[email protected]>

* fix: python3.8 in requirement

* fix: drop 3.8 support

---------

Signed-off-by: yihong0618 <[email protected]>
  • Loading branch information
yihong0618 authored Sep 27, 2023
1 parent 899b726 commit b36caec
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ test.py
/worktrees/
.pdm-python
tmp/
.pdm.toml
2 changes: 2 additions & 0 deletions garth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"connectapi",
"login",
"resume",
"loads",
"save",
"dumps",
]

configure = client.configure
Expand Down
15 changes: 15 additions & 0 deletions garth/http.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import json
import os
from typing import Dict, Optional, Tuple, Union
Expand Down Expand Up @@ -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:
Expand All @@ -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()
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 8 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b36caec

Please sign in to comment.