Skip to content

Commit 68a2b69

Browse files
authored
Merge pull request #33 from ebastos/main
Use dataclass for the session methods
2 parents 6675de8 + c5ae0f1 commit 68a2b69

2 files changed

Lines changed: 21 additions & 25 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "ws-api"
7-
version = "0.29.0"
7+
version = "0.29.1"
88
description = "Access your Wealthsimple account using their (GraphQL) API."
99
readme = "README.md"
1010
license = {file = "LICENSE"}

ws_api/session.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
1+
from dataclasses import dataclass, asdict
12
import json
2-
from abc import ABC
33

44

5-
class OAuthSession(ABC):
5+
@dataclass
6+
class OAuthSession:
67
"""
78
A class representing an OAuth session.
89
910
Attributes:
10-
client_id (str or None): OAuth Client ID, used in OAuth requests.
11-
access_token (str or None): OAuth Access Token, used to authenticate API requests.
12-
refresh_token (str or None): OAuth Refresh Token, used to obtain new access tokens when they expire.
11+
client_id: OAuth Client ID, used in OAuth requests.
12+
access_token: OAuth Access Token, used to authenticate API requests.
13+
refresh_token: OAuth Refresh Token, used to obtain new access tokens when they expire.
1314
"""
1415

15-
def __init__(self):
16-
self.client_id: str | None = None
17-
self.access_token = None
18-
self.refresh_token = None
16+
client_id: str | None = None
17+
access_token: str | None = None
18+
refresh_token: str | None = None
1919

2020

21+
@dataclass
2122
class WSAPISession(OAuthSession):
2223
"""
2324
A class representing a WSAPI session, extending OAuthSession.
2425
2526
Attributes:
26-
session_id (str or None): Session ID, sent in headers for OAuth requests.
27-
wssdi (str or None): Device ID, sent in headers of API requests.
28-
token_info (object or None): Cached result of getTokenInfo().
27+
session_id: Session ID, sent in headers for OAuth requests.
28+
wssdi: Device ID, sent in headers of API requests.
29+
token_info: Cached result of getTokenInfo().
2930
"""
3031

31-
def __init__(self):
32-
super().__init__()
33-
self.session_id: str | None = None
34-
self.wssdi: str | None = None
35-
self.token_info = None
32+
session_id: str | None = None
33+
wssdi: str | None = None
34+
token_info: dict | None = None
3635

37-
def to_json(self):
38-
return json.dumps(self, default=lambda o: o.__dict__, ensure_ascii=False)
36+
def to_json(self) -> str:
37+
return json.dumps(asdict(self), ensure_ascii=False)
3938

4039
@classmethod
41-
def from_json(cls, json_str):
42-
data = json.loads(json_str)
43-
obj = cls()
44-
obj.__dict__.update(data)
45-
return obj
40+
def from_json(cls, json_str: str) -> "WSAPISession":
41+
return cls(**json.loads(json_str))

0 commit comments

Comments
 (0)