Skip to content

Commit

Permalink
api: let user save keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolinski committed Mar 16, 2019
1 parent 4626e5f commit 57a729e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
9 changes: 8 additions & 1 deletion trakt/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, List, Optional, Type, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union

from trakt.core.abstract import AbstractApi, AbstractBaseModel
from trakt.core.components import DefaultHttpComponent, DefaultOauthComponent
Expand All @@ -21,6 +21,7 @@ def __init__(
http_component: Optional[Type[DefaultHttpComponent]] = None,
oauth_component: Optional[Type[DefaultOauthComponent]] = None,
countries_interface: Optional[Type[CountriesInterface]] = None,
authorization_keys: Optional[Dict[str, Any]] = None,
**config: str
) -> None:
AbstractBaseModel.set_client(self)
Expand All @@ -36,6 +37,12 @@ def __init__(
self.http = (http_component or DefaultHttpComponent)(self)
self.oauth = (oauth_component or DefaultOauthComponent)(self)

if authorization_keys:
self.config["authorization"] = authorization_keys
self.authenticated = True
self.oauth.token = authorization_keys["access_token"]
self.access_token = authorization_keys["access_token"]

self.countries = (countries_interface or CountriesInterface)(self, Executor)

def __getattr__(self, item: str) -> Executor:
Expand Down
6 changes: 5 additions & 1 deletion trakt/core/components/http_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
NotFound,
PreconditionFailed,
RateLimitExceeded,
RequestRelatedError,
ServerError,
ServiceUnavailable,
Unauthorized,
UnprocessableEntity,
RequestRelatedError,
)


Expand All @@ -37,6 +37,7 @@ def request(
method: str = "GET",
query_args: Dict[str, str] = None,
data: Any = None,
return_code: bool = False,
**kwargs: Any,
) -> Any:

Expand All @@ -51,6 +52,9 @@ def request(

self.handle_code(response.status_code)

if return_code:
return response.json(), response.status_code

return response.json()

def get_headers(self) -> Dict[str, str]:
Expand Down
17 changes: 16 additions & 1 deletion trakt/core/components/oauth.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import time
from typing import NamedTuple

from trakt.core.abstract import AbstractComponent
from trakt.core.decorators import auth_required
from trakt.core.exceptions import ClientError


class TokenResponse(NamedTuple):
Expand Down Expand Up @@ -117,7 +119,20 @@ def wait_for_verification(self, *, code: CodeResponse) -> TokenResponse:
"client_secret": self.client.client_secret,
}

ret = self.client.http.request("oauth/device/token", method="POST", data=data)
elapsed_time: float = 0
while True:
ret, status_code = self.client.http.request(
"oauth/device/token", method="POST", data=data, return_code=True
)
if status_code == 200:
break

elapsed_time += code.interval + 0.3

if elapsed_time > code.expires_in:
raise ClientError("Code expired; start the verification process again")

time.sleep(code.interval + 0.3)

token = TokenResponse(**ret)

Expand Down
3 changes: 3 additions & 0 deletions trakt/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def __init__(self, config: InternalConfigType) -> None:
def __getitem__(self, name: str) -> Any:
return self._config[name]

def __setitem__(self, name: str, value: Any) -> None:
self._config[name] = value


DEFAULT_CONFIG: InternalConfigType = {
"http": {"base_url": "https://api.trakt.tv", "max_retries": 3},
Expand Down

0 comments on commit 57a729e

Please sign in to comment.