Compare commits

..

2 Commits

Author SHA1 Message Date
6a64709d17 Product methods for python SDK 2023-04-21 23:39:20 +02:00
270f742c95 Remove port parameter 2023-04-21 17:56:35 +02:00
4 changed files with 53 additions and 24 deletions

View File

@ -1,12 +1,11 @@
const sdk = require("./fh_sdk"); const sdk = require("./fh_sdk");
const FAIRHOPPER_HOST = "http://127.0.0.1"; const FAIRHOPPER_HOST = "https://api.fairhopper.mjerenja.com";
const FAIRHOPPER_PORT = 8010;
async function main() { async function main() {
const fh = new sdk.FairHopper(FAIRHOPPER_HOST, FAIRHOPPER_PORT); const fh = new sdk.FairHopper(FAIRHOPPER_HOST);
console.log(`Pinging FairHopper server on ${FAIRHOPPER_HOST}:${FAIRHOPPER_PORT}`); console.log(`Pinging FairHopper server on ${FAIRHOPPER_HOST}`);
const pingResult = await fh.ping(); const pingResult = await fh.ping();
console.log("Ping result:", pingResult); console.log("Ping result:", pingResult);
console.log(); console.log();

View File

@ -36,9 +36,8 @@ const Direction = {
}; };
class FairHopper { class FairHopper {
constructor(host, port) { constructor(host) {
this.host = host; this.host = host;
this.port = port;
this.defaultHeaders = { this.defaultHeaders = {
Accept: "application/json", Accept: "application/json",
@ -47,7 +46,7 @@ class FairHopper {
} }
formatUrl(path) { formatUrl(path) {
return `${this.host}:${this.port}${path}`; return `${this.host}${path}`;
} }
async ping() { async ping() {

View File

@ -1,13 +1,12 @@
from fh_sdk import Direction, FairHopper, PositionError from fh_sdk import Direction, FairHopper, PositionError
FAIRHOPPER_HOST = "http://127.0.0.1" FAIRHOPPER_HOST = "https://api.fairhopper.mjerenja.com"
FAIRHOPPER_PORT = 8010
def main() -> None: def main() -> None:
fh = FairHopper(FAIRHOPPER_HOST, FAIRHOPPER_PORT) fh = FairHopper(FAIRHOPPER_HOST)
print(f"Pinging FairHopper server on {FAIRHOPPER_HOST}:{FAIRHOPPER_PORT}") print(f"Pinging FairHopper server on {FAIRHOPPER_HOST}")
res = fh.ping() res = fh.ping()
print("Ping result:", res) print("Ping result:", res)
print() print()

View File

@ -1,3 +1,4 @@
from typing import Optional
from pydantic import BaseModel from pydantic import BaseModel
from enum import Enum from enum import Enum
@ -27,6 +28,13 @@ class Direction(str, Enum):
DOWN = "down" DOWN = "down"
class PlayerState(str, Enum):
CREATED = "CREATED"
MOVING = "MOVING"
ON_DESTINATION = "ON_DESTINATION"
INACTIVE = "INACTIVE"
class Board(BaseModel): class Board(BaseModel):
width: int width: int
height: int height: int
@ -48,6 +56,13 @@ class Player(BaseModel):
position: Position position: Position
move_count: int move_count: int
move_attempt_count: int move_attempt_count: int
state: PlayerState
class Product(BaseModel):
name: str
id: str
description: Optional[str] = None
class PingResponse(BaseModel): class PingResponse(BaseModel):
@ -69,13 +84,16 @@ class GameInfoResponse(BaseModel):
destination: Destination destination: Destination
class ProductListResponse(BaseModel):
products: list[Product]
class FairHopper: class FairHopper:
def __init__(self, host, port) -> None: def __init__(self, host: str) -> None:
self.host = host self.host = host
self.port = port
def format_url(self, path: str) -> str: def format_url(self, path: str) -> str:
return f"{self.host}:{self.port}{path}" return f"{self.host}{path}"
def ping(self) -> PingResponse: def ping(self) -> PingResponse:
r = requests.get(self.format_url("/ping")) r = requests.get(self.format_url("/ping"))
@ -107,20 +125,20 @@ class FairHopper:
return PlayerInfoResponse(**r.json()) return PlayerInfoResponse(**r.json())
def move_left(self, id: str) -> PlayerInfoResponse: def move_left(self, player_id: str) -> PlayerInfoResponse:
return self.move(id, Direction.LEFT) return self.move(player_id, Direction.LEFT)
def move_right(self, id: str) -> PlayerInfoResponse: def move_right(self, player_id: str) -> PlayerInfoResponse:
return self.move(id, Direction.RIGHT) return self.move(player_id, Direction.RIGHT)
def move_up(self, id: str) -> PlayerInfoResponse: def move_up(self, player_id: str) -> PlayerInfoResponse:
return self.move(id, Direction.UP) return self.move(player_id, Direction.UP)
def move_down(self, id: str) -> PlayerInfoResponse: def move_down(self, player_id: str) -> PlayerInfoResponse:
return self.move(id, Direction.DOWN) return self.move(player_id, Direction.DOWN)
def move(self, id: str, direction: Direction) -> PlayerInfoResponse: def move(self, player_id: str, direction: Direction) -> PlayerInfoResponse:
path = f"/player/{id}/move/{direction}" path = f"/player/{player_id}/move/{direction}"
r = requests.post(self.format_url(path)) r = requests.post(self.format_url(path))
if r.status_code == 403: if r.status_code == 403:
@ -133,3 +151,17 @@ class FairHopper:
r.raise_for_status() r.raise_for_status()
return PlayerInfoResponse(**r.json()) 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 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)
r.raise_for_status()
return Product(**r.json())