-
Notifications
You must be signed in to change notification settings - Fork 16
/
format.ex
68 lines (53 loc) · 1.87 KB
/
format.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
defmodule Conduit.Plug.Format do
use Conduit.Plug.Builder
@moduledoc """
Formats the message body based on the content type.
It uses in order of preference:
1. The content type specified for the plug.
2. The content type specified on the message.
3. The default content type `text/plain`.
The location of the content type can be changed from `content_type`
to a header with the `:header` option.
This plug should be used in an outgoing pipeline.
## Examples
plug Conduit.Plug.Format
plug Conduit.Plug.Format, content_type: "application/json"
plug Conduit.Plug.Format, header: "Content-Type"
iex> import Conduit.Message
iex> message =
iex> %Conduit.Message{}
iex> |> put_body(%{})
iex> |> Conduit.Plug.Format.run(content_type: "application/json")
iex> message.body
"{}"
iex> message.content_type
"application/json"
iex> import Conduit.Message
iex> message =
iex> %Conduit.Message{}
iex> |> put_body(%{})
iex> |> Conduit.Plug.Format.run(content_type: "application/json", header: "content_type")
iex> message.body
"{}"
iex> get_header(message, "content_type")
"application/json"
"""
alias Conduit.ContentType
@doc """
Formats the message body based on the content type.
"""
@default_content_type "text/plain"
def call(message, next, opts) do
content_type = Keyword.get(opts, :content_type) || Map.get(message, :content_type) || @default_content_type
message
|> put_content_type_at(Keyword.get(opts, :header), content_type)
|> ContentType.format(content_type, opts)
|> next.()
end
defp put_content_type_at(message, nil, content_type) do
put_content_type(message, content_type)
end
defp put_content_type_at(message, header, content_type) do
put_header(message, header, content_type)
end
end