Current section

13 Versions

Jump to

Compare versions

14 files changed
+496 additions
-174 deletions
  @@ -21,12 +21,12 @@ defmodule GitHubClient do
21 21
22 22 # For a complete list of middlewares, see the docs
23 23 middleware Maxwell.Middleware.BaseUrl, "https://api.github.com"
24 - middleware Maxwell.Middleware.Headers, %{"content-type": "application/vnd.github.v3+json", "user-agent": "zhongwenool"}
24 + middleware Maxwell.Middleware.Headers, %{"content-type" => "application/vnd.github.v3+json", "user-agent" => "zhongwenool"}
25 25 middleware Maxwell.Middleware.Opts, connect_timeout: 3000
26 26 middleware Maxwell.Middleware.Json
27 27 middleware Maxwell.Middleware.Logger
28 28
29 - # adapter can be omitted, and the default will be used (currently :httpc)
29 + # adapter can be omitted, and the default will be used (currently :ibrowse)
30 30 adapter Maxwell.Adapter.Hackney
31 31
32 32 # List public repositories for the specified user.
  @@ -80,7 +80,7 @@ for a list of all functions, and detailed info about how they behave.
80 80 1. Add maxwell to your list of dependencies in `mix.exs`:
81 81 ```ex
82 82 def deps do
83 - [{:maxwell, "~> 2.2.0"}]
83 + [{:maxwell, "~> 2.2.1"}]
84 84 end
85 85 ```
86 86 2. Ensure maxwell has started before your application:
  @@ -98,22 +98,13 @@ Maxwell has support for different adapters that do the actual HTTP request proce
98 98
99 99 Maxwell has built-in support for the [httpc](http://erlang.org/doc/man/httpc.html) Erlang HTTP client.
100 100
101 - To use it simply place `adapter Maxwell.Adapter.Httpc` in your API client definition, or by
102 - setting the global default adapter, as shown below:
103 -
104 - ```ex
105 - config :maxwell,
106 - default_adapter: Maxwell.Adapter.Httpc
107 - ```
108 -
109 - **NOTE**: Remember to include `:ibrowse` in your applications list.
101 + To use it simply place `adapter Maxwell.Adapter.Httpc` in your API client definition.
110 102
111 103 ### ibrowse
112 104
113 105 Maxwell has built-in support for the [ibrowse](https://github.com/cmullaparthi/ibrowse) Erlang HTTP client.
114 106
115 - To use it simply place `adapter Maxwell.Adapter.Ibrowse` in your API client definition, or by
116 - setting the global default adapter, as shown previously.
107 + To use it simply place `adapter Maxwell.Adapter.Ibrowse` in your API client definition.
117 108
118 109 **NOTE**: Remember to include `:ibrowse` in your applications list.
119 110
  @@ -121,8 +112,7 @@ setting the global default adapter, as shown previously.
121 112
122 113 Maxwell has built-in support for the [hackney](https://github.com/benoitc/hackney) Erlang HTTP client.
123 114
124 - To use it simply place `adapter Maxwell.Adapter.Hackney` in your API client definition, or by
125 - setting the global default adapter, as shown previously.
115 + To use it simply place `adapter Maxwell.Adapter.Hackney` in your API client definition.
126 116
127 117 **NOTE**: Remember to include `:hackney` in your applications list.
  @@ -16,7 +16,7 @@
16 16 <<"lib/maxwell/middleware/middleware.ex">>,
17 17 <<"lib/maxwell/middleware/opts.ex">>,<<"lib/maxwell/middleware/rels.ex">>,
18 18 <<"lib/maxwell/middleware/retry.ex">>,<<"lib/maxwell/multipart.ex">>,
19 - <<"LICENSE">>,<<"mix.exs">>,<<"README.md">>]}.
19 + <<"lib/maxwell/query.ex">>,<<"LICENSE">>,<<"mix.exs">>,<<"README.md">>]}.
20 20 {<<"licenses">>,[<<"MIT">>]}.
21 21 {<<"links">>,[{<<"GitHub">>,<<"https://github.com/zhongwencool/maxwell">>}]}.
22 22 {<<"maintainers">>,[<<"zhongwencool">>]}.
  @@ -26,20 +26,20 @@
