maxwell v1.0.1 Maxwell
defmodule Client do
use Maxwell.Builder, ~w(get post put)a
adapter Maxwell.Adapter.Ibrowse
middleware Maxwell.Middleware.BaseUrl, "http://example.com"
middleware Maxwell.Middleware.Opts, [connect_timeout: 1000]
middleware Maxwell.Middleware.Headers, %{'User-Agent' => "zhongwencool"}
middleware Maxwell.Middleware.EncodeJson
middleware Maxwell.Middleware.DecodeJson
# get home page
# curl --header "User-Agent: zhongwencool" http://example.com
def home do
get!
end
# get help info with path
# curl --header "User-Agent: zhongwencool" http://example.com/help
def get_help do
url("/help) |> get!
end
# get user info with query
# curl --header "User-Agent: zhongwencool" http://example.com/user?name=username
def get_user_info(username) do
url("/user") |> query(%{name: username}) |> get!
end
# post user login with json
# curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://example.com/login
def login(username, password) do
url("/login") |> body(%{username: username, password: password}) |> post!
end
# upload put multipart form
# curl --form "file=@filepath" https://example.com/upload
def upload(filepath, username) do
url("/upload") |> query(%{username: username}) |> multipart([{:file, filepath}]) |> put!
end
# cover adapter(ibrowse) connect_timeout from 1000 to 6000
def delete(username) do
url("/delete") |> query(%{username: username}) |> opts([connect_timeout: 6000]) |> delete!
end
end
Summary
Functions
Method without body: delete
Method without body: delete!
Method without body: get
Method without body: get!
Method without body: head
Method without body: head!
Method without body: options
Method without body: options!
Method: patch
Method: patch!
Method: post
Method: post!
Method: put
Method: put!
Method without body: trace
Method without body: trace!
Functions
Method without body: delete
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns {:ok, %Maxwell{}}
or {:error, reason_term}
Examples
iex> url(request_url_string_or_char_list)
|> query(request_query_map)
|> headers(request_headers_map)
|> opts(request_opts_keyword_list)
|> YourClient.delete
{:ok, %Maxwell{
headers: reponse_headers_map,
status: reponse_http_status_integer,
body: reponse_body_term,
opts: request_opts_keyword_list,
url: request_urlwithquery_string,
query: request_query_map
}
or {:error, {:conn_failed, {:error, :nxdomain}}}
You can make asynchronous requests by passing respond_to: pid
option:
Maxwell.get(url: "http://example.org", respondto: self)
receive do
{:maxwellresponse, res} -> res.status # => 200
end
Method without body: delete!
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns %Maxwell{}
or raise %MaxWell.Error{}
Method without body: get
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns {:ok, %Maxwell{}}
or {:error, reason_term}
Examples
iex> url(request_url_string_or_char_list)
|> query(request_query_map)
|> headers(request_headers_map)
|> opts(request_opts_keyword_list)
|> YourClient.get
{:ok, %Maxwell{
headers: reponse_headers_map,
status: reponse_http_status_integer,
body: reponse_body_term,
opts: request_opts_keyword_list,
url: request_urlwithquery_string,
query: request_query_map
}
or {:error, {:conn_failed, {:error, :nxdomain}}}
You can make asynchronous requests by passing respond_to: pid
option:
Maxwell.get(url: "http://example.org", respondto: self)
receive do
{:maxwellresponse, res} -> res.status # => 200
end
Method without body: get!
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns %Maxwell{}
or raise %MaxWell.Error{}
Method without body: head
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns {:ok, %Maxwell{}}
or {:error, reason_term}
Examples
iex> url(request_url_string_or_char_list)
|> query(request_query_map)
|> headers(request_headers_map)
|> opts(request_opts_keyword_list)
|> YourClient.head
{:ok, %Maxwell{
headers: reponse_headers_map,
status: reponse_http_status_integer,
body: reponse_body_term,
opts: request_opts_keyword_list,
url: request_urlwithquery_string,
query: request_query_map
}
or {:error, {:conn_failed, {:error, :nxdomain}}}
You can make asynchronous requests by passing respond_to: pid
option:
Maxwell.get(url: "http://example.org", respondto: self)
receive do
{:maxwellresponse, res} -> res.status # => 200
end
Method without body: head!
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns %Maxwell{}
or raise %MaxWell.Error{}
Method without body: options
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns {:ok, %Maxwell{}}
or {:error, reason_term}
Examples
iex> url(request_url_string_or_char_list)
|> query(request_query_map)
|> headers(request_headers_map)
|> opts(request_opts_keyword_list)
|> YourClient.options
{:ok, %Maxwell{
headers: reponse_headers_map,
status: reponse_http_status_integer,
body: reponse_body_term,
opts: request_opts_keyword_list,
url: request_urlwithquery_string,
query: request_query_map
}
or {:error, {:conn_failed, {:error, :nxdomain}}}
You can make asynchronous requests by passing respond_to: pid
option:
Maxwell.get(url: "http://example.org", respondto: self)
receive do
{:maxwellresponse, res} -> res.status # => 200
end
Method without body: options!
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns %Maxwell{}
or raise %MaxWell.Error{}
Method: patch
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list, body: body_term]
or %Maxwell{}
Returns {:ok, %Maxwell{}}
or {:error, reason}
Examples
iex> url(request_url_string_or_char_list)
|> query(request_query_map)
|> headers(request_headers_map)
|> opts(request_opts_keyword_list)
|> body(request_body_term)
|> YourClient.patch
{:ok, %Maxwell{
headers: reponse_headers_map,
status: reponse_http_status_integer,
body: reponse_body_term,
opts: request_opts_keyword_list
url: request_urlwithquery_string,
}
or
{:error, {:connfailed, {:error, :timeout}}}
If adapter supports it, you can make asynchronous requests by passing respond_to: pid
option:
Maxwell.get(url: "http://example.org", respondto: self)
receive do
{:maxwell_response, res} -> res.status # => 200
end
Method: patch!
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list, body: body_term]
or %Maxwell{}
Return %Maxwell{}
or raise %Maxwell.Error{}
Method: post
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list, body: body_term]
or %Maxwell{}
Returns {:ok, %Maxwell{}}
or {:error, reason}
Examples
iex> url(request_url_string_or_char_list)
|> query(request_query_map)
|> headers(request_headers_map)
|> opts(request_opts_keyword_list)
|> body(request_body_term)
|> YourClient.post
{:ok, %Maxwell{
headers: reponse_headers_map,
status: reponse_http_status_integer,
body: reponse_body_term,
opts: request_opts_keyword_list
url: request_urlwithquery_string,
}
or
{:error, {:connfailed, {:error, :timeout}}}
If adapter supports it, you can make asynchronous requests by passing respond_to: pid
option:
Maxwell.get(url: "http://example.org", respondto: self)
receive do
{:maxwell_response, res} -> res.status # => 200
end
Method: post!
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list, body: body_term]
or %Maxwell{}
Return %Maxwell{}
or raise %Maxwell.Error{}
Method: put
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list, body: body_term]
or %Maxwell{}
Returns {:ok, %Maxwell{}}
or {:error, reason}
Examples
iex> url(request_url_string_or_char_list)
|> query(request_query_map)
|> headers(request_headers_map)
|> opts(request_opts_keyword_list)
|> body(request_body_term)
|> YourClient.put
{:ok, %Maxwell{
headers: reponse_headers_map,
status: reponse_http_status_integer,
body: reponse_body_term,
opts: request_opts_keyword_list
url: request_urlwithquery_string,
}
or
{:error, {:connfailed, {:error, :timeout}}}
If adapter supports it, you can make asynchronous requests by passing respond_to: pid
option:
Maxwell.get(url: "http://example.org", respondto: self)
receive do
{:maxwell_response, res} -> res.status # => 200
end
Method: put!
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list, body: body_term]
or %Maxwell{}
Return %Maxwell{}
or raise %Maxwell.Error{}
Method without body: trace
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns {:ok, %Maxwell{}}
or {:error, reason_term}
Examples
iex> url(request_url_string_or_char_list)
|> query(request_query_map)
|> headers(request_headers_map)
|> opts(request_opts_keyword_list)
|> YourClient.trace
{:ok, %Maxwell{
headers: reponse_headers_map,
status: reponse_http_status_integer,
body: reponse_body_term,
opts: request_opts_keyword_list,
url: request_urlwithquery_string,
query: request_query_map
}
or {:error, {:conn_failed, {:error, :nxdomain}}}
You can make asynchronous requests by passing respond_to: pid
option:
Maxwell.get(url: "http://example.org", respondto: self)
receive do
{:maxwellresponse, res} -> res.status # => 200
end
Method without body: trace!
Receives [url: url_string, headers: headers_map, query: query_map, opts: opts_keyword_list]
or %Maxwell{}
Returns %Maxwell{}
or raise %MaxWell.Error{}