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

body(maxwell \\ %Maxwell{}, body)
delete(maxwell \\ [])

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

delete!(maxwell \\ %Maxwell{})

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{}

get(maxwell \\ [])

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

get!(maxwell \\ %Maxwell{})

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{}

head(maxwell \\ [])

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

head!(maxwell \\ %Maxwell{})

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{}

headers(maxwell \\ %Maxwell{}, headers)
multipart(maxwell \\ %Maxwell{}, multipart)
options(maxwell \\ [])

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

options!(maxwell \\ %Maxwell{})

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{}

opts(maxwell \\ %Maxwell{}, opts)
patch(maxwell \\ %Maxwell{})

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

patch!(maxwell \\ %Maxwell{})

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{}

post(maxwell \\ %Maxwell{})

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

post!(maxwell \\ %Maxwell{})

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{}

put(maxwell \\ %Maxwell{})

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

put!(maxwell \\ %Maxwell{})

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{}

query(maxwell \\ %Maxwell{}, query)
respond_to(target_pid)
respond_to(maxwell, target_pid)
trace(maxwell \\ [])

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

trace!(maxwell \\ %Maxwell{})

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{}

url(maxwell \\ %Maxwell{}, url)