Skip to content

Commit

Permalink
more exchange and abstract classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jplacht committed Oct 30, 2023
1 parent 28e66ad commit b47c9da
Show file tree
Hide file tree
Showing 22 changed files with 546 additions and 98 deletions.
5 changes: 1 addition & 4 deletions pyfio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
from .fio_adapter import *
from .exceptions import *
from .urls import *
from .validators import *

# models
from .models.material_models import *
from .models.exchange_models import *

# endpoints
from .endpoints.material import *
from .endpoints.exchange import *
6 changes: 6 additions & 0 deletions pyfio/endpoints/abstracts/abstract_building.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AbstractBuilding:
def get(self, building_ticker: str):
raise NotImplementedError()

def allbuildings(self):
raise NotImplementedError()
15 changes: 15 additions & 0 deletions pyfio/endpoints/abstracts/abstract_exchange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AbstractExchange:
def get(self, exchange_ticker: str):
raise NotImplementedError()

def get_all(self):
raise NotImplementedError()

def get_full(self):
raise NotImplementedError()

def get_orders(self, company_code: str):
raise NotImplementedError()

def get_orders_exchange(self, company_code: str, exchange_code: str):
raise NotImplementedError()
9 changes: 9 additions & 0 deletions pyfio/endpoints/abstracts/abstract_material.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AbstractMaterial:
def get(self, material_ticker: str):
raise NotImplementedError()

def all(self):
raise NotImplementedError()

def category(self, category_name: str):
raise NotImplementedError()
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pyfio.endpoints.abstracts.abstract_building import AbstractBuilding
from pyfio.fio_adapter import FIOAdapter


class Building:
class Building(AbstractBuilding):
def __init__(self, adapter: FIOAdapter) -> None:
self._adapter: FIOAdapter = adapter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from pyfio.endpoints.abstracts.abstract_exchange import AbstractExchange
from pyfio.fio_adapter import FIOAdapter
from pyfio.validators import (
validate_ticker,
validate_exchange_code,
validate_company_code,
)
from pyfio.models.exchange_models import ExchangeTicker, OrderList
from pyfio.models.exchange_models import (
ExchangeTickerFull,
OrderList,
ExchangeTickerList,
ExchangeTickerFullList,
)
from pyfio.exceptions import (
ExchangeTickerInvalid,
MaterialTickerInvalid,
ExchangeTickerNotFound,
CompanyCodeInvalid,
)


class Exchange:
class Exchange(AbstractExchange):
def __init__(self, adapter: FIOAdapter) -> None:
self._adapter: FIOAdapter = adapter

Expand Down Expand Up @@ -56,7 +61,7 @@ def _validate_exchangeticker(self, exchange_ticker: str) -> None:
validate_exchange_code(exchange_code=splitted[1])

# /exchange/{ExchangeTicker}
def get(self, exchange_ticker: str) -> ExchangeTicker:
def get(self, exchange_ticker: str) -> ExchangeTickerFull:
"""Gets a single exchange ticker from FIO
Args:
Expand All @@ -78,19 +83,35 @@ def get(self, exchange_ticker: str) -> ExchangeTicker:
)

if status == 200:
return ExchangeTicker.model_validate(data)
return ExchangeTickerFull.model_validate(data)
elif status == 204:
raise ExchangeTickerNotFound("Exchangeticker not found")

# /exchange/all
def get_all(self) -> ExchangeTickerList:
"""Gets all simple exchange ticker from FIO
# /exchange/full
Returns:
ExchangeTickerList: Exchange ticker
"""
(_, data) = self._adapter.get(
endpoint=self._adapter.urls.exchange_get_all_url()
)

# /exchange/station
return ExchangeTickerList.model_validate(data)

# /exchange/cxpc/{ExchangeTicker}
# /exchange/full
def get_full(self) -> ExchangeTickerFullList:
"""Gets a complete list of all exchange information from FIO
Returns:
ExchangeTickerFullList: Exchange ticker full
"""
(_, data) = self._adapter.get(
endpoint=self._adapter.urls.exchange_get_full_url()
)

# /exchange/cxpc/{ExchangeTicker}/{TimeStamp}
return ExchangeTickerFullList.model_validate(data)

# /exchange/orders/{CompanyCode}
def get_orders(self, company_code: str) -> OrderList:
Expand All @@ -114,7 +135,7 @@ def get_orders(self, company_code: str) -> OrderList:
return OrderList.model_validate(data)

# /exchange/orders/{CompanyCode}/{ExchangeCode}
def get_order_exchange(self, company_code: str, exchange_code: str) -> OrderList:
def get_orders_exchange(self, company_code: str, exchange_code: str) -> OrderList:
"""Gets a companies order data for a specific exchange from FIO
Args:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from pyfio.endpoints.abstracts.abstract_material import AbstractMaterial
from pyfio.fio_adapter import FIOAdapter
from pyfio.validators import validate_ticker
from pyfio.models.material_models import MaterialTicker, MaterialTickerList
from pyfio.exceptions import MaterialTickerNotFound, MaterialCategoryNotFound


class Material:
class Material(AbstractMaterial):
def __init__(self, adapter: FIOAdapter) -> None:
self._adapter: FIOAdapter = adapter

Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions pyfio/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Not Implemented


class EndpointNotImplemented(Exception):
pass


# Material


Expand Down
25 changes: 18 additions & 7 deletions pyfio/fio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from pyfio.fio_adapter import FIOAdapter
from pyfio.endpoints import building, exchange, localmarket, material, planet, recipe
from pyfio.exceptions import EndpointNotImplemented

