Merge branch 'develop' into 'remove-twitter-api'
[akkoma] / lib / pleroma / web / api_spec / helpers.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.Helpers do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Schema
8
9 def request_body(description, schema_ref, opts \\ []) do
10 media_types = ["application/json", "multipart/form-data", "application/x-www-form-urlencoded"]
11
12 content =
13 media_types
14 |> Enum.map(fn type ->
15 {type,
16 %OpenApiSpex.MediaType{
17 schema: schema_ref,
18 example: opts[:example],
19 examples: opts[:examples]
20 }}
21 end)
22 |> Enum.into(%{})
23
24 %OpenApiSpex.RequestBody{
25 description: description,
26 content: content,
27 required: opts[:required] || false
28 }
29 end
30
31 def pagination_params do
32 [
33 Operation.parameter(:max_id, :query, :string, "Return items older than this ID"),
34 Operation.parameter(:min_id, :query, :string, "Return the oldest items newer than this ID"),
35 Operation.parameter(
36 :since_id,
37 :query,
38 :string,
39 "Return the newest items newer than this ID"
40 ),
41 Operation.parameter(
42 :limit,
43 :query,
44 %Schema{type: :integer, default: 20},
45 "Maximum number of items to return. Will be ignored if it's more than 40"
46 )
47 ]
48 end
49
50 def empty_object_response do
51 Operation.response("Empty object", "application/json", %Schema{type: :object, example: %{}})
52 end
53
54 def empty_array_response do
55 Operation.response("Empty array", "application/json", %Schema{type: :array, example: []})
56 end
57
58 def no_content_response do
59 Operation.response("No Content", "application/json", %Schema{type: :string, example: ""})
60 end
61 end