From d46873204033addacb9fd65e8a184afd3f608511 Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 10 Feb 2021 22:41:23 +0100 Subject: [PATCH] changed api according to specification --- build_deb.sh | 2 +- compLib/Api.py | 50 +++++++++++--------- compLib/__init__.py | 2 +- docs/source/lib/Api.rst | 51 +++++++++++--------- setup.py | 2 +- test.py | 101 ++++++++++++++++++++++++++++++++++++---- 6 files changed, 150 insertions(+), 58 deletions(-) diff --git a/build_deb.sh b/build_deb.sh index 72aa52f..6197626 100644 --- a/build_deb.sh +++ b/build_deb.sh @@ -34,7 +34,7 @@ fpm -s python --python-bin python3 --python-pip pip3 --python-package-name-prefi -d "python3-pigpio" \ -d "python3-numpy" \ -d "ffmpeg" \ - -v 0.0.4-20 -t deb setup.py + -v 0.1.0-0 -t deb setup.py # --deb-changelog changelog \ # --deb-upstream-changelog changelog \ diff --git a/compLib/Api.py b/compLib/Api.py index 8c614a4..9c6e0a6 100644 --- a/compLib/Api.py +++ b/compLib/Api.py @@ -1,4 +1,4 @@ -from typing import Dict, Tuple +from typing import Dict, Tuple, List import requests import json @@ -59,6 +59,7 @@ class Seeding: class Position: """Datastructure for holding a position """ + def __init__(self, x, y, degrees): self.x = x self.y = y @@ -68,46 +69,51 @@ class Position: class DoubleElim: """Class used for communicating with double elimination api """ + @staticmethod - def get_position() -> Position: + def get_position() -> Tuple[Position, int]: """Get position of the robot :return: A Position object with robot position - :rtype: Position + :rtype: Tuple[Position, int] """ - 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"]) + res = requests.get(API_URL_GET_POS) + response = json.loads(res.content) + Logging.get_logger().debug(f"DoubleElim.get_position = {response}, status code = {res.status_code}") + return Position(response["x"], response["y"], response["degrees"]), res.status_code @staticmethod - def get_opponent() -> Position: + def get_opponent() -> Tuple[Position, int]: """Get position of the opponents robot :return: A Position object with opponents robot position - :rtype: Position + :rtype: Tuple[Position, int] """ - 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"]) + res = requests.get(API_URL_GET_OP) + response = json.loads(res.content) + Logging.get_logger().debug(f"DoubleElim.get_opponent = x:{response}, status code = {res.status_code}") + return Position(response["x"], response["y"], response["degrees"]), res.status_code @staticmethod - def get_goal() -> Position: + def get_goal() -> Tuple[Position, int]: """Get position of the goal :return: A Position object with x and y coordinates of the goal, rotation is always -1 - :rtype: Position + :rtype: Tuple[Position, int] """ - 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) + res = requests.get(API_URL_GET_GOAL) + response = json.loads(res.content) + Logging.get_logger().debug(f"DoubleElim.get_goal = {response}, status code = {res.status_code}") + return Position(response["x"], response["y"], -1), res.status_code @staticmethod - def get_items() -> list: + def get_items() -> Tuple[List[Dict], int]: """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 + :return: A list will all items currently on the game field. Items are dictionaries that look like: {"id": 0, "x": 0, "y": 0} + :rtype: Tuple[List[Dict], int] """ - result = json.loads(requests.get(API_URL_GET_ITEMS).content) - Logging.get_logger().debug(f"DoubleElim.get_items = {result}") - return result + res = requests.get(API_URL_GET_ITEMS) + response = json.loads(res.content) + Logging.get_logger().debug(f"DoubleElim.get_items = {response}, status code = {res.status_code}") + return response, res.status_code diff --git a/compLib/__init__.py b/compLib/__init__.py index 90b556e..28d1de1 100644 --- a/compLib/__init__.py +++ b/compLib/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.0.4-20" +__version__ = "0.1.0-0" import compLib.LogstashLogging import logging diff --git a/docs/source/lib/Api.rst b/docs/source/lib/Api.rst index 7517d76..18605ae 100644 --- a/docs/source/lib/Api.rst +++ b/docs/source/lib/Api.rst @@ -32,26 +32,31 @@ Calling Seeding API from compLib.Api import Seeding - park = Seeding.get_park() - print(f"I should move to parking position: {park}") - - if park == 0: - print(f"I can't move to this position yet :(") - elif park == 1: - print(f"Moving to position 1!") - # drive to parking position using Motors module... - print(f"Now hopefully at position 1") - # drive back using Motors module... - elif park == 2: - # do something similar to park == 1.. - elif park == 3: - # do something similar to park == 1.. - - success = Seeding.pay_park() - if success: - print(f"We scored some points!") + park, code = Seeding.get_park() + if code == 403: + print(f"I am not in the correct zone to make that request!") else: - print(f"We failed :(") + park = park["id"] + print(f"I should move to parking position: {park}") + + if park == 0: + print(f"I can't move to this position yet :(") + elif park == 1: + print(f"Moving to position 1!") + # drive to parking position using Motors module... + print(f"Now hopefully at position 1") + # drive back using Motors module... + elif park == 2: + # do something similar to park == 1.. + elif park == 3: + # do something similar to park == 1.. + + success = Seeding.pay_park() + # check which code the api returned + if success == 204: + print(f"We scored some points!") + else: + print(f"We failed :(") Calling Double Elimination API ---------------------------------- @@ -60,8 +65,8 @@ Calling Double Elimination API from compLib.Api import DoubleElim - position = DoubleElim.get_position() - print(f"Position of my robot is: x={position.x}, y={position.y} and rotation is: {position.degrees}") + position, status = DoubleElim.get_position() + print(f"Position of my robot is: x={position.x}, y={position.y} and rotation is: {position.degrees}, the server responded with status code: {status}") - goal = DoubleElim.get_goal() - print(f"Goal is at: x={goal.x}, y={goal.y}") + goal, status = DoubleElim.get_goal() + print(f"Goal is at: x={goal.x}, y={goal.y}, the server responded with status code: {status}") diff --git a/setup.py b/setup.py index 303818a..10aef70 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ else: setuptools.setup( name="complib", - version="0.0.4-20", + version="0.1.0-0", author="F-WuTs", author_email="--", description="", diff --git a/test.py b/test.py index 228e8b7..3438843 100644 --- a/test.py +++ b/test.py @@ -35,6 +35,19 @@ def change_api_state(park_id: int = 0, in_get_park: bool = False, was_in_park: b requests.post("http://localhost:5000/api/api/updateState", json=data) +def change_double_elim_api_state(started: bool = False, goal: list = [0, 0], position: list = [0, 0, 0], + op_position: list = [0, 0, 0], items: list = []): + data = { + "started": started, + "goal": goal, + "position": position, + "opPosition": op_position, + "items": items + } + + requests.post("http://localhost:5000/api/api/updateDoubleElimState", json=data) + + class TestApiServer(unittest.TestCase): def test_get_park(self): ret, code = Api.Seeding.get_park() @@ -70,7 +83,7 @@ class TestApiServer(unittest.TestCase): ret = Api.Seeding.pay_park() assert type(ret) is int - assert ret == 201 + assert ret == 204 def test_simon_says(self): change_api_state(in_simon_zone=False) @@ -163,26 +176,94 @@ class TestApiServer(unittest.TestCase): last_pos = next_pos def test_get_position(self): - response = Api.DoubleElim.get_position() + response, status = Api.DoubleElim.get_position() + assert type(status) is int + assert status == 503 assert type(response) == Api.Position - assert 0 <= response.x - assert 0 <= response.y - assert 0 <= response.degrees <= 360 + assert 0 == response.x + assert 0 == response.y + assert 0 == response.degrees + + change_double_elim_api_state(started=True, position=[50, 75, 90]) + + response, status = Api.DoubleElim.get_position() + assert type(status) is int + assert status == 200 + assert type(response) == Api.Position + assert 50 == response.x + assert 75 == response.y + assert 90 == response.degrees def test_get_opponent(self): - response = Api.DoubleElim.get_opponent() + response, status = Api.DoubleElim.get_opponent() + assert type(status) is int + assert status == 503 assert type(response) == Api.Position + assert 0 == response.x + assert 0 == response.y + assert 0 == response.degrees + + change_double_elim_api_state(started=True, op_position=[50, 75, 90]) + + response, status = Api.DoubleElim.get_opponent() + assert type(status) is int + assert status == 200 + assert type(response) == Api.Position + assert 50 == response.x + assert 75 == response.y + assert 90 == response.degrees def test_get_goal(self): - response = Api.DoubleElim.get_goal() + response, status = Api.DoubleElim.get_goal() + assert type(status) is int + assert status == 503 assert type(response) == Api.Position - assert 0 <= response.x - assert 0 <= response.y + assert 0 == response.x + assert 0 == response.y + assert response.degrees == -1 + + change_double_elim_api_state(started=True, goal=[50, 75]) + + response, status = Api.DoubleElim.get_goal() + assert type(status) is int + assert status == 200 + assert type(response) == Api.Position + assert 50 == response.x + assert 75 == response.y assert response.degrees == -1 def test_get_items(self): - response = Api.DoubleElim.get_items() + response, status = Api.DoubleElim.get_items() + assert type(status) is int + assert status == 503 assert type(response) == list + assert response == [] + + change_double_elim_api_state(started=True) + + response, status = Api.DoubleElim.get_items() + assert type(status) is int + assert status == 200 + assert type(response) == list + assert response == [] + + change_double_elim_api_state(started=True, items=[{"id": 0, "x": 50, "y": 75}]) + + response, status = Api.DoubleElim.get_items() + assert type(status) is int + assert status == 200 + assert type(response) == list + assert len(response) == 1 + assert response == [{"id": 0, "x": 50, "y": 75}] + + change_double_elim_api_state(started=True, items=[{"id": 0, "x": 50, "y": 75}, {"id": 2, "x": -50, "y": -75}]) + + response, status = Api.DoubleElim.get_items() + assert type(status) is int + assert status == 200 + assert type(response) == list + assert len(response) == 2 + assert response == [{"id": 0, "x": 50, "y": 75}, {"id": 2, "x": -50, "y": -75}] def setUp(self) -> None: if START_SERVER: