-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathadapter.ex
141 lines (117 loc) · 5.53 KB
/
adapter.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
defmodule Dayron.Adapter do
@moduledoc ~S"""
Behaviour for creating Dayron Adapters
Adapters are wrappers around client libraries, responsible to send HTTP
requests and parse the response status and body.
## Example
defmodule Dayron.CustomAdapter do
@behaviour Dayron.Adapter
def get(url, headers, opts) do
make_a_get_request(url, headers, opts)
end
end
"""
alias Dayron.Response
alias Dayron.ClientError
@type headers :: [{binary, binary}] | %{binary => binary}
@type body :: struct
@type response :: {:ok, Response.t} | {:error, ClientError.t}
@doc """
Issues a GET request to the given url. The headers param is an enumerable
consisting of two-item tuples that will be sent as request headers.
Returns `{:ok, response}` if the request is successful,
`{:error, reason}` otherwise.
## Options:
* `:timeout` - timeout to establish a connection, in milliseconds.
* `:recv_timeout` - timeout used when receiving a connection.
* `:stream_to` - a PID to stream the response to
* `:proxy` - a proxy to be used for the request;
it can be a regular url or a `{Host, Proxy}` tuple
* `:proxy_auth` - proxy authentication `{User, Password}` tuple
* `:ssl` - SSL options supported by the `ssl` erlang module
* `:follow_redirect` - a boolean that causes redirects to be followed
* `:max_redirect` - the maximum number of redirects to follow
* `:params` - an enumerable consisting of two-item tuples that will be
appended to the url as query string parameters
Timeouts can be an integer or `:infinity`.
Check the adapter implementations for default values.
"""
@callback get(binary, headers, Keyword.t) :: response
@doc """
Issues a POST request to the given url.
Returns `{:ok, response}` if the request is successful,
`{:error, reason}` otherwise.
## Arguments:
* `url` - target url as a binary string or char list
* `body` - request body. Usually a struct deriving `Poison.Encoder`
* `headers` - HTTP headers as an orddict
(e.g., `[{"Accept", "application/json"}]`)
* `options` - Keyword list of options
## Options:
* `:timeout` - timeout to establish a connection, in milliseconds.
* `:recv_timeout` - timeout used when receiving a connection.
* `:stream_to` - a PID to stream the response to
* `:proxy` - a proxy to be used for the request;
it can be a regular url or a `{Host, Proxy}` tuple
* `:proxy_auth` - proxy authentication `{User, Password}` tuple
* `:ssl` - SSL options supported by the `ssl` erlang module
* `:follow_redirect` - a boolean that causes redirects to be followed
* `:max_redirect` - the maximum number of redirects to follow
* `:params` - an enumerable consisting of two-item tuples that will be
appended to the url as query string parameters
Timeouts can be an integer or `:infinity`.
Check the adapter implementations for default values.
"""
@callback post(binary, body, headers, Keyword.t) :: response
@doc """
Issues a PATCH request to the given url.
Returns `{:ok, response}` if the request is successful,
`{:error, reason}` otherwise.
## Arguments:
* `url` - target url as a binary string or char list
* `body` - request body. Usually a struct deriving `Poison.Encoder`
* `headers` - HTTP headers as an orddict
(e.g., `[{"Accept", "application/json"}]`)
* `options` - Keyword list of options
## Options:
* `:timeout` - timeout to establish a connection, in milliseconds.
* `:recv_timeout` - timeout used when receiving a connection.
* `:stream_to` - a PID to stream the response to
* `:proxy` - a proxy to be used for the request;
it can be a regular url or a `{Host, Proxy}` tuple
* `:proxy_auth` - proxy authentication `{User, Password}` tuple
* `:ssl` - SSL options supported by the `ssl` erlang module
* `:follow_redirect` - a boolean that causes redirects to be followed
* `:max_redirect` - the maximum number of redirects to follow
* `:params` - an enumerable consisting of two-item tuples that will be
appended to the url as query string parameters
Timeouts can be an integer or `:infinity`.
Check the adapter implementations for default values.
"""
@callback patch(binary, body, headers, Keyword.t) :: response
@doc """
Issues a DELETE request to the given url.
Returns `{:ok, response}` if the request is successful,
`{:error, reason}` otherwise.
## Arguments:
* `url` - target url as a binary string or char list
* `headers` - HTTP headers as an orddict
(e.g., `[{"Accept", "application/json"}]`)
* `options` - Keyword list of options
## Options:
* `:timeout` - timeout to establish a connection, in milliseconds.
* `:recv_timeout` - timeout used when receiving a connection.
* `:stream_to` - a PID to stream the response to
* `:proxy` - a proxy to be used for the request;
it can be a regular url or a `{Host, Proxy}` tuple
* `:proxy_auth` - proxy authentication `{User, Password}` tuple
* `:ssl` - SSL options supported by the `ssl` erlang module
* `:follow_redirect` - a boolean that causes redirects to be followed
* `:max_redirect` - the maximum number of redirects to follow
* `:params` - an enumerable consisting of two-item tuples that will be
appended to the url as query string parameters
Timeouts can be an integer or `:infinity`.
Check the adapter implementations for default values.
"""
@callback delete(binary, headers, Keyword.t) :: response
end