Skip to content

Commit

Permalink
Merge pull request #311 from ryanwinchester/master
Browse files Browse the repository at this point in the history
[Proposal] Adding Request struct and process_response/1
  • Loading branch information
edgurgel authored Oct 14, 2018
2 parents da41878 + cd5f6b6 commit 0ad936f
Show file tree
Hide file tree
Showing 3 changed files with 460 additions and 123 deletions.
66 changes: 64 additions & 2 deletions lib/httpoison.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
defmodule HTTPoison.Request do
@moduledoc """
`Request` properties:
* `:method` - HTTP method as an atom (`:get`, `:head`, `:post`, `:put`,
`:delete`, etc.)
* `:url` - target url as a binary string or char list
* `:body` - request body. See more below
* `:headers` - HTTP headers as an orddict (e.g., `[{"Accept", "application/json"}]`)
* `:options` - Keyword list of options
* `:params` - Query parameters as a map, keyword, or orddict
`:body`:
* binary, char list or an iolist
* `{:form, [{K, V}, ...]}` - send a form url encoded
* `{:file, "/path/to/file"}` - send a file
* `{:stream, enumerable}` - lazily send a stream of binaries/charlists
`:options`:
* `:timeout` - timeout for establishing a TCP or SSL connection, in milliseconds. Default is 8000
* `:recv_timeout` - timeout for receiving an HTTP response from the socket. Default is 5000
* `:stream_to` - a PID to stream the response to
* `:async` - if given `:once`, will only stream one message at a time, requires call to `stream_next`
* `:proxy` - a proxy to be used for the request; it can be a regular url
or a `{Host, Port}` tuple, or a `{:socks5, ProxyHost, ProxyPort}` tuple
* `:proxy_auth` - proxy authentication `{User, Password}` tuple
* `:socks5_user`- socks5 username
* `:socks5_pass`- socks5 password
* `:ssl` - SSL options supported by the `ssl` erlang module
* `:follow_redirect` - a boolean that causes redirects to be followed
* `:max_redirect` - an integer denoting 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
* `:max_body_length` - a non-negative integer denoting the max response body length. See :hackney.body/2
Timeouts can be an integer or `:infinity`
"""
@enforce_keys [:url]
defstruct method: :get, url: nil, headers: [], body: "", params: %{}, options: []

@type method :: :get | :post | :put | :patch | :delete | :options | :head
@type headers :: [{atom, binary}] | [{binary, binary}] | %{binary => binary}
@type url :: binary
@type body :: binary | {:form, [{atom, any}]} | {:file, binary}
@type params :: map | keyword | [{binary, binary}]
@type options :: keyword

@type t :: %__MODULE__{
method: method,
url: binary,
headers: headers,
body: body,
params: params,
options: options
}
end

defmodule HTTPoison.Response do
defstruct status_code: nil, body: nil, headers: [], request_url: nil
@type t :: %__MODULE__{status_code: integer, body: term, headers: list}
defstruct status_code: nil, body: nil, headers: [], request_url: nil, request: nil

@type t :: %__MODULE__{
status_code: integer,
body: term,
headers: list,
request: HTTPoison.Request.t(),
request_url: HTTPoison.Request.url()
}
end

defmodule HTTPoison.AsyncResponse do
Expand Down
Loading

0 comments on commit 0ad936f

Please sign in to comment.