From a7738203dde3aef3a8ba485671f7215e841ffab9 Mon Sep 17 00:00:00 2001 From: C'tri Goudie Date: Tue, 27 Feb 2024 15:55:56 +0000 Subject: [PATCH] consolidate responses, rename waypoint slicer --- .vscode/launch.json | 3 ++- setup.py | 12 +++++++++++- straders_sdk/client_api.py | 6 +++--- straders_sdk/client_json_cache.py | 2 +- straders_sdk/client_mediator.py | 6 +++--- straders_sdk/client_pg_logger.py | 8 ++++++-- straders_sdk/client_postgres.py | 2 +- straders_sdk/models_misc.py | 6 ++++-- straders_sdk/models_ship.py | 17 ++++++++--------- straders_sdk/pg_pieces/agents.py | 2 +- straders_sdk/pg_pieces/construction_sites.py | 2 +- straders_sdk/pg_pieces/contracts.py | 2 +- straders_sdk/pg_pieces/extractions.py | 2 +- straders_sdk/pg_pieces/jump_gates.py | 8 ++++---- straders_sdk/pg_pieces/select_ship.py | 2 +- straders_sdk/pg_pieces/transactions.py | 2 +- straders_sdk/pg_pieces/upsert_market.py | 6 +++--- straders_sdk/pg_pieces/upsert_ship.py | 2 +- straders_sdk/request_consumer.py | 8 ++++++++ .../{local_response.py => resp_local_resp.py} | 0 .../{responses.py => resp_responses.py} | 3 ++- straders_sdk/responses/__init__.py | 2 ++ straders_sdk/utils.py | 19 +++++++++++++++++-- 23 files changed, 82 insertions(+), 40 deletions(-) rename straders_sdk/{local_response.py => resp_local_resp.py} (100%) rename straders_sdk/{responses.py => resp_responses.py} (98%) create mode 100644 straders_sdk/responses/__init__.py diff --git a/.vscode/launch.json b/.vscode/launch.json index 408a94b..882569d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,8 @@ "type": "python", "request": "launch", "program": "${file}", - "console": "integratedTerminal" + "console": "integratedTerminal", + "justMyCode": False }, { "name": "Python: main.py", diff --git a/setup.py b/setup.py index fad85c9..ba455a5 100644 --- a/setup.py +++ b/setup.py @@ -8,6 +8,16 @@ "straders": "straders_sdk", # Map "straders" package to "straders_sdk" directory "straders.pathfinder": "straders_sdk/pathfinder", # Map " "straders.pg_pieces": "straders_sdk/pg_pieces", # Map "straders.pg_pieces" package to "straders_sdk/pg_pieces" directory + "straders.clients": "straders_sdk/clients", # Map "straders.clients" package to "straders_sdk/clients" directory + "straders.models": "straders_sdk/models", # Map "straders.models" package to "straders_sdk/models" directory + "straders.responses": "straders_sdk/responses", # Map "straders.responses" package to "straders_sdk/responses" directory }, - packages=["straders_sdk", "straders_sdk.pg_pieces", "straders_sdk.pathfinder"], + packages=[ + "straders_sdk", + "straders_sdk.pg_pieces", + "straders_sdk.pathfinder", + "straders_sdk.clients", + "straders_sdk.models", + "straders_sdk.responses", + ], ) diff --git a/straders_sdk/client_api.py b/straders_sdk/client_api.py index cd864bd..8f7ab07 100644 --- a/straders_sdk/client_api.py +++ b/straders_sdk/client_api.py @@ -10,9 +10,9 @@ post_and_validate, get_and_validate_paginated, get_and_validate_page, - waypoint_slicer, + waypoint_to_system, ) -from .local_response import LocalSpaceTradersRespose # +from .resp_local_resp import LocalSpaceTradersRespose # from .models_misc import ( Waypoint, Survey, @@ -100,7 +100,7 @@ def view_my_contracts(self) -> list["Contract"] or SpaceTradersResponse: def waypoints_view_one( self, waypoint_symbol, force=False ) -> Waypoint or SpaceTradersResponse: - system_symbol = waypoint_slicer(waypoint_symbol) + system_symbol = waypoint_to_system(waypoint_symbol) if waypoint_symbol == "": raise ValueError("waypoint_symbol cannot be empty") url = _url(f"systems/{system_symbol}/waypoints/{waypoint_symbol}") diff --git a/straders_sdk/client_json_cache.py b/straders_sdk/client_json_cache.py index e2ec2d4..2f8ae3b 100644 --- a/straders_sdk/client_json_cache.py +++ b/straders_sdk/client_json_cache.py @@ -1,7 +1,7 @@ from typing import Protocol from .models_misc import Waypoint, Survey, Market, System from .responses import SpaceTradersResponse -from .local_response import LocalSpaceTradersRespose +from .resp_local_resp import LocalSpaceTradersRespose from .client_interface import SpaceTradersClient import json import os diff --git a/straders_sdk/client_mediator.py b/straders_sdk/client_mediator.py index 4900d17..6b0e35f 100644 --- a/straders_sdk/client_mediator.py +++ b/straders_sdk/client_mediator.py @@ -1,9 +1,9 @@ from .utils import get_and_validate, get_and_validate_paginated, post_and_validate, _url -from .utils import ApiConfig, _log_response, waypoint_slicer +from .utils import ApiConfig, _log_response, waypoint_to_system from .client_interface import SpaceTradersInteractive, SpaceTradersClient import time from .responses import SpaceTradersResponse -from .local_response import LocalSpaceTradersRespose +from .resp_local_resp import LocalSpaceTradersRespose from .models_contracts import Contract from .models_misc import ( Waypoint, @@ -390,7 +390,7 @@ def waypoints_view_one( ) -> Waypoint or SpaceTradersResponse: # check self self.set_connections() - system_symbol = waypoint_slicer(waypoint_symbol) + system_symbol = waypoint_to_system(waypoint_symbol) if waypoint_symbol in self.waypoints and not force: return self.waypoints[waypoint_symbol] if not force: diff --git a/straders_sdk/client_pg_logger.py b/straders_sdk/client_pg_logger.py index 4b6b4df..a7b8316 100644 --- a/straders_sdk/client_pg_logger.py +++ b/straders_sdk/client_pg_logger.py @@ -6,7 +6,11 @@ from .responses import SpaceTradersResponse from .pg_pieces.transactions import _upsert_transaction from .pg_pieces.extractions import _upsert_extraction -from straders_sdk.utils import try_execute_select, try_execute_upsert, waypoint_slicer +from straders_sdk.utils import ( + try_execute_select, + try_execute_upsert, + waypoint_to_system, +) import psycopg2 import uuid import json @@ -411,7 +415,7 @@ def ship_warp( url = _url("my/ships/:ship_name/warp") event_params = { "destination_waypoint": dest_waypoint_symbol, - "destination_system": waypoint_slicer(dest_waypoint_symbol), + "destination_system": waypoint_to_system(dest_waypoint_symbol), } self.log_event( "ship_warp", diff --git a/straders_sdk/client_postgres.py b/straders_sdk/client_postgres.py index 8eae2a1..091e84f 100644 --- a/straders_sdk/client_postgres.py +++ b/straders_sdk/client_postgres.py @@ -33,7 +33,7 @@ _upsert_construction_site, select_construction_site_one, ) -from .local_response import LocalSpaceTradersRespose +from .resp_local_resp import LocalSpaceTradersRespose from .models_misc import RouteNode, ConstructionSite from .models_ship import Ship, ShipInventory, ShipNav, ShipModule, ShipMount from .utils import try_execute_select, try_execute_upsert diff --git a/straders_sdk/models_misc.py b/straders_sdk/models_misc.py index 2d2f322..fad2049 100644 --- a/straders_sdk/models_misc.py +++ b/straders_sdk/models_misc.py @@ -2,7 +2,7 @@ from datetime import datetime, timedelta import requests -from .utils import DATE_FORMAT, waypoint_slicer +from .utils import DATE_FORMAT, waypoint_to_system class SymbolClass: @@ -500,7 +500,9 @@ def __init__( ) -> None: self.waypoint_symbol = waypoint_symbol self.connected_waypoints = connected_waypoints - self.connected_systems = [waypoint_slicer(c) for c in self.connected_waypoints] + self.connected_systems = [ + waypoint_to_system(c) for c in self.connected_waypoints + ] @classmethod def from_json(cls, waypoint_symbol, json_data: dict): diff --git a/straders_sdk/models_ship.py b/straders_sdk/models_ship.py index 262e36d..27b0f93 100644 --- a/straders_sdk/models_ship.py +++ b/straders_sdk/models_ship.py @@ -3,7 +3,6 @@ from .models_misc import ShipFrame, ShipModule, ShipMount from .models_misc import ShipReactor, ShipEngine from .models_misc import ShipRequirements, ShipNav -from .client_interface import SpaceTradersInteractive import logging from .utils import parse_timestamp from .constants import SURVEYOR_SYMBOLS, MINING_SYMBOLS, SIPHON_SYMBOLS @@ -49,14 +48,14 @@ def from_json(cls, json_data: dict): return cls(*json_data.values()) -class Ship(SpaceTradersInteractive): +class Ship: name: str role: str faction: str - nav: ShipNav - frame: ShipFrame - reactor: ShipReactor - engine: ShipEngine + nav: "ShipNav" + frame: "ShipFrame" + reactor: "ShipReactor" + engine: "ShipEngine" crew_capacity: int crew_current: int crew_required: int @@ -65,7 +64,7 @@ class Ship(SpaceTradersInteractive): crew_wages: int cargo_capacity: int cargo_units_used: int - cargo_inventory: list[ShipInventory] + cargo_inventory: list["ShipInventory"] # ---- FUEL INFO ---- fuel_capacity: int @@ -78,8 +77,8 @@ class Ship(SpaceTradersInteractive): # ---- REACTOR INFO ---- # todo: modules and mounts - modules: list[ShipModule] - mounts: list[ShipMount] + modules: list["ShipModule"] + mounts: list["ShipMount"] def __init__(self) -> None: pass diff --git a/straders_sdk/pg_pieces/agents.py b/straders_sdk/pg_pieces/agents.py index 1a385b0..dc003a5 100644 --- a/straders_sdk/pg_pieces/agents.py +++ b/straders_sdk/pg_pieces/agents.py @@ -1,5 +1,5 @@ from ..models_misc import Agent -from ..local_response import LocalSpaceTradersRespose +from ..resp_local_resp import LocalSpaceTradersRespose import logging import datetime from ..utils import try_execute_upsert, try_execute_select diff --git a/straders_sdk/pg_pieces/construction_sites.py b/straders_sdk/pg_pieces/construction_sites.py index f45e2f4..eb4df18 100644 --- a/straders_sdk/pg_pieces/construction_sites.py +++ b/straders_sdk/pg_pieces/construction_sites.py @@ -1,5 +1,5 @@ from ..models_misc import ConstructionSite, ConstructionSiteMaterial -from ..local_response import LocalSpaceTradersRespose +from ..resp_local_resp import LocalSpaceTradersRespose import logging import datetime from ..utils import try_execute_upsert, try_execute_select diff --git a/straders_sdk/pg_pieces/contracts.py b/straders_sdk/pg_pieces/contracts.py index 1128867..c1842e6 100644 --- a/straders_sdk/pg_pieces/contracts.py +++ b/straders_sdk/pg_pieces/contracts.py @@ -2,7 +2,7 @@ from ..models_contracts import Contract, ContractDeliverGood import logging from datetime import datetime -from ..local_response import LocalSpaceTradersRespose +from ..resp_local_resp import LocalSpaceTradersRespose from ..utils import try_execute_select, try_execute_upsert diff --git a/straders_sdk/pg_pieces/extractions.py b/straders_sdk/pg_pieces/extractions.py index 14f0218..18c7880 100644 --- a/straders_sdk/pg_pieces/extractions.py +++ b/straders_sdk/pg_pieces/extractions.py @@ -1,5 +1,5 @@ from ..models_misc import Agent -from ..local_response import LocalSpaceTradersRespose +from ..resp_local_resp import LocalSpaceTradersRespose import logging import datetime from ..utils import try_execute_upsert diff --git a/straders_sdk/pg_pieces/jump_gates.py b/straders_sdk/pg_pieces/jump_gates.py index 6bc4557..a1fe216 100644 --- a/straders_sdk/pg_pieces/jump_gates.py +++ b/straders_sdk/pg_pieces/jump_gates.py @@ -1,8 +1,8 @@ from ..models_misc import Waypoint, JumpGate, JumpGateConnection import logging from datetime import datetime -from ..local_response import LocalSpaceTradersRespose -from ..utils import try_execute_select, try_execute_upsert, waypoint_slicer +from ..resp_local_resp import LocalSpaceTradersRespose +from ..utils import try_execute_select, try_execute_upsert, waypoint_to_system def _upsert_jump_gate(jump_gate: JumpGate, connection): @@ -29,9 +29,9 @@ def _upsert_jump_gate(jump_gate: JumpGate, connection): connection_sql, ( jump_gate.waypoint_symbol, - waypoint_slicer(jump_gate.waypoint_symbol), + waypoint_to_system(jump_gate.waypoint_symbol), dest_waypoint, - waypoint_slicer(dest_waypoint), + waypoint_to_system(dest_waypoint), ), connection, ) diff --git a/straders_sdk/pg_pieces/select_ship.py b/straders_sdk/pg_pieces/select_ship.py index dda19f8..9b2fcc5 100644 --- a/straders_sdk/pg_pieces/select_ship.py +++ b/straders_sdk/pg_pieces/select_ship.py @@ -1,4 +1,4 @@ -from ..local_response import LocalSpaceTradersRespose +from ..resp_local_resp import LocalSpaceTradersRespose from ..models_ship import Ship, ShipFrame, ShipNav, ShipInventory from ..models_misc import RouteNode from ..client_interface import SpaceTradersClient diff --git a/straders_sdk/pg_pieces/transactions.py b/straders_sdk/pg_pieces/transactions.py index a588b14..bf2c5a1 100644 --- a/straders_sdk/pg_pieces/transactions.py +++ b/straders_sdk/pg_pieces/transactions.py @@ -1,5 +1,5 @@ from ..models_misc import Agent -from ..local_response import LocalSpaceTradersRespose +from ..resp_local_resp import LocalSpaceTradersRespose import logging import datetime diff --git a/straders_sdk/pg_pieces/upsert_market.py b/straders_sdk/pg_pieces/upsert_market.py index c03c2a9..fcddfa4 100644 --- a/straders_sdk/pg_pieces/upsert_market.py +++ b/straders_sdk/pg_pieces/upsert_market.py @@ -2,13 +2,13 @@ import psycopg2 import logging from datetime import datetime -from ..utils import waypoint_slicer +from ..utils import waypoint_to_system from ..utils import try_execute_select, try_execute_upsert -from ..local_response import LocalSpaceTradersRespose +from ..resp_local_resp import LocalSpaceTradersRespose def _upsert_market(market: Market, connection): - system_symbol = waypoint_slicer(market.symbol) + system_symbol = waypoint_to_system(market.symbol) sql = """INSERT INTO public.market( symbol, system_symbol) VALUES (%s, %s) diff --git a/straders_sdk/pg_pieces/upsert_ship.py b/straders_sdk/pg_pieces/upsert_ship.py index 9cd467a..6f22938 100644 --- a/straders_sdk/pg_pieces/upsert_ship.py +++ b/straders_sdk/pg_pieces/upsert_ship.py @@ -4,7 +4,7 @@ import logging import re import datetime -from ..local_response import LocalSpaceTradersRespose +from ..resp_local_resp import LocalSpaceTradersRespose from ..utils import try_execute_upsert # from psycopg2 import connection diff --git a/straders_sdk/request_consumer.py b/straders_sdk/request_consumer.py index d6b50b2..958437d 100644 --- a/straders_sdk/request_consumer.py +++ b/straders_sdk/request_consumer.py @@ -32,6 +32,7 @@ def __init__(self, auto_start=True) -> None: target=self._consume_until_stopped, daemon=auto_start ) self._session = requests.Session() + self.handlers = {} if auto_start: self.start() pass @@ -44,6 +45,11 @@ def start(self): if not self._consumer_thread.is_alive(): self._consumer_thread.start() + def register_handler(self, handler, identifier): + if not callable(handler): + raise ValueError("Handler must be callable") + self.handlers[identifier] = handler + def _consume_until_stopped(self): """this method should be tied to the _consumer_thread""" base_mod = 120 @@ -65,6 +71,8 @@ def _consume_until_stopped(self): try: # print("Doing the thing") package.response = self._session.send(package.request) + for identifier, handler in self.handlers.items(): + handler(package.response) delay_mod = max(0, delay_mod - 0.5) except Exception as e: diff --git a/straders_sdk/local_response.py b/straders_sdk/resp_local_resp.py similarity index 100% rename from straders_sdk/local_response.py rename to straders_sdk/resp_local_resp.py diff --git a/straders_sdk/responses.py b/straders_sdk/resp_responses.py similarity index 98% rename from straders_sdk/responses.py rename to straders_sdk/resp_responses.py index 19f5ed1..236cd93 100644 --- a/straders_sdk/responses.py +++ b/straders_sdk/resp_responses.py @@ -17,6 +17,7 @@ @runtime_checkable class SpaceTradersResponse(Protocol): data: dict + url: str response_json: dict error: str status_code: int @@ -32,7 +33,7 @@ class RemoteSpaceTradersRespose: def __init__(self, response: requests.Response, priority: int = None): self.data = {} - + self.url = response.url self.error = None self.error_code = None diff --git a/straders_sdk/responses/__init__.py b/straders_sdk/responses/__init__.py new file mode 100644 index 0000000..dc88069 --- /dev/null +++ b/straders_sdk/responses/__init__.py @@ -0,0 +1,2 @@ +from ..resp_local_resp import LocalSpaceTradersRespose +from ..resp_responses import SpaceTradersResponse, RemoteSpaceTradersRespose diff --git a/straders_sdk/utils.py b/straders_sdk/utils.py index 82dfa0a..d636ef5 100644 --- a/straders_sdk/utils.py +++ b/straders_sdk/utils.py @@ -8,7 +8,7 @@ import random import json, base64 import time -from .local_response import LocalSpaceTradersRespose +from .resp_local_resp import LocalSpaceTradersRespose import threading import copy from requests import Session @@ -93,6 +93,9 @@ def rate_limit_check(response: requests.Response): def request_and_validate( method, url, data=None, json=None, headers=None, params=None, priority=6 ): + if isinstance(data, dict): + json = data + data = None if priority == 6: logging.warning("Priority not set in url %s", url) request = requests.Request( @@ -130,6 +133,17 @@ def get_name_from_token(token: str) -> str: return identifier +def get_date_from_token(token: str) -> str: + + header_b64, payload_b64, signature = token.split(".") + + payload = json.loads(base64.urlsafe_b64decode(payload_b64 + "==")) + + reset_date = payload.get("reset_date", None) + + return reset_date + + def get_and_validate( url, params=None, @@ -150,6 +164,7 @@ def post_and_validate( url, data=None, json=None, headers=None, priority=5, session: Session = None ) -> SpaceTradersResponse: "wraps the requests.post function to make it easier to use" + headers = headers or {} headers["Content-Type"] = "application/json" return request_and_validate( "POST", url, data=data, json=json, headers=headers, priority=priority @@ -226,7 +241,7 @@ def sleep_until_ready(ship: "Ship"): sleep(max(ship.seconds_until_cooldown, ship.nav.travel_time_remaining)) -def waypoint_slicer(waypoint_symbol: str) -> str: +def waypoint_to_system(waypoint_symbol: str) -> str: "returns the system symbol from a waypoint symbol" if "-" not in waypoint_symbol: return waypoint_symbol