Compare commits
20 commits
main
...
auth_secre
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00b619d892 | ||
|
|
944ba9791b | ||
|
|
ac4a90edd1 | ||
|
|
70cfef54ad | ||
|
|
eaeb1c1b7f | ||
|
|
6de35547a3 | ||
|
|
1bb40881a7 | ||
|
|
719eb363a9 | ||
|
|
7b94d0074d | ||
|
|
e597798d7f | ||
|
|
80535cb27c | ||
|
|
f8cc27a037 | ||
|
|
d2fa2774eb | ||
|
|
bfc5adac70 | ||
|
|
4f3370efef | ||
|
|
85e9b18039 | ||
|
|
64a335ac98 | ||
|
|
57cebaaf02 | ||
|
|
b90aaad637 | ||
|
|
4e4fb04b88 |
7 changed files with 147 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -160,3 +160,4 @@ cython_debug/
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
secrets/auth.yaml
|
||||||
|
|
@ -1,4 +1,2 @@
|
||||||
# bgg_api
|
# bgg_api
|
||||||
|
|
||||||
An API implementation that will be used by my own board game website.
|
|
||||||
The plan is for this API to be used by a single person who wants their board game collection to be cached so that retrieval is much faster.
|
|
||||||
|
|
|
||||||
25
auth_manager.py
Normal file
25
auth_manager.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#Can only be imported on main.py
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
username: str = None
|
||||||
|
password: str = None
|
||||||
|
|
||||||
|
auth_secret_file_location = './secrets/auth.yaml'
|
||||||
|
|
||||||
|
def load_username_password_from_secrets():
|
||||||
|
global username
|
||||||
|
global password
|
||||||
|
|
||||||
|
with open(auth_secret_file_location, 'r') as auth_file:
|
||||||
|
auth_object = yaml.safe_load(auth_file)
|
||||||
|
|
||||||
|
username = auth_object['username']
|
||||||
|
password = auth_object['password']
|
||||||
|
|
||||||
|
|
||||||
|
def get_username_password():
|
||||||
|
return username, password
|
||||||
|
|
||||||
|
|
||||||
|
load_username_password_from_secrets()
|
||||||
77
bgg_connection.py
Normal file
77
bgg_connection.py
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
import requests
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
from pydantic import HttpUrl
|
||||||
|
import requests
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def get_authenticated_bgg_session(username: str, password: str) -> requests.Session:
|
||||||
|
login_url = "https://boardgamegeek.com/login/api/v1"
|
||||||
|
|
||||||
|
post_data = {
|
||||||
|
"credentials":{
|
||||||
|
"username": username,
|
||||||
|
"password": password
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
authenticated_session = requests.Session()
|
||||||
|
login_response = authenticated_session.post(login_url, json=post_data)
|
||||||
|
|
||||||
|
assert login_response.status_code == 204
|
||||||
|
|
||||||
|
return authenticated_session
|
||||||
19
classes/boardgame.py
Normal file
19
classes/boardgame.py
Normal 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
22
main.py
Normal 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
|
||||||
3
secrets/auth_example.yaml
Normal file
3
secrets/auth_example.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#Rename this file to auth.yaml and provide correct values below to authenticate to bgg
|
||||||
|
username: username_example
|
||||||
|
password: password_example
|
||||||
Loading…
Reference in a new issue