Skip to content

Commit

Permalink
update SDK to use request consumer instead of requests' session limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Nov 15, 2023
1 parent c622f89 commit 5b87a67
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 40 deletions.
142 changes: 114 additions & 28 deletions straders_sdk/client_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from datetime import datetime

logger = logging.getLogger(__name__)
COOLDOWN_OFFSET = -2
MOVEMENT_OFFSET = -2


class SpaceTradersApiClient(SpaceTradersClient):
Expand All @@ -41,17 +43,21 @@ def __init__(
version=None,
session: LimiterSession = None,
connection=None,
request_priority_baseline=5,
) -> None:
self.token = token
self.config = ApiConfig(base_url, version)
self.current_agent = None
self.current_agent_symbol = None
self.session = session
self.priority = request_priority_baseline
pass

def agents_view_one(self, agent_symbol: str) -> "Agent" or SpaceTradersResponse:
url = f"/agents/{agent_symbol}"
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
return Agent.from_json(resp.data)
return resp
Expand All @@ -63,7 +69,9 @@ def set_current_agent(self, agent_symbol: str, token: str = None):

def view_my_self(self) -> "Agent" or SpaceTradersResponse:
url = _url("my/agent")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
self.current_agent = Agent.from_json(resp.data)
self.current_agent_symbol = self.current_agent.symbol
Expand All @@ -73,7 +81,9 @@ def view_my_self(self) -> "Agent" or SpaceTradersResponse:

