Add OpenAPI
[akkoma] / lib / pleroma / web / api_spec / operations / app_operation.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ApiSpec.AppOperation do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Schema
8 alias Pleroma.Web.ApiSpec.Schemas.AppCreateRequest
9 alias Pleroma.Web.ApiSpec.Schemas.AppCreateResponse
10
11 @spec open_api_operation(atom) :: Operation.t()
12 def open_api_operation(action) do
13 operation = String.to_existing_atom("#{action}_operation")
14 apply(__MODULE__, operation, [])
15 end
16
17 @spec create_operation() :: Operation.t()
18 def create_operation do
19 %Operation{
20 tags: ["apps"],
21 summary: "Create an application",
22 description: "Create a new application to obtain OAuth2 credentials",
23 operationId: "AppController.create",
24 requestBody:
25 Operation.request_body("Parameters", "application/json", AppCreateRequest, required: true),
26 responses: %{
27 200 => Operation.response("App", "application/json", AppCreateResponse),
28 422 =>
29 Operation.response(
30 "Unprocessable Entity",
31 "application/json",
32 %Schema{
33 type: :object,
34 description:
35 "If a required parameter is missing or improperly formatted, the request will fail.",
36 properties: %{
37 error: %Schema{type: :string}
38 },
39 example: %{
40 "error" => "Validation failed: Redirect URI must be an absolute URI."
41 }
42 }
43 )
44 }
45 }
46 end
47
48 def verify_credentials_operation do
49 %Operation{
50 tags: ["apps"],
51 summary: "Verify your app works",
52 description: "Confirm that the app's OAuth2 credentials work.",
53 operationId: "AppController.verify_credentials",
54 parameters: [
55 Operation.parameter(:authorization, :header, :string, "Bearer <app token>", required: true)
56 ],
57 responses: %{
58 200 =>
59 Operation.response("App", "application/json", %Schema{
60 type: :object,
61 description:
62 "If the Authorization header was provided with a valid token, you should see your app returned as an Application entity.",
63 properties: %{
64 name: %Schema{type: :string},
65 vapid_key: %Schema{type: :string},
66 website: %Schema{type: :string, nullable: true}
67 },
68 example: %{
69 "name" => "My App",
70 "vapid_key" =>
71 "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=",
72 "website" => "https://myapp.com/"
73 }
74 }),
75 422 =>
76 Operation.response(
77 "Unauthorized",
78 "application/json",
79 %Schema{
80 type: :object,
81 description:
82 "If the Authorization header contains an invalid token, is malformed, or is not present, an error will be returned indicating an authorization failure.",
83 properties: %{
84 error: %Schema{type: :string}
85 },
86 example: %{
87 "error" => "The access token is invalid."
88 }
89 }
90 )
91 }
92 }
93 end
94 end