This repository has been archived on 2025-06-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
compLIB/compLib/Api.py
2021-01-21 16:43:01 +00:00

109 lines
3.4 KiB
Python

import requests
import json
import os
from compLib.LogstashLogging import Logging
API_URL = os.getenv("API_URL", "http://localhost:5000/") + "api/"
API_URL_GET_POS = API_URL + "getPos"
API_URL_GET_OP = API_URL + "getOp"
API_URL_GET_GOAL = API_URL + "getGoal"
API_URL_GET_ITEMS = API_URL + "getItems"
API_URL_GET_PARK = API_URL + "getPark"
API_URL_PAY_PARK = API_URL + "payPark"
API_URL_SIMON_SAYS = API_URL + "simonSays"
class Seeding:
"""Class used for communicating with seeding api
"""
@staticmethod
def get_park() -> int:
"""Get a parkingsapce from the api.
:return: An int between 0 and 3
:rtype: int
"""
result = json.loads(requests.get(API_URL_GET_PARK).content)["id"]
Logging.get_logger().debug(f"Seeding.get_park = {result}")
return result
@staticmethod
def pay_park() -> bool:
"""Pay for parking.
:return: True if successful, False if not successful
:rtype: bool
"""
result = requests.get(API_URL_PAY_PARK).status_code == 200
Logging.get_logger().debug(f"Seeding.pay_park = {result}")
return result
@staticmethod
def simon_says() -> int:
"""Get next simon says zone from the api.
:return: An int between 0 and 3 or -1 after making this request 5 times.
:rtype: int
"""
result = json.loads(requests.get(API_URL_SIMON_SAYS).content)["id"]
Logging.get_logger().debug(f"Seeding.simon_says = {result}")
return result
class Position:
"""Datastructure for holding a position
"""
def __init__(self, x, y, degrees):
self.x = x
self.y = y
self.degrees = degrees
class DoubleElim:
"""Class used for communicating with double elimination api
"""
@staticmethod
def get_position() -> Position:
"""Get position of the robot
:return: A Position object with robot position
:rtype: Position
"""
response = json.loads(requests.get(API_URL_GET_POS).content)
Logging.get_logger().debug(f"DoubleElim.get_position = {response}")
return Position(response["x"], response["y"], response["degrees"])
@staticmethod
def get_opponent() -> Position:
"""Get position of the opponents robot
:return: A Position object with opponents robot position
:rtype: Position
"""
response = json.loads(requests.get(API_URL_GET_OP).content)
Logging.get_logger().debug(f"DoubleElim.get_opponent = x:{response}")
return Position(response["x"], response["y"], response["degrees"])
@staticmethod
def get_goal() -> Position:
"""Get position of the goal
:return: A Position object with x and y coordinates of the goal, rotation is always -1
:rtype: Position
"""
response = json.loads(requests.get(API_URL_GET_GOAL).content)
Logging.get_logger().debug(f"DoubleElim.get_goal = x:{response}")
return Position(response["x"], response["y"], -1)
@staticmethod
def get_items() -> list:
"""Get a list with all current items
:return: A list will all items currently on the game field. Items are dictionaries that look like: {"id": 0}
:rtype: list
"""
result = json.loads(requests.get(API_URL_GET_ITEMS).content)
Logging.get_logger().debug(f"DoubleElim.get_items = {result}")
return result