Skip to content

Commit 7a64bea

Browse files
authored
Merge pull request #26 from sypht-team/hotfix/add_token_expiry_buffer
added token expiry and blackify
2 parents ac82c1d + 010aefe commit 7a64bea

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import find_packages, setup
22

3-
__version__ = "0.5.9"
3+
__version__ = "0.5.10"
44
__pkg_name__ = "sypht"
55

66
setup(

sypht/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/usr/bin/env python
22

33
import argparse
4+
import json
45
import re
56
import sys
67
import textwrap
7-
import json
8+
89
from sypht.client import SyphtClient
910

1011

1112
class Extract(object):
12-
""" Extract values from a document. """
13+
"""Extract values from a document."""
1314

1415
def __init__(self, path, products):
1516
self.path = path

sypht/client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
from base64 import b64encode
44
from datetime import datetime, timedelta
55
from urllib.parse import quote_plus, urljoin
6+
67
import requests
78

89
SYPHT_API_BASE_ENDPOINT = "https://api.sypht.com"
910
SYPHT_AUTH_ENDPOINT = "https://auth.sypht.com/oauth2/token"
1011
SYPHT_LEGACY_AUTH_ENDPOINT = "https://login.sypht.com/oauth/token"
1112
SYPHT_OAUTH_COMPANY_ID_CLAIM_KEY = "https://api.sypht.com/companyId"
13+
TOKEN_EXPIRY_BUFFER_SECONDS = 10
1214

1315

1416
def _iter_chunked_sequence(seq, size):
@@ -131,16 +133,24 @@ def _is_token_expired(self):
131133
def _authenticate_client(self):
132134
if "/oauth/" in self.auth_endpoint:
133135
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,
135140
)
136141
elif "/oauth2/" in self.auth_endpoint:
137142
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,
139147
)
140148
else:
141149
raise ValueError(f"Invalid authentication endpoint: {self.auth_endpoint}")
142150

143-
self._auth_expiry = datetime.utcnow() + timedelta(seconds=expires_in)
151+
self._auth_expiry = datetime.utcnow() + timedelta(
152+
seconds=expires_in - TOKEN_EXPIRY_BUFFER_SECONDS
153+
)
144154
self._access_token = access_token
145155

146156
def _get_headers(self, **headers):
@@ -606,7 +616,8 @@ def search_entities(
606616
company_id = company_id or self.company_id
607617
entity_type = quote_plus(entity_type)
608618
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}/",
610621
)
611622
headers = self._get_headers()
612623
headers["Accept"] = "application/json"

0 commit comments

Comments
 (0)