Skip to content

Commit

Permalink
not to get credentials token in every api call
Browse files Browse the repository at this point in the history
  • Loading branch information
JunchengXue committed Jun 28, 2022
1 parent 7704337 commit 1e8986a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion requirements/core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pydantic>=1.9.0
PyYAML
boto3==1.23.1
requests
dask
dask
pyjwt
13 changes: 12 additions & 1 deletion tidy3d/web/httputils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
""" handles communication with server """
# import os
import time
from typing import Dict
from enum import Enum

import jwt
import requests

from .auth import get_credentials, MAX_ATTEMPTS
Expand Down Expand Up @@ -61,9 +63,18 @@ def get_query_url(method: str) -> str:
# return os.path.join(Config.web_api_endpoint, method)


def need_token_refresh(token: str) -> bool:
"""check whether to refresh token or not"""
decoded = jwt.decode(token, options={"verify_signature": False})
return decoded["exp"] - time.time() < 300


def get_headers() -> Dict[str, str]:
"""get headers for http request"""
get_credentials()
if Config.auth is None or Config.auth["accessToken"] is None:
get_credentials()
elif need_token_refresh(Config.auth["accessToken"]):
get_credentials()
access_token = Config.auth["accessToken"]
return {
"Authorization": f"Bearer {access_token}",
Expand Down

0 comments on commit 1e8986a

Please sign in to comment.