From 5b0c70024a91df15f4a3ef68990e79dcd3cac224 Mon Sep 17 00:00:00 2001 From: Zev Averbach Date: Sat, 23 Apr 2022 15:38:51 +0200 Subject: [PATCH] further simplified constructors, renamed parent class of token stores, made it an official ABC, updated README --- README.md | 19 +++++++++++++++++-- avt_fresh/token.py | 18 +++++------------- setup.py | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 919aa79..e07838c 100644 --- a/README.md +++ b/README.md @@ -134,11 +134,26 @@ Finally, once you have an app on that developer page, click into it and click "g You should only have to do this once in each environment you use this library in. +# OAuth Token Stores + +By default this library stores OAuth tokens on disk in whatever working directory its methods are called from. As an alternative you can use Redis via the `avt_fresh.token.TokenStoreOnRedis` at instantiation of an `ApiClient` like so: + +```python +client = Client( + client_secret="...", + client_id="...", + redirect_uri="https://...", + account_id="...", + token_store=avt_fresh.token.TokenStoreOnRedis, + connection_string="redis://..." , +) +``` + +As a further alternative, feel free to implement and inject your own! See `avt_fresh.token.TokenStore` for the API, but tl;dr simply inherit from `TokenStore` and implement `get()` and `set()` methods, the former of which should return an instance of `avt_fresh.token.TokenTup`. + # Hardcoded Stuff / TODOs Here are some quirks and TODOs. PRs are welcome!: -OAuth tokens are currently saved in the ever-so-insecure path of `~/freshbooks_oauth_token.json`. TODO: don't do this anymore. ¯\_(ツ)_/¯ - Only Python 3.10 is supported at the moment. When it comes to invoice statuses, we're only using `v3_status` strings, not the numbers. What's more, when you create an invoice we're only supporting two possible statuses: "draft" and "paid". diff --git a/avt_fresh/token.py b/avt_fresh/token.py index 01a7e5d..3b4da2a 100644 --- a/avt_fresh/token.py +++ b/avt_fresh/token.py @@ -28,14 +28,10 @@ class TokenTup(typing.NamedTuple): @dataclass -class Token: +class TokenStore(metaclass=abc.ABCMeta): @abc.abstractmethod - def get(cls) -> "Token": - ... - - @abc.abstractmethod - def delete(self) -> None: + def get(cls) -> TokenTup: ... @abc.abstractmethod @@ -43,10 +39,7 @@ class Token: ... -class TokenStoreOnDisk(Token): - - def __init__(self, **kwargs): - super().__init__(**kwargs) +class TokenStoreOnDisk(TokenStore): @classmethod def get(cls) -> TokenTup: @@ -64,10 +57,9 @@ class TokenStoreOnDisk(Token): json.dump(token_dict, fout) -class TokenStoreOnRedis(Token): +class TokenStoreOnRedis(TokenStore): - def __init__(self, redis_url, **kwargs): - super().__init__(**kwargs) + def __init__(self, redis_url): self.redis_client = redis.from_url(redis_url) def get(self) -> TokenTup: diff --git a/setup.py b/setup.py index a4f3e07..bf84bda 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name="avt_fresh", author="Zev Averbach", author_email="zev@averba.ch", - version="0.0.14", + version="0.0.15", license="MIT", python_requires=">3.10.0", keywords="freshbooks API",