Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: can resume and dump from string #5

Merged
merged 6 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
matin marked this conversation as resolved.
Show resolved Hide resolved
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
matin marked this conversation as resolved.
Show resolved Hide resolved
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