26 26 {<<"name">>,<<"mimerl">>},
27 27 {<<"optional">>,false},
28 28 {<<"requirement">>,<<"~> 1.0.2">>}],
29 + [{<<"app">>,<<"fuse">>},
30 + {<<"name">>,<<"fuse">>},
31 + {<<"optional">>,true},
32 + {<<"requirement">>,<<"~> 2.4">>}],
29 33 [{<<"app">>,<<"poison">>},
30 34 {<<"name">>,<<"poison">>},
31 35 {<<"optional">>,true},
32 36 {<<"requirement">>,<<"~> 2.1 or ~> 3.0">>}],
33 - [{<<"app">>,<<"ibrowse">>},
34 - {<<"name">>,<<"ibrowse">>},
35 - {<<"optional">>,true},
36 - {<<"requirement">>,<<"~> 4.2">>}],
37 37 [{<<"app">>,<<"hackney">>},
38 38 {<<"name">>,<<"hackney">>},
39 39 {<<"optional">>,true},
40 40 {<<"requirement">>,<<"~> 1.6">>}],
41 - [{<<"app">>,<<"fuse">>},
42 - {<<"name">>,<<"fuse">>},
41 + [{<<"app">>,<<"ibrowse">>},
42 + {<<"name">>,<<"ibrowse">>},
43 43 {<<"optional">>,true},
44 - {<<"requirement">>,<<"~> 2.4">>}]]}.
45 - {<<"version">>,<<"2.2.0">>}.
44 + {<<"requirement">>,<<"~> 4.2">>}]]}.
45 + {<<"version">>,<<"2.2.1">>}.
  @@ -2,16 +2,16 @@ defmodule Maxwell do
2 2 @moduledoc """
3 3 The maxwell specification.
4 4
5 - There are two kind of usages: Basic Usage and Advanced Middleware Usage.
5 + There are two kind of usages: basic usage and advanced middleware usage.
6 6
7 7 ### Basic Usage
8 8
9 9 ## Returns Origin IP, for example %{"origin" => "127.0.0.1"}
10 - Maxwell.Conn.new
11 - |> Maxwell.Conn.put_url("http://httpbin.org/ip")
12 - |> Maxwell.get!
13 - |> Maxwell.Conn.get_resp_body
14 - |> Poison.decode!
10 + "http://httpbin.org/ip"
11 + |> Maxwell.Conn.new()
12 + |> Maxwell.get!()
13 + |> Maxwell.Conn.get_resp_body()
14 + |> Poison.decode!()
15 15
16 16 Find all `get_*&put_*` helper functions by `h Maxwell.Conn.xxx`
17 17
  @@ -22,14 +22,14 @@ defmodule Maxwell do
22 22 adapter Maxwell.Adapter.Ibrowse
23 23
24 24 middleware Maxwell.Middleware.BaseUrl, "http://httpbin.org"
25 - middleware Maxwell.Middleware.Opts, [connect_timeout: 1000]
26 - middleware Maxwell.Middleware.Headers, %{'User-Agent' => "zhongwencool"}
25 + middleware Maxwell.Middleware.Opts, [connect_timeout: 5000]
26 + middleware Maxwell.Middleware.Headers, %{"User-Agent" => "zhongwencool"}
27 27 middleware Maxwell.Middleware.Json
28 28
29 29 ## Returns origin IP, for example "127.0.0.1"
30 - def ip do
31 - new()
32 - |> put_path("ip")
30 + def ip() do
31 + "/ip"
32 + |> new()
33 33 |> get!()
34 34 |> get_resp_body("origin")
35 35 end
  @@ -37,8 +37,8 @@ defmodule Maxwell do
37 37 ## Generates n random bytes of binary data, accepts optional seed integer parameter
38 38 def get_random_bytes(size) do
39 39 "/bytes/\#\{size\}"
40 - |> put_path
41 - |> get!
40 + |> new()
41 + |> get!()
42 42 |> get_resp_body(&to_string/1)
43 43 end
44 44 end
  @@ -50,7 +50,12 @@ defmodule Maxwell.Adapter.Httpc do
50 50 query_string: query_string, path: path,
51 51 method: method, opts: opts, req_body: req_body} = conn
52 52 url = Util.url_serialize(url, path, query_string, :char_list)
53 - req_body = {:chunkify, &Util.stream_iterate/1, req_body}
53 + chunked = Util.chunked?(conn)
54 + req_body =
55 + case chunked do
56 + true -> {:chunkify, &Util.stream_iterate/1, req_body}
57 + false -> {&Util.stream_iterate/1, req_body}
58 + end
54 59 {content_type, req_headers} = header_serialize(req_headers)
55 60 {http_opts, options} = opts_serialize(opts)
56 61 result = request(method, url, req_headers, content_type, req_body, http_opts, options)
  @@ -50,7 +50,6 @@ if Code.ensure_loaded?(:ibrowse) do
50 50 method: method, opts: opts, req_body: req_body} = conn
51 51 url = Util.url_serialize(url, path, query_string, :char_list)
52 52 req_headers = Util.header_serialize(req_headers)
53 - opts = Keyword.put(opts, :transfer_encoding, :chunked)
54 53 req_body = {&Util.stream_iterate/1, req_body}
55 54 result = :ibrowse.send_req(url, req_headers, method, req_body, opts)
56 55 format_response(result, conn)
Loading more files…