Skip to content

Commit

Permalink
Add formatting and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
cheziyi committed May 29, 2024
1 parent 4a1a31a commit 2efb512
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/lint-format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Linting and code formatting checks

on:
pull_request:
push:
branches:
- master

jobs:
lint-format-check:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: pip install .[all]

- name: Linting check
run: mypy src

- name: Formatting check
run: black src --check
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ dependencies = []

[project.optional-dependencies]
system-token-issuer = ["boto3", "boto3-stubs[lambda]", "PyJWT"]
http-bearer-token-client = ['requests']
all = ["synnax-shared[system-token-issuer,http-bearer-token-client]"]
http-bearer-token-client = ["requests", "types-requests"]
dev = ["mypy", "black"]
all = ["synnax-shared[system-token-issuer,http-bearer-token-client,dev]"]

[tool.setuptools.package-data]
"synnax_shared" = ["py.typed"]
Empty file added src/synnax_shared/py.typed
Empty file.
19 changes: 11 additions & 8 deletions src/synnax_shared/system_token_issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ def __init__(

def get_token(self) -> str:
if self.token is None:
self.refresh_token()
return self.refresh_token()

if self.is_token_expiring():
self.refresh_token()
if self.is_token_expiring(self.token):
return self.refresh_token()

return self.token

def refresh_token(self) -> None:
def refresh_token(self) -> str:
logging.info("Refreshing token")
response = self.lambda_client.invoke(
FunctionName=self.function_name,
Expand All @@ -44,10 +42,15 @@ def refresh_token(self) -> None:
),
)
payload = json.loads(response["Payload"].read())

if payload["token"] is None:
raise Exception("Token not returned from token issuer")

self.token = payload["token"]
return payload["token"]

def is_token_expiring(self) -> bool:
payload = jwt.decode(self.token, options={"verify_signature": False})
def is_token_expiring(self, token: str) -> bool:
payload = jwt.decode(token, options={"verify_signature": False})
epoch_time = int(time.time())
if payload["exp"] - epoch_time < self.refresh_before_expiry_seconds:
return True
Expand Down

0 comments on commit 2efb512

Please sign in to comment.