/
mint.ex
151 lines (124 loc) · 4.2 KB
/
mint.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
if Code.ensure_loaded?(Mint.HTTP1) do
defmodule Arangox.MintClient do
@moduledoc """
An HTTP client implementation of the \
[`:mint`](https://hexdocs.pm/mint/Mint.HTTP.html "documentation") \
library. Requires [`:mint`](https://hex.pm/packages/mint "hex.pm") to be
added as a dependency.
[__Hex.pm__](https://hex.pm/packages/mint)
[__Documentation__](https://hexdocs.pm/mint/Mint.HTTP.html)
"""
alias Mint.HTTP1, as: Mint
alias Arangox.{
Client,
Connection,
Endpoint,
Request,
Response
}
@behaviour Client
@impl true
def connect(%Endpoint{addr: addr, ssl?: ssl?}, opts) do
connect_timeout = Keyword.get(opts, :connect_timeout, 5_000)
transport_opts = if ssl?, do: :ssl_opts, else: :tcp_opts
transport_opts = Keyword.get(opts, transport_opts, [])
transport_opts = Keyword.merge([timeout: connect_timeout], transport_opts)
transport_opts =
if ssl?,
do: Keyword.put_new(transport_opts, :verify, :verify_none),
else: transport_opts
client_opts = Keyword.get(opts, :client_opts, [])
options = Keyword.merge([transport_opts: transport_opts], client_opts)
options = Keyword.merge(options, mode: :passive)
with(
{:ok, conn} <- open(addr, ssl?, options),
true <- Mint.open?(conn)
) do
{:ok, conn}
else
{:error, exception} ->
{:error, exception}
false ->
{:error, "connection lost"}
end
end
defp open({:unix, _path}, _ssl?, _options) do
raise ArgumentError, """
Mint doesn't support unix sockets :(
"""
end
defp open({:tcp, host, port}, ssl?, options) do
scheme = if ssl?, do: :https, else: :http
Mint.connect(scheme, host, port, options)
end
@impl true
def request(
%Request{method: method, path: path, headers: headers, body: body},
%Connection{socket: socket} = state
) do
with(
{:ok, new_socket, ref} <-
Mint.request(
socket,
method
|> to_string()
|> String.upcase(),
path,
Enum.into(headers, []),
body
),
{:ok, new_socket, buffer} <-
do_recv(new_socket, ref)
) do
do_response(ref, buffer, %{state | socket: new_socket})
else
{:error, new_socket, %_{reason: :closed}} ->
{:error, :noproc, %{state | socket: new_socket}}
{:error, new_socket, %_{reason: :closed}, _} ->
{:error, :noproc, %{state | socket: new_socket}}
{:error, new_socket, exception} ->
{:error, exception, %{state | socket: new_socket}}
{:error, new_socket, exception, _} ->
{:error, exception, %{state | socket: new_socket}}
end
end
defp do_recv(conn, ref, buffer \\ []) do
case Mint.recv(conn, 0, :infinity) do
{:ok, new_conn, next_buffer} ->
if {:done, ref} in next_buffer do
{:ok, new_conn, buffer ++ next_buffer}
else
do_recv(new_conn, ref, buffer ++ next_buffer)
end
{:error, _, _, _} = error ->
error
end
end
defp do_response(ref, buffer, state) do
case buffer do
[{:status, ^ref, status}, {:headers, ^ref, headers}, {:done, ^ref}] ->
{:ok, %Response{status: status, headers: Map.new(headers)}, state}
[{:status, ^ref, status}, {:headers, ^ref, headers}, {:data, ^ref, body}, {:done, ^ref}] ->
{:ok, %Response{status: status, headers: Map.new(headers), body: body}, state}
[{:status, ^ref, status}, {:headers, ^ref, headers} | rest_buffer] ->
body =
for kv <- rest_buffer, into: "" do
case kv do
{:data, ^ref, data} ->
data
{:done, ^ref} ->
""
end
end
{:ok, %Response{status: status, headers: Map.new(headers), body: body}, state}
end
end
@impl true
def alive?(%Connection{socket: conn}), do: Mint.open?(conn)
@impl true
def close(%Connection{socket: conn}) do
Mint.close(conn)
:ok
end
end
end