Compare commits

...

16 commits

Author SHA1 Message Date
Yarne Coppens
eaeb1c1b7f Put requested board game in variable for expandability/testing 2024-07-26 12:04:44 +02:00
Yarne Coppens
6de35547a3 Implemented boardgame type check for expansions 2024-07-26 12:01:12 +02:00
Yarne Coppens
1bb40881a7 Getting all boardgame attributes. All except expansion id's 2024-07-26 11:41:34 +02:00
Yarne Coppens
719eb363a9 can now get information about a single boardgame 2024-07-26 10:46:46 +02:00
Yarne Coppens
7b94d0074d fixed BoardGame class image_url and thumbnail_url wrong declaration 2024-07-26 10:45:29 +02:00
Yarne Coppens
e597798d7f Merge branch 'creating_boardgame_class' into getting_boardgame_from_bgg 2024-07-26 10:20:57 +02:00
Yarne Coppens
80535cb27c Added expansion id property to BoardGame class 2024-07-25 22:07:14 +02:00
Yarne Coppens
f8cc27a037 Created basic BoardGame class 2024-07-25 22:01:25 +02:00
Yarne Coppens
d2fa2774eb Started work on getting single boardgame from boardgamegeek 2024-07-25 21:52:49 +02:00
Yarne Coppens
bfc5adac70 Started work on bgg_connection module 2024-07-25 19:58:28 +02:00
Yarne Coppens
4f3370efef Used response model 2024-07-25 19:38:20 +02:00
Yarne Coppens
85e9b18039 Gave the get_boardgame_by_id endpoint a Return Type 2024-07-25 18:02:37 +02:00
Yarne Coppens
64a335ac98 Fixed Pydantic class not inheriting from BaseModel 2024-07-25 18:01:51 +02:00
Yarne Coppens
57cebaaf02 Added API endpoint base for retrieving boardgame by ID 2024-07-25 17:53:59 +02:00
Yarne Coppens
b90aaad637 Started base boardgame class 2024-07-25 17:27:27 +02:00
Yarne Coppens
4e4fb04b88 Created base for fastapi 2024-07-24 22:53:56 +02:00
3 changed files with 100 additions and 0 deletions

59
bgg_connection.py Normal file
View file

@ -0,0 +1,59 @@
import requests
import xml.etree.ElementTree as ET
from pydantic import HttpUrl
from classes.boardgame import BoardGame, BoardGameExpansion
def url_to_xml_object(url: HttpUrl) -> ET.Element:
r = requests.get(url)
root = ET.fromstring(r.content)
return root
#Requires single boardgame XML 'item' from bgg api
def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
boardgame_type = boardgame_xml.get('type')
match boardgame_type:
case "boardgame":
boardgame = BoardGame(
id = boardgame_xml.get('id'),
name = boardgame_xml.find('name').get('value'),
description = boardgame_xml.find('description').text,
image_url = boardgame_xml.find('image').text,
thumbnail_url = boardgame_xml.find('thumbnail').text,
year_published = int(boardgame_xml.find('yearpublished').get('value')),
min_players = int(boardgame_xml.find('minplayers').get('value')),
max_players = int(boardgame_xml.find('maxplayers').get('value')),
min_playing_time = int(boardgame_xml.find('minplaytime').get('value')),
max_playing_time = int(boardgame_xml.find('maxplaytime').get('value')),
min_age = int(boardgame_xml.find('minage').get('value')),
all_expansion_ids = [0,1,2,3]
)
case "boardgameexpansion":
boardgame = BoardGameExpansion(
id = boardgame_xml.get('id'),
name = boardgame_xml.find('name').get('value'),
description = boardgame_xml.find('description').text,
image_url = boardgame_xml.find('image').text,
thumbnail_url = boardgame_xml.find('thumbnail').text,
year_published = int(boardgame_xml.find('yearpublished').get('value')),
min_players = int(boardgame_xml.find('minplayers').get('value')),
max_players = int(boardgame_xml.find('maxplayers').get('value')),
min_playing_time = int(boardgame_xml.find('minplaytime').get('value')),
max_playing_time = int(boardgame_xml.find('maxplaytime').get('value')),
min_age = int(boardgame_xml.find('minage').get('value')),
all_expansion_ids = [0,1,2,3]
)
return boardgame
def get_boardgame(boardgame_id: int) -> BoardGame:
url : str = "https://boardgamegeek.com/xmlapi2/thing?id={}&stats=true".format(boardgame_id)
boardgame_xml_object : ET.Element = url_to_xml_object(url)
requested_boardgame = convert_xml_to_boardgame(boardgame_xml_object.find('item'))
return requested_boardgame

19
classes/boardgame.py Normal file
View file

@ -0,0 +1,19 @@
from pydantic import BaseModel, HttpUrl
class BoardGame(BaseModel):
id: int
name: str
description: str
image_url : HttpUrl
thumbnail_url : HttpUrl
year_published: int
min_players: int
max_players: int
min_playing_time: int
max_playing_time: int
min_age: int
all_expansion_ids: list[int]
class BoardGameExpansion(BoardGame):
pass

22
main.py Normal file
View file

@ -0,0 +1,22 @@
from typing import Union
from fastapi import FastAPI
from classes.boardgame import BoardGame, BoardGameExpansion
from bgg_connection import get_boardgame
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
@app.get("/boardgame/{boardgame_id}", response_model=BoardGame)
def get_boardgame_by_id(boardgame_id: int):
requested_boardgame: BoardGame = get_boardgame(boardgame_id)
return requested_boardgame