Skip to content

Commit

Permalink
consolidate responses, rename waypoint slicer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Feb 27, 2024
1 parent e0f7e8f commit a773820
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
"console": "integratedTerminal",
"justMyCode": False
},
{
"name": "Python: main.py",
Expand Down
12 changes: 11 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
6 changes: 3 additions & 3 deletions straders_sdk/client_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/client_json_cache.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions straders_sdk/client_mediator.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions straders_sdk/client_pg_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/client_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions straders_sdk/models_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
17 changes: 8 additions & 9 deletions straders_sdk/models_ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/pg_pieces/agents.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/pg_pieces/construction_sites.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/pg_pieces/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/pg_pieces/extractions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions straders_sdk/pg_pieces/jump_gates.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/pg_pieces/select_ship.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/pg_pieces/transactions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..models_misc import Agent
from ..local_response import LocalSpaceTradersRespose
from ..resp_local_resp import LocalSpaceTradersRespose
import logging
import datetime

Expand Down
6 changes: 3 additions & 3 deletions straders_sdk/pg_pieces/upsert_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion straders_sdk/pg_pieces/upsert_ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions straders_sdk/request_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion straders_sdk/responses.py → straders_sdk/resp_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@runtime_checkable
class SpaceTradersResponse(Protocol):
data: dict
url: str
response_json: dict
error: str
status_code: int
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions straders_sdk/responses/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from ..resp_local_resp import LocalSpaceTradersRespose
from ..resp_responses import SpaceTradersResponse, RemoteSpaceTradersRespose
19 changes: 17 additions & 2 deletions straders_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a773820

Please sign in to comment.