|
3 | 3 | from base64 import b64encode |
4 | 4 | from datetime import datetime, timedelta |
5 | 5 | from urllib.parse import quote_plus, urljoin |
| 6 | + |
6 | 7 | import requests |
7 | 8 |
|
8 | 9 | SYPHT_API_BASE_ENDPOINT = "https://api.sypht.com" |
9 | 10 | SYPHT_AUTH_ENDPOINT = "https://auth.sypht.com/oauth2/token" |
10 | 11 | SYPHT_LEGACY_AUTH_ENDPOINT = "https://login.sypht.com/oauth/token" |
11 | 12 | SYPHT_OAUTH_COMPANY_ID_CLAIM_KEY = "https://api.sypht.com/companyId" |
| 13 | +TOKEN_EXPIRY_BUFFER = 10 |
12 | 14 |
|
13 | 15 |
|
14 | 16 | def _iter_chunked_sequence(seq, size): |
@@ -131,16 +133,24 @@ def _is_token_expired(self): |
131 | 133 | def _authenticate_client(self): |
132 | 134 | if "/oauth/" in self.auth_endpoint: |
133 | 135 | access_token, expires_in = self._authenticate_v1( |
134 | | - self.auth_endpoint, self.client_id, self._client_secret, audience=self.audience |
| 136 | + self.auth_endpoint, |
| 137 | + self.client_id, |
| 138 | + self._client_secret, |
| 139 | + audience=self.audience, |
135 | 140 | ) |
136 | 141 | elif "/oauth2/" in self.auth_endpoint: |
137 | 142 | access_token, expires_in = self._authenticate_v2( |
138 | | - self.auth_endpoint, self.client_id, self._client_secret, audience=self.audience |
| 143 | + self.auth_endpoint, |
| 144 | + self.client_id, |
| 145 | + self._client_secret, |
| 146 | + audience=self.audience, |
139 | 147 | ) |
140 | 148 | else: |
141 | 149 | raise ValueError(f"Invalid authentication endpoint: {self.auth_endpoint}") |
142 | 150 |
|
143 | | - self._auth_expiry = datetime.utcnow() + timedelta(seconds=expires_in) |
| 151 | + self._auth_expiry = datetime.utcnow() + timedelta( |
| 152 | + seconds=expires_in - TOKEN_EXPIRY_BUFFER |
| 153 | + ) |
144 | 154 | self._access_token = access_token |
145 | 155 |
|
146 | 156 | def _get_headers(self, **headers): |
@@ -606,7 +616,8 @@ def search_entities( |
606 | 616 | company_id = company_id or self.company_id |
607 | 617 | entity_type = quote_plus(entity_type) |
608 | 618 | endpoint = urljoin( |
609 | | - endpoint or self.base_endpoint, f"storage/{company_id}/entitysearch/{entity_type}/" |
| 619 | + endpoint or self.base_endpoint, |
| 620 | + f"storage/{company_id}/entitysearch/{entity_type}/", |
610 | 621 | ) |
611 | 622 | headers = self._get_headers() |
612 | 623 | headers["Accept"] = "application/json" |
|
0 commit comments