Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Login to Support Manual Polling #307

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ print(credentials)
}
```

#### No polling
You can avoid the polling mechanism by providing the polling_id directly

```python
client = TgtgClient(email="<your_email>")
polling_id = client.request_polling_id()
# You now click in the link you received by email
client.complete_login_with_polling_id(cached_polling_id)
credentials = client.get_credentials()
```

### Build the client from tokens

```python
Expand Down
60 changes: 32 additions & 28 deletions tgtg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,36 +152,40 @@ def login(self):
if self._already_logged:
self._refresh_token()
else:
response = self.session.post(
self._get_url(AUTH_BY_EMAIL_ENDPOINT),
headers=self._headers,
json={
"device_type": self.device_type,
"email": self.email,
},
proxies=self.proxies,
timeout=self.timeout,
)
if response.status_code == HTTPStatus.OK:
first_login_response = response.json()
if first_login_response["state"] == "TERMS":
raise TgtgPollingError(
f"This email {self.email} is not linked to a tgtg account. "
"Please signup with this email first."
)
elif first_login_response["state"] == "WAIT":
self.start_polling(first_login_response["polling_id"])
else:
raise TgtgLoginError(response.status_code, response.content)
polling_id = self.request_polling_id()
self.complete_login_with_polling_id(polling_id)

def request_polling_id(self):
response = self.session.post(
self._get_url(AUTH_BY_EMAIL_ENDPOINT),
headers=self._headers,
json={
"device_type": self.device_type,
"email": self.email,
},
proxies=self.proxies,
timeout=self.timeout,
)
if response.status_code == HTTPStatus.OK:
first_login_response = response.json()
if first_login_response["state"] == "TERMS":
raise TgtgPollingError(
f"This email {self.email} is not linked to a tgtg account. "
"Please signup with this email first."
)
elif first_login_response["state"] == "WAIT":
return first_login_response["polling_id"]
else:
if response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
raise TgtgAPIError(
response.status_code, "Too many requests. Try again later."
)
else:
raise TgtgLoginError(response.status_code, response.content)
raise TgtgLoginError(response.status_code, response.content)
else:
if response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
raise TgtgAPIError(
response.status_code, "Too many requests. Try again later."
)
else:
raise TgtgLoginError(response.status_code, response.content)

def start_polling(self, polling_id):
def complete_login_with_polling_id(self, polling_id):
for _ in range(MAX_POLLING_TRIES):
response = self.session.post(
self._get_url(AUTH_POLLING_ENDPOINT),
Expand Down