<filename>battlesnake_server/main.py<gh_stars>0
from .models import GameStatus

from pprint import pprint

import json
import random

from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.exception_handlers import request_validation_exception_handler


app = FastAPI()

move_choices = ['up', 'down', 'left', 'right']

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    pprint(exc)
    return await request_validation_exception_handler(request, exc)

@app.get('/')
def read_root():
    '''returns the json stored at the root level directory if it exists, otherwise just apiversion'''
    try:
        with open('battlesnake-info.json', 'r') as fo:
            battlesnake_info = json.load(fo)
            return battlesnake_info 
    except FileNotFoundError:
        return {'apiversion' : "1"}

@app.post('/start', summary="this endpoint receives a request at the start of a match")
def save_start_info(info : GameStatus):
    '''dumps info param to a file (startinfo.json), returns a 200 status code'''
    with open("startinfo.json", "w") as fo:
        json.dump(info.dict(), fo) 

    return "ok" 

@app.post('/end', summary="this endpoint receives a request at the end of a match")
def save_end_info(info : GameStatus):
    '''dumps info param to a file (endinfo.json), returns a 200 status code'''
    with open("endinfo.json", "w") as fo:
        json.dump(info.dict(), fo)

    return "ok"

@app.post('/move',
summary="this endpoint receives a request every turn, expects a JSON Object with a 'move' key and an optional 'shout' key as a response")
def make_move(info : GameStatus):
    '''saves info to a file called turn{turn_number}.json, returns random move'''
    with open(f"turn{info.turn}.json", "w") as fo:
        json.dump(info.dict(), fo)
    return {
        "move" : random.choice(move_choices),
    } 

<filename>battlesnake_server/main.py<gh_stars>0
from .models import GameStatus

from pprint import pprint

import json
import random

from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.exception_handlers import request_validation_exception_handler


app = FastAPI()

move_choices = ['up', 'down', 'left', 'right']

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    pprint(exc)
    return await request_validation_exception_handler(request, exc)

@app.get('/')
def read_root():
    '''returns the json stored at the root level directory if it exists, otherwise just apiversion'''
    try:
        with open('battlesnake-info.json', 'r') as fo:
            battlesnake_info = json.load(fo)
            return battlesnake_info 
    except FileNotFoundError:
        return {'apiversion' : "1"}

@app.post('/start', summary="this endpoint receives a request at the start of a match")
def save_start_info(info : GameStatus):
    '''dumps info param to a file (startinfo.json), returns a 200 status code'''
    with open("startinfo.json", "w") as fo:
        json.dump(info.dict(), fo) 

    return "ok" 

@app.post('/end', summary="this endpoint receives a request at the end of a match")
def save_end_info(info : GameStatus):
    '''dumps info param to a file (endinfo.json), returns a 200 status code'''
    with open("endinfo.json", "w") as fo:
        json.dump(info.dict(), fo)

    return "ok"

@app.post('/move',
summary="this endpoint receives a request every turn, expects a JSON Object with a 'move' key and an optional 'shout' key as a response")
def make_move(info : GameStatus):
    '''saves info to a file called turn{turn_number}.json, returns random move'''
    with open(f"turn{info.turn}.json", "w") as fo:
        json.dump(info.dict(), fo)
    return {
        "move" : random.choice(move_choices),
    } 

returns the json stored at the root level directory if it exists, otherwise just apiversion dumps info param to a file (startinfo.json), returns a 200 status code dumps info param to a file (endinfo.json), returns a 200 status code saves info to a file called turn{turn_number}.json, returns random move