-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add OpenApi spec and some unit tests
- Loading branch information
Showing
19 changed files
with
413 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[ | ||
import_deps: [:phoenix], | ||
import_deps: [:phoenix, :open_api_spex], | ||
plugins: [Phoenix.LiveView.HTMLFormatter], | ||
inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Helldivers2Web.ApiSpec do | ||
alias OpenApiSpex.{Info, OpenApi, Paths, Server} | ||
alias Helldivers2Web.{Endpoint, Router} | ||
@behaviour OpenApi | ||
|
||
@impl OpenApi | ||
def spec do | ||
%OpenApi{ | ||
servers: [ | ||
# Populate the Server info from a phoenix endpoint | ||
Server.from_endpoint(Endpoint) | ||
], | ||
info: %Info{ | ||
title: "Helldivers 2", | ||
version: "0.0.1" | ||
}, | ||
# Populate the paths from a phoenix router | ||
paths: Paths.from_router(Router) | ||
} | ||
|> OpenApiSpex.resolve_schema_modules() # Discover request/response schemas from path specs | ||
end | ||
end |
37 changes: 33 additions & 4 deletions
37
lib/helldivers_2_web/controllers/api/planets_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 31 additions & 2 deletions
33
lib/helldivers_2_web/controllers/api/war_season_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Helldivers2Web.Plugs.WarSeason do | ||
import Plug.Conn | ||
|
||
alias Helldivers2Web.FallbackController | ||
alias Helldivers2.WarSeason | ||
|
||
@doc """ | ||
If the request contains a `war_id` it validates that it exists. | ||
This ensures that the rest of the application can assume any `war_id` passed exists. | ||
""" | ||
@spec check_war_id(Plug.Conn.t()) :: Plug.Conn.t() | ||
def check_war_id(conn, _options \\ []) do | ||
case Map.get(conn.params, "war_id") do | ||
nil -> | ||
conn | ||
war_id -> | ||
validate_war_id(conn, war_id) | ||
end | ||
end | ||
|
||
# We have a war_id, validate it exists | ||
defp validate_war_id(conn, war_id) do | ||
case WarSeason.exists?(war_id) do | ||
true -> | ||
conn | ||
false -> | ||
conn | ||
|> FallbackController.call({:error, :not_found}) | ||
|> halt() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Helldivers2Web.Schemas.HomeWorldSchema do | ||
alias Helldivers2Web.Schemas.PlanetSchema | ||
alias OpenApiSpex.Schema | ||
require OpenApiSpex | ||
|
||
@doc "Generates a schema for a single homeworld schema response" | ||
def response(), do: {"Homeworld response", "application/json", __MODULE__} | ||
|
||
OpenApiSpex.schema(%{ | ||
description: "Homeworld information of a given faction", | ||
type: :object, | ||
properties: %{ | ||
race: %Schema{ | ||
type: :string, | ||
description: "The race that the planet is the homeworld of" | ||
}, | ||
planets: %Schema{ | ||
type: :array, | ||
items: PlanetSchema, | ||
description: "The planets this race considers it's homeworlds" | ||
} | ||
} | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Helldivers2Web.Schemas.NotFoundSchema do | ||
alias OpenApiSpex.Schema | ||
require OpenApiSpex | ||
|
||
def response(), do: {"Resource not found", "application/json", __MODULE__} | ||
|
||
OpenApiSpex.schema(%{ | ||
description: "The resource you tried to retrieve could not be found", | ||
type: :object, | ||
properties: %{ | ||
errors: %Schema{type: :object, properties: %{ | ||
detail: %Schema{type: :string, description: "Description of what went wrong"} | ||
}} | ||
} | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule Helldivers2Web.Schemas.PlanetSchema do | ||
alias OpenApiSpex.Schema | ||
require OpenApiSpex | ||
|
||
@doc "Generates a schema for a single planet schema response" | ||
def response(), do: {"Planet response", "application/json", __MODULE__} | ||
|
||
@doc "Generates a schema for an array of planet schemas" | ||
def responses(), do: {"Planets response", "application/json", %Schema{type: :array, items: __MODULE__}} | ||
|
||
OpenApiSpex.schema(%{ | ||
description: "Represents a planet in the galactic war that must receive Managed democracy", | ||
type: :object, | ||
properties: %{ | ||
index: %Schema{ | ||
type: :integer, | ||
description: | ||
"The index of this planet, for convenience kept the same as in the official API" | ||
}, | ||
name: %Schema{ | ||
type: :string, | ||
description: "The human readable name of the planet, or unknown if it's not a known name" | ||
}, | ||
hash: %Schema{ | ||
type: :integer, | ||
description: "A hash retrieved from the official API, purpose unknown" | ||
}, | ||
position: %Schema{ | ||
type: :object, | ||
description: "The coordinates in the galaxy where this planet is located", | ||
properties: %{ | ||
x: %Schema{type: :number, description: "X coordinate"}, | ||
y: %Schema{type: :number, description: "Y coordinate"} | ||
} | ||
}, | ||
waypoints: %Schema{ | ||
type: :array, | ||
description: "Waypoints, seems to link planets together but purpose unclear" | ||
}, | ||
max_health: %Schema{ | ||
type: :integer, | ||
description: "The maximum health of this planet, used in conflict stats" | ||
}, | ||
disabled: %Schema{ | ||
type: :boolean, | ||
description: "Whether or not this planet is currently playable (enabled)" | ||
}, | ||
initial_owner: %Schema{ | ||
type: :string, | ||
description: "Which faction originally claimed this planet" | ||
} | ||
} | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule Helldivers2Web.Schemas.TooManyRequestsSchema do | ||
alias OpenApiSpex.Schema | ||
require OpenApiSpex | ||
|
||
def response(), do: {"Rate limit exceeded", "application/json", __MODULE__} | ||
|
||
OpenApiSpex.schema(%{ | ||
description: "You're making too many requests in a limited span of time", | ||
type: :object, | ||
properties: %{ | ||
error: %Schema{type: :string, description: "Error message for rate limit being exceeded"} | ||
} | ||
}) | ||
end |
Oops, something went wrong.