Skip to content

Commit

Permalink
planet endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jplacht committed Oct 31, 2023
1 parent 6ebcaf4 commit 91bf205
Show file tree
Hide file tree
Showing 17 changed files with 1,047 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ We welcome contributions of all types! In order to set up **pyfio** locally plea

# Tests

**pyfio** uses pytest, requests_mock and pytest-cov to run tests, mock calls towards FIO endpoints and generate the code coverage report.
**pyfio** uses `pytest`, `requests_mock` and `pytest-cov` to run tests, mock calls towards FIO endpoints and generate the code coverage report and use `black` as formatter.

Run tests:
```shell
Expand Down
1 change: 1 addition & 0 deletions pyfio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .models.exchange_models import *
from .models.recipe_models import *
from .models.building_models import *
from .models.planet_models import *
2 changes: 1 addition & 1 deletion pyfio/endpoints/abstracts/abstract_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class AbstractExchange:
def get(self, exchange_ticker: str):
raise NotImplementedError()

def get_all(self):
def all(self):
raise NotImplementedError()

def get_full(self):
Expand Down
24 changes: 24 additions & 0 deletions pyfio/endpoints/abstracts/abstract_localmarket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class AbstractLocalMarket:
# /localmarket/{LocalMarketId}
def get(self, localmarket_id: str):
raise NotImplementedError()

# /localmarket/planet/{Planet}
def planet(self, planet: str):
raise NotImplementedError()

# /localmarket/planet/{Planet}/{Type}
def planet_type(self, planet: str, type: str):
raise NotImplementedError()

# /localmarket/shipping/source/{SourcePlanet}
def shipping_from(self, planet: str):
raise NotImplementedError()

# /localmarket/shipping/destination/{DestinationPlanet}
def shipping_to(self, planet: str):
raise NotImplementedError()

# /localmarket/company/{Company}
def company(self, companycode: str):
raise NotImplementedError()
12 changes: 12 additions & 0 deletions pyfio/endpoints/abstracts/abstract_planet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AbstractPlanet:
def get(self, planet: str):
raise NotImplementedError()

def all(self):
raise NotImplementedError()

def full(self):
raise NotImplementedError()

def sites(self, planet: str):
raise NotImplementedError()
4 changes: 2 additions & 2 deletions pyfio/endpoints/endpoints_v1/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get(self, exchange_ticker: str) -> ExchangeTickerFull:
raise ExchangeTickerNotFound("Exchangeticker not found")

# /exchange/all
def get_all(self) -> ExchangeTickerList:
def all(self) -> ExchangeTickerList:
"""Gets all simple exchange ticker from FIO
Returns:
Expand All @@ -101,7 +101,7 @@ def get_all(self) -> ExchangeTickerList:
return ExchangeTickerList.model_validate(data)

# /exchange/full
def get_full(self) -> ExchangeTickerFullList:
def full(self) -> ExchangeTickerFullList:
"""Gets a complete list of all exchange information from FIO
Returns:
Expand Down
3 changes: 2 additions & 1 deletion pyfio/endpoints/endpoints_v1/localmarket.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pyfio.endpoints.abstracts.abstract_localmarket import AbstractLocalMarket
from pyfio.fio_adapter import FIOAdapter


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

Expand Down
74 changes: 71 additions & 3 deletions pyfio/endpoints/endpoints_v1/planet.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,80 @@
from pyfio.endpoints.abstracts.abstract_planet import AbstractPlanet
from pyfio.exceptions import PlanetNotFound
from pyfio.fio_adapter import FIOAdapter
from pyfio.models.planet_models import (
PlanetFull,
PlanetFullList,
PlanetList,
PlanetSiteList,
)


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

# /planet/{Planet}
def get(self, planet: str) -> PlanetFull:
"""Gets full planet data from FIO
Args:
planet (str): PlanetId, PlanetNaturalId or PlanetName
Raises:
PlanetNotFound: Planet not found
Returns:
PlanetFull: Full planet information
"""
(status, data) = self._adapter.get(
endpoint=self._adapter.urls.planet_get_url(planet=planet)
)

if status == 200:
return PlanetFull.model_validate(data)
elif status == 204:
raise PlanetNotFound("Planet not found")

# /planet/allplanets
def all(self) -> PlanetList:
"""Gets a list of all Planets with minimal information from FIO
Returns:
PlanetList: List of Planets as List[Planet]
"""
(_, data) = self._adapter.get(endpoint=self._adapter.urls.planet_all_url())

return PlanetList.model_validate(data)

# /planet/allplanets/full
# /planet/{Planet}
def full(self) -> PlanetFullList:
"""Gets a list of all planets from FIO with full planet information
Returns:
PlanetFullList: List of Planets with full information as List[PlanetFull]
"""
(_, data) = self._adapter.get(endpoint=self._adapter.urls.planet_full_url())

return PlanetFullList.model_validate(data)

# /planet/sites/{Planet}
# /planet/sitescount/{Planet}
def sites(self, planet: str) -> PlanetSiteList:
"""Gets a list of sites on the planet from FIO
Args:
planet (str): PlanetId, PlanetNaturalId or PlanetName
Raises:
PlanetNotFound: Planet not found
Returns:
PlanetSiteList: List of Planet sites as List[PlanetSite]
"""
(status, data) = self._adapter.get(
endpoint=self._adapter.urls.planet_sites_url(planet=planet)
)

if status == 200:
return PlanetSiteList.model_validate(data)
elif status == 204:
raise PlanetNotFound("Planet not found")
7 changes: 7 additions & 0 deletions pyfio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ class CompanyCodeInvalid(Exception):

class BuildingTickerNotFound(Exception):
pass


# Planet


class PlanetNotFound(Exception):
pass
132 changes: 132 additions & 0 deletions pyfio/models/planet_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from typing import List, Optional
from pydantic import BaseModel, RootModel, Field, NaiveDatetime
from datetime import datetime


class PlanetResource(BaseModel):
MaterialId: str = Field(min_length=32)
ResourceType: str
Factor: float


class BuildingRequirement(BaseModel):
MaterialName: str
MaterialId: str = Field(min_length=32)
MaterialTicker: str = Field(max_length=3)
MaterialCategory: str = Field(min_length=32)
MaterialAmount: int
MaterialWeight: float
MaterialVolume: float


class ProductionFee(BaseModel):
Category: str
WorkforceLevel: str
FeeAmount: int
FeeCurrency: Optional[str]


class COGCProgram(BaseModel):
ProgramType: Optional[str]
StartEpochMs: datetime
EndEpochMs: datetime


class COGCVote(BaseModel):
CompanyName: str
CompanyCode: str
Influence: float
VoteType: str
VoteTimeEpochMs: datetime


class Planet(BaseModel):
PlanetNaturalId: str
PlanetName: str


class PlanetList(RootModel):
root: List[Planet]

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


class PlanetFull(Planet):
Resources: List[PlanetResource]
BuildRequirements: List[BuildingRequirement]
ProductionFees: List[ProductionFee]
COGCPrograms: List[COGCProgram]
COGCVotes: List[COGCVote]

PlanetId: str = Field(min_length=32)
Namer: Optional[str]
NamingDataEpochMs: int
Nameable: bool
SystemId: str = Field(min_length=32)
Gravity: float
MagneticField: float
Mass: float
MassEarth: float
OrbitSemiMajorAxis: int
OrbitEccentricity: float
OrbitInclination: float
OrbitRightAscension: int
OrbitPeriapsis: int
OrbitIndex: int
Pressure: float
Radiation: float
Radius: float
Sunlight: float
Surface: bool
Temperature: float
Fertility: float
HasLocalMarket: bool
HasChamberOfCommerce: bool
HasWarehouse: bool
HasAdministrationCenter: bool
HasShipyard: bool
FactionCode: Optional[str]
FactionName: Optional[str]
GovernorId: Optional[str] = Field(min_length=32)
GovernorUserName: Optional[str]
GovernorCorporationId: Optional[str] = Field(min_length=32)
GovernorCorporationName: Optional[str]
GovernorCorporationCode: Optional[str]
CurrencyName: Optional[str]
CurrencyCode: Optional[str]
CollectorId: Optional[str] = Field(min_length=32)
CollectorName: Optional[str]
CollectorCode: Optional[str]
BaseLocalMarketFee: int
LocalMarketFeeFactor: int
WarehouseFee: int
PopulationId: Optional[str] = Field(min_length=32)
COGCProgramStatus: Optional[str]
PlanetTier: int
UserNameSubmitted: str
Timestamp: NaiveDatetime


class PlanetFullList(RootModel):
root: List[PlanetFull]

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


class PlanetSite(BaseModel):
PlanetId: str = Field(min_length=32)
OwnerId: str = Field(min_length=32)
OwnerName: str
OwnerCode: Optional[str]
PlotNumber: int
PlotId: str = Field(min_length=32)
SiteId: str


class PlanetSiteList(RootModel):
root: List[PlanetSite]

def __iter__(self):
return iter(self.root)
23 changes: 23 additions & 0 deletions pyfio/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def __init__(self, base_url: str) -> None:
self.recipe_base = "/recipes"
self.recipe_all = "/allrecipes"

# planet
self.planet_base = "/planet"
self.planet_all = "/allplanets"
self.planet_full = "/allplanets/full"
self.planet_sites = "/sites"

# Material
def material_url(self) -> str:
return self.base_url + self.material_base
Expand Down Expand Up @@ -80,3 +86,20 @@ def recipe_get_url(self, material_ticker: str) -> str:

def recipe_get_all_url(self) -> str:
return self.recipe_url() + self.recipe_all

# Planet

def planet_url(self) -> str:
return self.base_url + self.planet_base

def planet_get_url(self, planet: str) -> str:
return self.planet_url() + "/" + planet

def planet_all_url(self) -> str:
return self.planet_url() + self.planet_all

def planet_full_url(self) -> str:
return self.planet_url() + self.planet_full

def planet_sites_url(self, planet: str) -> str:
return self.planet_url() + self.planet_sites + "/" + planet
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -247,26 +247,26 @@ def test_exchange_get_notfound(requests_mock, ftx_fio: FIO) -> None:
ftx_fio.Exchange.get("AAR.AI1")


def test_exchange_get_all(
def test_exchange_all(
requests_mock, exchangeticker_1, exchangeticker_2, ftx_fio: FIO
) -> None:
requests_mock.get(
ftx_fio._adapter.urls.exchange_get_all_url(),
status_code=200,
json=[exchangeticker_1, exchangeticker_2],
)
data = ftx_fio.Exchange.get_all()
data = ftx_fio.Exchange.all()

assert type(data) == ExchangeTickerList


def test_exchange_get_full(requests_mock, exchangeticker_full, ftx_fio: FIO) -> None:
def test_exchange_full(requests_mock, exchangeticker_full, ftx_fio: FIO) -> None:
requests_mock.get(
ftx_fio._adapter.urls.exchange_get_full_url(),
status_code=200,
json=[exchangeticker_full, exchangeticker_full],
)
data = ftx_fio.Exchange.get_full()
data = ftx_fio.Exchange.full()

assert type(data) == ExchangeTickerFullList

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def test_material_single(requests_mock, material_1, ftx_fio: FIO) -> None:
json=material_1,
)
data = ftx_fio.Material.get("DW")
print(data)

assert type(data) == MaterialTicker
assert data.Ticker == "DW"
Expand Down
Loading

0 comments on commit 91bf205

Please sign in to comment.