Skip to content

Commit a8c74b0

Browse files
Merge pull request #1 from whitehead-ai/PROD-215
Raise AuthError when necessary
2 parents 69d44f2 + 351294c commit a8c74b0

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

poetry.lock

Lines changed: 43 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ cryptography = "^3.4.6"
1616
gql = {version = "3.0.0a5", extras = ["all"], allow-prereleases = true}
1717
py-gql-client = "^1.0.1"
1818
dataclasses = {version = "^0.6", python = "~3.6"}
19+
portalocker = "^2.3.0"
20+
pywin32 = {version = "==228", markers="sys_platform == 'win32'"}
1921

2022

2123
[build-system]

whitehead_sdk/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from gql.client import Client
22
from gql.transport.aiohttp import AIOHTTPTransport
33
from . import config, utils, token_cache
4+
from .exceptions import AuthError
45

56

67
def Whitehead(api_key, developer_id):
@@ -12,6 +13,8 @@ def Whitehead(api_key, developer_id):
1213
if not jwt_token:
1314
exchange_token, nonce = utils.create_exchange_token(api_key)
1415
auth_data = utils.request_jwt(developer_id, exchange_token)
16+
if auth_data.get("enc_token") is None:
17+
raise AuthError(auth_data.get("error"))
1518
jwt_token = utils.decrypt_jwt(auth_data, api_key, nonce)
1619
cache.write(jwt_token)
1720

whitehead_sdk/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class AuthError(Exception):
2+
pass

whitehead_sdk/token_cache.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
import os
2-
import fcntl
32
import hashlib
43
import tempfile
4+
import portalocker
55
from contextlib import contextmanager, suppress
66
from datetime import datetime
77

88

9-
@contextmanager
10-
def file_lock(fd, cmd):
11-
try:
12-
fcntl.lockf(fd, cmd)
13-
yield
14-
except IOError:
15-
yield
16-
finally:
17-
fcntl.lockf(fd, fcntl.LOCK_UN)
18-
19-
209
class TokenCache:
2110
def __init__(self, dev_id, api_key):
2211
self._hash = hashlib.md5(f"{dev_id}:{api_key}".encode()).hexdigest()
@@ -34,13 +23,11 @@ def cache_path(self):
3423

3524
def read(self):
3625
with suppress(FileNotFoundError):
37-
with open(self.cache_path, "r") as f:
38-
with file_lock(f, fcntl.LOCK_SH):
39-
return f.read()
26+
with portalocker.Lock(self.cache_path, "r") as f:
27+
return f.read()
4028

4129
def write(self, data):
42-
with open(self.cache_path, "a") as f:
43-
with file_lock(f, fcntl.LOCK_EX):
44-
f.seek(0)
45-
f.truncate(0)
46-
f.write(data)
30+
with portalocker.Lock(self.cache_path, "a") as f:
31+
f.seek(0)
32+
f.truncate(0)
33+
f.write(data)

0 commit comments

Comments
 (0)