Skip to content

Commit

Permalink
add endpoint for fetching planet status
Browse files Browse the repository at this point in the history
add endpoint to fetch the war status of a specific planet rather than globally
  • Loading branch information
dealloc committed Feb 29, 2024
1 parent 71baede commit 4b9ed93
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/helldivers_2_web/controllers/api/planets_controller.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Helldivers2Web.Api.PlanetsController do
alias Helldivers2Web.Schemas.PlanetStatusSchema
use Helldivers2Web, :controller
use OpenApiSpex.ControllerSpecs

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/helldivers_2_web/controllers/api/planets_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions lib/helldivers_2_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/helldivers_2_web/controllers/api/planets_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4b9ed93

Please sign in to comment.