Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not to get credentials token in every api call #426

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
tylerflex marked this conversation as resolved.
Show resolved Hide resolved
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