Skip to content

Conversation

@glensc
Copy link
Contributor

@glensc glensc commented Jan 13, 2022

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
  • TraktApiTokenAuth - uses HttpClient and returns client_id, client_token
  • TraktApi - integrates TraktApiTokenAuth to HttpClient, provides get/post/put/delete methods
  • 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 can leave it as 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
  • bumps python version to 3.7 for @dataclass:

@glensc
Copy link
Contributor Author

glensc commented Jan 14, 2022

some question:

  • init() in code says it returns "token", but the implementation of every method returns different things. some return just token, some return whole response

and in docs it's not mentioned at all what the init returns:

should it return anything at all?

EDIT: return values as:

  • pin auth -> OAUTH_TOKEN
  • oauth -> OAUTH_TOKEN
  • device auth -> response

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

@glensc
Copy link
Contributor Author

glensc commented Jan 14, 2022

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...

@glensc
Copy link
Contributor Author

glensc commented Jan 14, 2022

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?

@glensc
Copy link
Contributor Author

glensc commented Jan 15, 2022

as of c8865dc tree, Includes #181

@glensc glensc changed the title PoC: New api client [4.0]: PoC: New api client Jan 15, 2022
@glensc glensc changed the title [4.0]: PoC: New api client [4.0.0]: PoC: New api client Jan 15, 2022
@glensc glensc changed the title [4.0.0]: PoC: New api client [4.0.0]: PoC: Refactor api client Jan 15, 2022
@glensc
Copy link
Contributor Author

glensc commented Jan 2, 2025

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant