[4.0.0]: PoC: Refactor api client#178
Draft
glensc wants to merge 193 commits intomoogar0880:masterfrom
Draft
Conversation
Contributor
Author
|
some question:
and in docs it's not mentioned at all what the init returns: should it return anything at all? EDIT: return values as:
EDIT 2: the device auth was added in: but perhaps it wasn't reviewed properly what it returns. my suggestion is to change function not to return anything. EDIT 3: removed in 97a8541 |
it's not consistent, and not useful see moogar0880/PyTrakt#178 (comment)
Contributor
Author
|
Also device_auth prints on success: success_message = (
"You've been successfully authenticated. "
"With access_token {access_token} and refresh_token {refresh_token}"
)while oauth_auth does basically the same, it doesn't print anything... |
Contributor
Author
|
tree at 4e143b8: tested application run, tested device auth, oauth auth. could not test pin auth as I don't have application id, it's no longer supported method? |
This reverts commit 9e165f1.
Contributor
Author
Contributor
Author
|
Carrying to glensc/python-pytrakt#51 |
glensc
added a commit
to glensc/python-pytrakt
that referenced
this pull request
Jan 8, 2025
Carrying: - moogar0880/PyTrakt#178 --- Refactors bunch of different logic stuffed to `core.py` into more specific classes following `SOLID` principles. New classes: - `HttpClient` - trakt api client that takes `base_url` and implements `get`/`post`/`put`/`delete` methods, additionally can use requests `auth` - `TokenAuth` - class implementing `requests` module custom auth to supply tokens as request headers - <del>`TraktApiTokenAuth` - uses `HttpClient` and returns `client_id`, `client_token`</del> - <del>`TraktApi` - integrates `TraktApiTokenAuth` to `HttpClient`, provides `get`/`post`/`put`/`delete` methods</del> - `AuthConfig` - class to deal with loading/storing auth tokens and carry their state runtime - `DeviceAuthAdapter`, `OAuthAdapter`, `PinAuthAdapter` - dealing with specific `trakt.init()` handling The `HttpClient` could be used directly. if someone wants to make raw queries without the abstraction of the object they can use just the API client class. this is not documented, so we can leave it as an internal detail. reasons of using `HttpClient` directly could be any of: - performance - memory - lack of higher-level abstraction Some implementation comments: - `api()` and `config()` in core module are functions, so they are evaluated after module load, to be able to set constants in `core.py` before the use of these constants - `@lru_cache(maxsize=None)` is used to memorize function result. while `maxsize=1` would work too `maxsize=None` uses simpler code internally (disables lru, never expires) - (in future) in classes, `@lru_cache` stacked with`@property`, could be replaced with `@static_property` if python is bumped to 3.8 - `AuthConfig` was needed to carry the tokens, as they are updated in memory at runtime - <del>bumps python version to 3.7 for `@dataclass`: https://docs.python.org/3/library/dataclasses.html#module-dataclasses</del>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactors bunch of different logic stuffed to core.py into more specific classes following SOLID principles.
New classes:
HttpClient- trakt api client that takesbase_urland implementsget/post/put/deletemethods, additionally can use requestsauthTokenAuth- class implementingrequestsmodule custom auth to supply tokens as request headersTraktApiTokenAuth- usesHttpClientand returnsclient_id,client_tokenTraktApi- integratesTraktApiTokenAuthtoHttpClient, providesget/post/put/deletemethodsAuthConfig- class to deal with loading/storing auth tokens and carry their state runtimetrakt.init()handlingThe
HttpClientcould be used directlyif someone wants to make raw queries without the abstraction of the object they can use just the API client class. this is not documented, so can leave it as internal detail.
reasons of using
HttpClientdirectly could be any of:Some implementation comments:
api()andconfig()in core module are functions, so they are evaluated after module load, to be able to set constants incore.pybefore the use of these constants@lru_cache(maxsize=None)is used to memorize function result. whilemaxsize=1would work toomaxsize=Noneuses simpler code internally (disables lru, never expires)@lru_cachestacked with@property, could be replaced with@static_propertyif python is bumped to 3.8AuthConfigwas needed to carry the tokens, as they are updated in memory at runtime@dataclass: