Remove purchase product code and add details to exceptions
This commit is contained in:
@ -1,28 +1,35 @@
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel
|
||||
from enum import Enum
|
||||
from typing import List, Optional
|
||||
|
||||
import requests
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class BaseError(Exception):
|
||||
...
|
||||
detail: str = "BaseError"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.detail
|
||||
|
||||
|
||||
class PositionError(BaseError):
|
||||
...
|
||||
detail = "Invalid position"
|
||||
|
||||
|
||||
class PlayerInactiveError(BaseError):
|
||||
...
|
||||
detail = "Player inactive"
|
||||
|
||||
|
||||
class PlayerNotFoundError(BaseError):
|
||||
...
|
||||
detail = "Player not found"
|
||||
|
||||
|
||||
class GameLockedError(BaseError):
|
||||
detail = "Game locked. Product selection in progress."
|
||||
|
||||
|
||||
class ValidationError(BaseError):
|
||||
...
|
||||
detail = "Validation error"
|
||||
|
||||
|
||||
class Direction(str, Enum):
|
||||
@ -63,12 +70,6 @@ class Player(BaseModel):
|
||||
state: PlayerState
|
||||
|
||||
|
||||
class Product(BaseModel):
|
||||
name: str
|
||||
id: str
|
||||
description: Optional[str] = None
|
||||
|
||||
|
||||
class PingResponse(BaseModel):
|
||||
message: str
|
||||
|
||||
@ -88,10 +89,6 @@ class GameInfoResponse(BaseModel):
|
||||
destination: Destination
|
||||
|
||||
|
||||
class ProductListResponse(BaseModel):
|
||||
products: List[Product]
|
||||
|
||||
|
||||
class FairHopper:
|
||||
def __init__(self, host: str) -> None:
|
||||
self.host = host
|
||||
@ -160,42 +157,9 @@ class FairHopper:
|
||||
raise PositionError()
|
||||
elif r.status_code == 422:
|
||||
raise ValidationError()
|
||||
elif r.status_code == 423:
|
||||
raise GameLockedError()
|
||||
else:
|
||||
r.raise_for_status()
|
||||
|
||||
return PlayerInfoResponse(**r.json())
|
||||
|
||||
def get_products(self) -> List[Product]:
|
||||
r = requests.get(self.format_url("/products"))
|
||||
response_data = ProductListResponse(**r.json())
|
||||
return response_data.products
|
||||
|
||||
def get_product(self, product_id: str) -> Product:
|
||||
r = requests.get(self.format_url(f"/products/{product_id}"))
|
||||
|
||||
if r.status_code == 422:
|
||||
raise ValidationError()
|
||||
else:
|
||||
r.raise_for_status()
|
||||
|
||||
return Product(**r.json())
|
||||
|
||||
def purchase_product(self, player_id: str, product_id: str) -> Product:
|
||||
url = self.format_url(f"/player/{player_id}/product/purchase")
|
||||
payload = {
|
||||
"product_id": product_id,
|
||||
}
|
||||
r = requests.post(url, json=payload)
|
||||
|
||||
if r.status_code == 403:
|
||||
raise PlayerInactiveError()
|
||||
elif r.status_code == 404:
|
||||
raise PlayerNotFoundError()
|
||||
elif r.status_code == 409:
|
||||
raise PositionError()
|
||||
elif r.status_code == 422:
|
||||
raise ValidationError()
|
||||
else:
|
||||
r.raise_for_status()
|
||||
|
||||
return Product(**r.json())
|
||||
|
||||
Reference in New Issue
Block a user