Skip to content

Commit

Permalink
add "get name from token" method to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Dec 19, 2023
1 parent e124788 commit b90f900
Showing 1 changed file with 13 additions and 36 deletions.
49 changes: 13 additions & 36 deletions straders_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sys import stdout
from datetime import datetime, timedelta
import random
import json, base64
import time
from .local_response import LocalSpaceTradersRespose
import threading
Expand Down Expand Up @@ -107,43 +108,19 @@ def request_and_validate(
return RemoteSpaceTradersRespose(packaged_request.response)


def old_request_and_validate(
method, url, data=None, json=None, headers=None, params=None
):
if method == "GET":
r_method = requests.get
elif method == "POST":
r_method = requests.post
elif method == "PATCH":
r_method = requests.patch

else:
return LocalSpaceTradersRespose("Method %s not supported", 0, 0, url)

start = datetime.now()
resp = False
try:
response = r_method(
url, data=data, json=json, headers=headers, params=params, timeout=5
)
except (requests.exceptions.ConnectionError, TimeoutError, TypeError) as err:
logging.error("ConnectionError: %s, %s", url, err)
return LocalSpaceTradersRespose(
"Could not connect!! network issue?", 404, 0, url
)
def get_name_from_token(token: str) -> str:
# Split the JWT into its three components

except Exception as err:
logging.error("Error: %s, %s", url, err)
return LocalSpaceTradersRespose(f"Could not connect!! {err}", 404, 0, url)
_log_response(response)
if response.status_code == 429:
logging.debug("Rate limited")
if st_log_client:
st_log_client.log_429(url, RemoteSpaceTradersRespose(response))
sleep(0.1)
return request_and_validate(method, url, data, json, headers, params)
else:
return RemoteSpaceTradersRespose(response)
# take the "Bearer " off the front

header_b64, payload_b64, signature = token.split(".")

# Base64 decode the header and payload
payload = json.loads(base64.urlsafe_b64decode(payload_b64 + "=="))

identifier = payload.get("identifier", None)

return identifier


def get_and_validate(
Expand Down

0 comments on commit b90f900

Please sign in to comment.