def view_my_contracts(self) -> list["Contract"] or SpaceTradersResponse:
url = _url("my/contracts")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
return [Contract.from_json(d) for d in resp.data]
return resp
Expand All @@ -84,7 +94,9 @@ def waypoints_view_one(
if waypoint_symbol == "":
raise ValueError("waypoint_symbol cannot be empty")
url = _url(f"systems/{system_symbol}/waypoints/{waypoint_symbol}")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if not resp:
print(resp.error)
return resp
Expand All @@ -107,7 +119,12 @@ def waypoints_view(

url = _url(f"systems/{system_symbol}/waypoints")
resp = get_and_validate_paginated(
url, 20, 50, headers=self._headers(), session=self.session
url,
20,
50,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
new_wayps = {d["symbol"]: Waypoint.from_json(d) for d in resp.data}
Expand All @@ -125,7 +142,9 @@ def register(self, callsign, faction="COSMIC", email=None) -> SpaceTradersRespon
data = {"symbol": callsign, "faction": faction}
if email is not None:
data["email"] = email
resp = post_and_validate(url, data, session=self.session)
resp = post_and_validate(
url, data, session=self.session, priority=self.priority
)
if resp:
self.token = resp.data.get("token")
return resp
Expand All @@ -135,7 +154,9 @@ def ship_orbit(self, ship: Ship):
url = _url(f"my/ships/{ship.name}/orbit")
if ship.nav.status == "IN_ORBIT":
return LocalSpaceTradersRespose(None, 0, None, url=url)
resp = post_and_validate(url, headers=self._headers(), session=self.session)
resp = post_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
self.update(resp.data)
return resp
Expand All @@ -145,7 +166,11 @@ def ship_patch_nav(self, ship: Ship, flight_mode: str):
url = _url(f"my/ships/{ship.name}/nav")
data = {"flightMode": flight_mode}
resp = patch_and_validate(
url, data, headers=self._headers(), session=self.session
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
self.update(resp.data)
Expand All @@ -160,7 +185,11 @@ def ship_move(self, ship: Ship, dest_waypoint_symbol: str):
url = _url(f"my/ships/{ship.name}/navigate")
data = {"waypointSymbol": dest_waypoint_symbol}
resp = post_and_validate(
url, data, headers=self._headers(), vip=True, session=self.session
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority + MOVEMENT_OFFSET,
)
if resp:
self.update(resp.data)
Expand All @@ -171,7 +200,11 @@ def ship_jump(self, ship: Ship, dest_system_symbol: str):
url = _url(f"my/ships/{ship.name}/jump")
data = {"systemSymbol": dest_system_symbol}
resp = post_and_validate(
url, data, headers=self._headers(), vip=True, session=self.session
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority + COOLDOWN_OFFSET,
)
if resp:
self.update(resp.data)
Expand All @@ -180,7 +213,9 @@ def ship_jump(self, ship: Ship, dest_system_symbol: str):
def ship_negotiate(self, ship: "Ship") -> "Contract" or SpaceTradersResponse:
"/my/ships/{shipSymbol}/negotiate/contract"
url = _url(f"my/ships/{ship.name}/negotiate/contract")
resp = post_and_validate(url, headers=self._headers(), session=self.session)
resp = post_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
resp = Contract.from_json(resp.data.get("contract"))
return resp
Expand Down Expand Up @@ -218,7 +253,11 @@ def ship_extract(self, ship: Ship, survey: Survey = None) -> SpaceTradersRespons
data = survey.to_json() if survey is not None else None

resp = post_and_validate(
url, json=data, headers=self._headers(), vip=True, session=self.session
url,
json=data,
headers=self._headers(),
session=self.session,
priority=self.priority + COOLDOWN_OFFSET,
)
if resp:
self.update(resp.data)
Expand Down Expand Up @@ -317,7 +356,10 @@ def ship_survey(self, ship: Ship) -> list[Survey] or SpaceTradersResponse:
return LocalSpaceTradersRespose("Ship still on cooldown", 0, 4000)
url = _url(f"my/ships/{ship.name}/survey")
resp = post_and_validate(
url, headers=self._headers(), vip=True, session=self.session
url,
headers=self._headers(),
session=self.session,
priority=self.priority + COOLDOWN_OFFSET,
)

self.update(resp.data)
Expand All @@ -337,7 +379,11 @@ def ship_transfer_cargo(
"shipSymbol": target_ship_name,
}
resp = post_and_validate(
url, data, headers=self._headers(), session=self.session
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
self.update(resp.data)

Expand All @@ -349,7 +395,11 @@ def ship_install_mount(self, ship: Ship, mount_symbol: str) -> SpaceTradersRespo
url = _url(f"my/ships/{ship.name}/mounts/install")
data = {"symbol": mount_symbol}
resp = post_and_validate(
url, data, headers=self._headers(), session=self.session
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
self.update(resp.data)
Expand All @@ -364,7 +414,11 @@ def ship_remove_mount(self, ship: Ship, mount_symbol: str) -> SpaceTradersRespon
url = _url(f"my/ships/{ship.name}/mounts/remove")
data = {"symbol": mount_symbol}
resp = post_and_validate(
url, data, headers=self._headers(), session=self.session
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
self.update(resp.data)
Expand All @@ -378,7 +432,9 @@ def ship_jettison_cargo(
) -> SpaceTradersResponse:
url = _url(f"my/ships/{ship.name}/jettison")
data = {"symbol": trade_symbol, "units": quantity}
resp = post_and_validate(url, data, headers=self._headers())
resp = post_and_validate(
url, data, headers=self._headers(), priority=self.priority
)
if resp:
ship.update(resp.data)
return resp
Expand Down Expand Up @@ -420,7 +476,11 @@ def systems_view_twenty(
) -> list["System"] or SpaceTradersResponse:
url = _url("systems")
resp = get_and_validate_page(
url, page_number, headers=self._headers(), session=self.session
url,
page_number,
headers=self._headers(),
session=self.session,
priority=self.priority,
)

if resp:
Expand All @@ -435,22 +495,27 @@ def systems_view_all(self) -> list[System] or SpaceTradersResponse:
page_limit=999,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
resp = [System.from_json(d) for d in resp.data]
return resp

def systems_view_one(self, system_symbol: str) -> System or SpaceTradersResponse:
url = _url(f"systems/{system_symbol}")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
return System.from_json(resp.data)
return resp

def system_market(self, wp: Waypoint) -> Market:
# /systems/{systemSymbol}/waypoints/{waypointSymbol}/market
url = _url(f"systems/{wp.system_symbol}/waypoints/{wp.symbol}/market")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
resp = Market.from_json(resp.data)
return resp
Expand All @@ -466,7 +531,9 @@ def system_shipyard(self, wp: Waypoint) -> Shipyard or SpaceTradersResponse:
"""

url = _url(f"systems/{wp.system_symbol}/waypoints/{wp.symbol}/shipyard")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)

if resp:
return Shipyard.from_json(resp.data)
Expand All @@ -476,7 +543,9 @@ def system_shipyard(self, wp: Waypoint) -> Shipyard or SpaceTradersResponse:
def system_jumpgate(self, wp: Waypoint) -> JumpGate or SpaceTradersResponse:
"""/systems/{systemSymbol}/waypoints/{waypointSymbol}/jump-gate"""
url = _url(f"systems/{wp.system_symbol}/waypoints/{wp.symbol}/jump-gate")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
gate = JumpGate.from_json(wp.symbol, resp.data)
gate.waypoint_symbol = wp.symbol
Expand All @@ -487,7 +556,9 @@ def ship_cooldown(self, ship: "Ship") -> SpaceTradersResponse:
"""/my/ships/{shipSymbol}/cooldown"""
# /my/ships/{shipSymbol}/cooldown
url = _url(f"my/ships/{ship.name}/cooldown")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp and "expiration" in resp.data:
ship.update({"cooldown": resp.data})
else:
Expand All @@ -499,7 +570,12 @@ def ships_view(self) -> dict[str:"Ship"] or SpaceTradersResponse:
"""/my/ships"""
url = _url("my/ships")
resp = get_and_validate_paginated(
url, 20, 10, headers=self._headers(), session=self.session
url,
20,
10,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
resp = {ship["symbol"]: Ship.from_json(ship) for ship in resp.data}
Expand All @@ -509,7 +585,9 @@ def ships_view(self) -> dict[str:"Ship"] or SpaceTradersResponse:
def ships_view_one(self, symbol: str) -> "Ship" or SpaceTradersResponse:
"/my/ships/{shipSymbol}"
url = _url(f"my/ships/{symbol}")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
if resp:
return Ship.from_json(resp.data)
return resp
Expand All @@ -520,7 +598,11 @@ def ships_purchase(
url = _url("my/ships")
data = {"shipType": ship_type, "waypointSymbol": shipyard_waypoint}
resp = post_and_validate(
url, data, headers=self._headers(), session=self.session
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if not resp:
return resp
Expand All @@ -534,7 +616,9 @@ def contracts_deliver(
url = _url(f"/my/contracts/{contract.id}/deliver")
data = {"shipSymbol": ship.name, "tradeSymbol": trade_symbol, "units": units}
headers = self._headers()
resp = post_and_validate(url, data, headers=headers, session=self.session)
resp = post_and_validate(
url, data, headers=headers, session=self.session, priority=self.priority
)
if not resp:
print(f"failed to deliver to contract {resp.status_code}, {resp.error}")
return resp
Expand All @@ -543,7 +627,9 @@ def contracts_deliver(
def contracts_fulfill(self, contract: Contract):
url = _url(f"/my/contracts/{contract.id}/fulfill")
headers = self._headers()
resp = post_and_validate(url, headers=headers, session=self.session)
resp = post_and_validate(
url, headers=headers, session=self.session, priority=self.priority
)
if not resp:
print(f"failed to fulfill contract {resp.status_code}, {resp.error}")
return resp
Expand Down
Loading

0 comments on commit 5b87a67

Please sign in to comment.