-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created scripts, code and documentation for client and server.
- Loading branch information
1 parent
efdd681
commit 3987d99
Showing
69 changed files
with
391 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/.env_oauth | ||
client/.env_oauth | ||
/infra/terraform.tfvars | ||
/.idea/.gitignore | ||
/infra/.terraform.lock.hcl | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# client/config/__init__.py | ||
|
||
from .oauth import oauth_settings | ||
|
||
__all__ = ["oauth_settings"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .logger import logger | ||
|
||
__all__ = ["logger"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import logging | ||
|
||
logging.basicConfig( | ||
level=logging.INFO, | ||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | ||
) | ||
|
||
logger = logging.getLogger("logger") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# client/models/__init__.py | ||
from .hero import Hero | ||
|
||
from .dnd_hero import DnDHero, AbilityScores, SkillProficiencies, Equipment, Spell | ||
|
||
__all__ = ["DnDHero", "AbilityScores", "SkillProficiencies", "Equipment", "Spell"] | ||
__all__ = ["Hero"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import Optional | ||
from pydantic import BaseModel | ||
|
||
|
||
class Hero(BaseModel): | ||
id: str | ||
name: str | ||
race: str | ||
class_: str # Avoids conflict with the Python `class` keyword | ||
level: int | ||
background: Optional[str] = None | ||
alignment: Optional[str] = None | ||
hit_points: int | ||
armor_class: int | ||
speed: int | ||
personality_traits: Optional[str] = None | ||
ideals: Optional[str] = None | ||
bonds: Optional[str] = None | ||
flaws: Optional[str] = None |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
fastapi==0.115.2 # FastAPI framework for building APIs | ||
uvicorn==0.32.0 # ASGI server for running FastAPI apps | ||
pydantic==2.9.2 # Data validation and parsing for FastAPI models | ||
fastapi==0.115.2 | ||
uvicorn==0.32.0 | ||
pydantic==2.9.2 | ||
config~=0.5.1 | ||
dotenv~=0.0.5 | ||
python-dotenv==1.0.1 | ||
httpx==0.27.2 | ||
jwt==1.3.1 | ||
pyjwt==2.9.0 | ||
pydantic_settings==2.6.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
# client/routers/__init__.py | ||
|
||
__all__ = ["heroes", "auth"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,66 @@ | ||
# client/routers/heroes.py | ||
|
||
import os | ||
from http.client import HTTPException | ||
from typing import List | ||
|
||
from fastapi import APIRouter | ||
from fastapi import HTTPException | ||
import httpx | ||
from fastapi import APIRouter, HTTPException | ||
|
||
from client.models.dnd_hero import DnDHero | ||
from client.services.auth_service import verify_scope | ||
from client.services.hero_service import HeroService | ||
from client.logger import logger | ||
from client.models import Hero | ||
from client.services.token_storage import get_stored_token # Import get_stored_token function | ||
|
||
router = APIRouter() | ||
hero_service = HeroService() | ||
|
||
# Set values based on environment variable or hard-coded URL | ||
BACKEND_API_BASE_URL = os.getenv("HVALFANGST_API_URL", "https://hvalfangstlinuxwebapp.azurewebsites.net/api") | ||
|
||
|
||
# Helper function to make HTTP requests to the backend API | ||
async def request_backend(method: str, endpoint: str, json=None): | ||
url = f"{BACKEND_API_BASE_URL}{endpoint}" | ||
|
||
# Retrieve the access token from token storage | ||
token_data = get_stored_token() | ||
headers = {"Authorization": f"Bearer {token_data}"} if token_data else {} | ||
|
||
# Log the request details | ||
logger.info(f"Preparing {method} request to URL: {url}") | ||
logger.info(f"Headers: {headers}") | ||
logger.info(f"Payload: {json}") | ||
|
||
try: | ||
async with httpx.AsyncClient() as client: | ||
response = await client.request(method, url, json=json, headers=headers) | ||
response.raise_for_status() | ||
logger.info(f"Request to {url} completed successfully with status code {response.status_code}") | ||
return response.json() | ||
except httpx.HTTPStatusError as e: | ||
logger.error(f"HTTP error occurred for {method} request to {url}: {e.response.status_code} - {e.response.text}") | ||
raise HTTPException(status_code=e.response.status_code, detail=e.response.text) | ||
except Exception as e: | ||
logger.error(f"An unexpected error occurred: {e}") | ||
raise HTTPException(status_code=500, detail="An unexpected error occurred") | ||
|
||
|
||
# POST: Create a new Hero | ||
@router.post("/heroes/", response_model=DnDHero) | ||
async def create_hero(hero: DnDHero): | ||
return await hero_service.create_hero(hero) | ||
@router.post("/heroes/", response_model=Hero) | ||
async def create_hero(hero: Hero): | ||
return await request_backend("POST", "/heroes/", json=hero.dict()) | ||
|
||
|
||
# GET: Retrieve a hero by ID | ||
@router.get("/heroes/{hero_id}", response_model=DnDHero) | ||
@router.get("/heroes/{hero_id}", response_model=Hero) | ||
async def read_hero(hero_id: str): | ||
hero = await hero_service.get_hero(hero_id) | ||
if hero: | ||
return hero | ||
else: | ||
raise HTTPException(status_code=404, detail="Hero not found") | ||
return await request_backend("GET", f"/heroes/{hero_id}") | ||
|
||
|
||
# GET: Retrieve all heroes | ||
@router.get("/heroes/", response_model=List[DnDHero]) | ||
@router.get("/heroes/", response_model=List[Hero]) | ||
async def read_heroes(): | ||
await verify_scope(["Heroes.Read"]) | ||
return await hero_service.list_heroes() | ||
return await request_backend("GET", "/heroes/") | ||
|
||
|
||
# DELETE: Delete a hero by ID | ||
@router.delete("/heroes/{hero_id}", response_model=dict) | ||
async def delete_hero(hero_id: str): | ||
success = await hero_service.delete_hero(hero_id) | ||
if success: | ||
return {"message": f"Hero with id '{hero_id}' deleted successfully"} | ||
else: | ||
raise HTTPException(status_code=404, detail="Hero not found") | ||
|
||
|
||
# GET: Custom query to retrieve heroes with Fireball spell and AC < 20 | ||
@router.get("/heroes-fireball-low-ac", response_model=List[DnDHero]) | ||
async def get_fireball_heroes_with_low_ac(): | ||
return await hero_service.query_heroes_fireball_low_ac() | ||
return await request_backend("DELETE", f"/heroes/{hero_id}") |
Oops, something went wrong.