from pyfio.endpoints.endpoints_v1 import building as building_v1
from pyfio.endpoints.endpoints_v1 import exchange as exchange_v1
from pyfio.endpoints.endpoints_v1 import localmarket as localmarket_v1
from pyfio.endpoints.endpoints_v1 import material as material_v1
from pyfio.endpoints.endpoints_v1 import planet as planet_v1
from pyfio.endpoints.endpoints_v1 import recipe as recipe_v1


class FIO:
Expand All @@ -12,9 +19,13 @@ def __init__(
) -> None:
self._adapter = FIOAdapter(api_key, version, base_url, ssl_verify)

self.Building = building.Building(self._adapter)
self.Exchange = exchange.Exchange(self._adapter)
self.LocalMarket = localmarket.LocalMarket(self._adapter)
self.Material = material.Material(self._adapter)
self.Planet = planet.Planet(self._adapter)
self.Recipe = recipe.Recipe(self._adapter)
if version == "1.0.0":
self.Building = building_v1.Building(self._adapter)
self.Exchange = exchange_v1.Exchange(self._adapter)
self.LocalMarket = localmarket_v1.LocalMarket(self._adapter)
self.Material = material_v1.Material(self._adapter)
self.Planet = planet_v1.Planet(self._adapter)
self.Recipe = recipe_v1.Recipe(self._adapter)

else:
raise EndpointNotImplemented()
74 changes: 48 additions & 26 deletions pyfio/models/exchange_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,74 @@
from datetime import datetime
from pydantic import BaseModel, RootModel, Field, NaiveDatetime

# Exchange


class ExchangeOrder(BaseModel):
OrderId: str = Field(min_length=32)
CompanyId: str = Field(min_length=32)
CompanyName: str
CompanyCode: str
CompanyName: Optional[str]
CompanyCode: Optional[str]
ItemCount: Optional[int] # Market Maker ItemCount = null
ItemCost: float


class ExchangeTicker(BaseModel):
MaterialTicker: str = Field(max_length=3)
ExchangeCode: str = Field(min_length=3)
MMBuy: Optional[float]
MMSell: Optional[float]
PriceAverage: Optional[float]
Ask: Optional[float]
AskCount: Optional[int]
Bid: Optional[float]
BidCount: Optional[int]
Supply: Optional[int]
Demand: Optional[int]


class ExchangeTickerList(RootModel):
root: List[ExchangeTicker]

def __iter__(self):
return iter(self.root)


class ExchangeTickerFull(ExchangeTicker):
BuyingOrders: Optional[List[ExchangeOrder]]
SellingOrders: Optional[List[ExchangeOrder]]

ExchangeName: str
CXDataModelId: str = Field(min_length=32)
MaterialName: str
MaterialTicker: str = Field(max_length=3)
MaterialId: str = Field(min_length=32)
ExchangeName: str
ExchangeCode: str = Field(min_length=3)
Currency: str = Field(min_length=2)
Price: float
PriceTimeEpochMs: datetime
High: float
AllTimeHigh: float
Low: float
AllTimeLow: float
Ask: float
AskCount: int
Bid: float
BidCount: int
Supply: int
Demand: int
Traded: int
VolumeAmount: float
PriceAverage: float
NarrowPriceBandLow: float
NarrowPriceBandHigh: float
WidePriceBandLow: float
WidePriceBandHigh: float
MMBuy: float
MMSell: float
Price: Optional[float]
PriceTimeEpochMs: Optional[datetime]
High: Optional[float]
AllTimeHigh: Optional[float]
Low: Optional[float]
AllTimeLow: Optional[float]
Traded: Optional[int]
VolumeAmount: Optional[float]
NarrowPriceBandLow: Optional[float]
NarrowPriceBandHigh: Optional[float]
WidePriceBandLow: Optional[float]
WidePriceBandHigh: Optional[float]
UserNameSubmitted: str
Timestamp: NaiveDatetime


class ExchangeTickerFullList(RootModel):
root: List[ExchangeTickerFull]

def __iter__(self):
return iter(self.root)


# Order


class OrderDefinition(BaseModel):
Count: int
Cost: float
Expand Down
8 changes: 8 additions & 0 deletions pyfio/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def __init__(self, base_url: str) -> None:
# exchange
self.exchange_base = "/exchange"
self.exchange_orders = "/orders"
self.exchange_all = "/all"
self.exchange_full = "/full"

# Material
def material_url(self) -> str:
Expand All @@ -30,6 +32,12 @@ def exchange_url(self) -> str:
def exchange_get_url(self, exchange_ticker: str) -> str:
return self.exchange_url() + "/" + exchange_ticker

def exchange_get_all_url(self) -> str:
return self.exchange_url() + self.exchange_all

def exchange_get_full_url(self) -> str:
return self.exchange_url() + self.exchange_full

def exchange_get_orders_companycode(self, company_code: str) -> str:
return self.exchange_url() + self.exchange_orders + "/" + company_code

Expand Down
5 changes: 4 additions & 1 deletion pyfio/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ def validate_exchange_code(exchange_code: str) -> None:


def validate_company_code(company_code: str) -> None:
if 1 < len(company_code) < 4 or company_code == "":
if company_code == "" or company_code is None:
raise CompanyCodeInvalid("Invalid company code. Can't be empty or None type")

if len(company_code) > 4:
raise CompanyCodeInvalid("Invalid company code. Must be 1 to 4 characters")
Loading

0 comments on commit b47c9da

Please sign in to comment.