From 4b9ed933a5053d7371ff53e592e86d049e8375e8 Mon Sep 17 00:00:00 2001 From: Wannes Gennar Date: Thu, 29 Feb 2024 01:26:45 +0100 Subject: [PATCH] add endpoint for fetching planet status add endpoint to fetch the war status of a specific planet rather than globally --- .../controllers/api/planets_controller.ex | 24 +++++++++++++ .../controllers/api/planets_json.ex | 1 + lib/helldivers_2_web/router.ex | 1 + .../api/planets_controller_test.exs | 35 +++++++++++++++++++ test/support/conn_case.ex | 3 ++ 5 files changed, 64 insertions(+) diff --git a/lib/helldivers_2_web/controllers/api/planets_controller.ex b/lib/helldivers_2_web/controllers/api/planets_controller.ex index 4e390f0..21320f5 100644 --- a/lib/helldivers_2_web/controllers/api/planets_controller.ex +++ b/lib/helldivers_2_web/controllers/api/planets_controller.ex @@ -1,4 +1,5 @@ defmodule Helldivers2Web.Api.PlanetsController do + alias Helldivers2Web.Schemas.PlanetStatusSchema use Helldivers2Web, :controller use OpenApiSpex.ControllerSpecs @@ -46,4 +47,27 @@ defmodule Helldivers2Web.Api.PlanetsController do render(conn, :show, planet: planets) end end + + operation :show_planet_status, + summary: "Get the current war status of a specific planet", + parameters: [ + war_id: [in: :path, description: "The war ID", type: :integer, example: 801], + planet_index: [ + in: :path, + description: "The index of the planet", + type: :integer, + example: 0 + ] + ], + responses: [ + ok: PlanetStatusSchema.response(), + not_found: NotFoundSchema.response(), + too_many_requests: TooManyRequestsSchema.response(), + unprocessable_entity: JsonErrorResponse.response() + ] + def show_planet_status(conn, %{war_id: war_id, planet_index: planet_index}) do + with {:ok, planet_status} <- WarSeason.get_planet_status(war_id, planet_index) do + render(conn, :show_status, planet_status: planet_status) + end + end end diff --git a/lib/helldivers_2_web/controllers/api/planets_json.ex b/lib/helldivers_2_web/controllers/api/planets_json.ex index 66c617d..f0bd106 100644 --- a/lib/helldivers_2_web/controllers/api/planets_json.ex +++ b/lib/helldivers_2_web/controllers/api/planets_json.ex @@ -31,6 +31,7 @@ defmodule Helldivers2Web.Api.PlanetsJSON do end @doc "Named separately from `show/1` to avoid pattern matching conflicts (`PlanetStatus` also matches for `%{planet: planet}`)" + def show_status(%{planet_status: planet_status}), do: show_status(planet_status) def show_status(%PlanetStatus{} = planet_status) do %{ "planet" => show(planet_status.planet), diff --git a/lib/helldivers_2_web/router.ex b/lib/helldivers_2_web/router.ex index 4694f1a..8b1c795 100644 --- a/lib/helldivers_2_web/router.ex +++ b/lib/helldivers_2_web/router.ex @@ -45,6 +45,7 @@ defmodule Helldivers2Web.Router do get "/:war_id/planets", PlanetsController, :index get "/:war_id/planets/:planet_index", PlanetsController, :show + get "/:war_id/planets/:planet_index/status", PlanetsController, :show_planet_status end # Enable LiveDashboard in development diff --git a/test/helldivers_2_web/controllers/api/planets_controller_test.exs b/test/helldivers_2_web/controllers/api/planets_controller_test.exs index 562e641..b8e4513 100644 --- a/test/helldivers_2_web/controllers/api/planets_controller_test.exs +++ b/test/helldivers_2_web/controllers/api/planets_controller_test.exs @@ -70,4 +70,39 @@ defmodule Helldivers2Web.Api.PlanetsControllerTest do assert_schema(response, "PlanetSchema", spec) end end + + describe "GET /api/:war_id/planets/:planet_index/status" do + setup _ do + # We need war status for these endpoints + WarSeason.store(@war_id, war_status_fixture()) + end + + test "returns 404 for unknown planets", %{conn: conn} do + conn + |> get(~p"/api/#{@war_id}/planets/5/status") + |> json_response(404) + end + + test "returns 422 for invalid planet indices", %{conn: conn} do + conn + |> get(~p"/api/#{@war_id}/planets/invalid/status") + |> json_response(422) + end + + test "returns 200 for a valid planet index", %{conn: conn} do + conn + |> get(~p"/api/#{@war_id}/planets/0/status") + |> json_response(200) + end + + test "response conforms to schema", %{conn: conn} do + response = + conn + |> get(~p"/api/#{@war_id}/planets/0/status") + |> json_response(200) + + spec = Helldivers2Web.ApiSpec.spec() + assert_schema(response, "PlanetStatusSchema", spec) + end + end end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index d34e669..03e335c 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -32,6 +32,9 @@ defmodule Helldivers2Web.ConnCase do end setup _tags do + # Ensure the rate limit bucket is cleared before every test. + ExRated.delete_bucket("127.0.0.1") + {:ok, conn: Phoenix.ConnTest.build_conn()} end end