Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support httpc profile through adapter configs #36

Merged
merged 1 commit into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hex_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ default_config() ->
#{
api_url => <<"https://hex.pm/api">>,
http_adapter => hex_http_httpc,
http_adapter_config => #{profile => default},
http_user_agent_fragment => <<"(httpc)">>,
repo_url => <<"https://repo.hex.pm">>,
repo_public_key => ?HEXPM_PUBLIC_KEY,
Expand Down
6 changes: 4 additions & 2 deletions src/hex_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
-type status() :: non_neg_integer().
-type headers() :: #{binary() => binary()}.
-type body() :: nil.
-type adapter_config() :: map().

-callback request(method(), URI :: binary(), headers(), body()) ->
-callback request(method(), URI :: binary(), headers(), body(), adapter_config()) ->
{ok, status(), headers(), binary()} |
{error, term()}.

Expand All @@ -20,7 +21,8 @@ request(Config, Method, URI, Headers, Body) when is_binary(URI) and is_map(Heade
Adapter = maps:get(http_adapter, Config),
UserAgentFragment = maps:get(http_user_agent_fragment, Config),
Headers2 = put_new(<<"user-agent">>, user_agent(UserAgentFragment), Headers),
Adapter:request(Method, URI, Headers2, Body).
AdapterConfig = maps:get(http_adapter_config, Config, #{}),
Adapter:request(Method, URI, Headers2, Body, AdapterConfig).

user_agent(UserAgentFragment) ->
OTPRelease = erlang:system_info(otp_release),
Expand Down
7 changes: 4 additions & 3 deletions src/hex_http_httpc.erl
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
-module(hex_http_httpc).
-behaviour(hex_http).
-export([request/4]).
-export([request/5]).

%%====================================================================
%% API functions
%%====================================================================

request(Method, URI, ReqHeaders, Body) ->
request(Method, URI, ReqHeaders, Body, AdapterConfig) ->
Profile = maps:get(profile, AdapterConfig, default),
Request = build_request(URI, ReqHeaders, Body),
{ok, {{_, StatusCode, _}, RespHeaders, RespBody}} =
httpc:request(Method, Request, [], [{body_format, binary}]),
httpc:request(Method, Request, [], [{body_format, binary}], Profile),
RespHeaders2 = load_headers(RespHeaders),
{ok, {StatusCode, RespHeaders2, RespBody}}.

Expand Down
4 changes: 2 additions & 2 deletions test/support/hex_http_test.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(hex_http_test).
-behaviour(hex_http).
-export([request/4]).
-export([request/5]).
-define(TEST_REPO_URL, "https://repo.test").
-define(TEST_API_URL, "https://api.test").
-define(PRIVATE_KEY, hex_test_helpers:fixture("test_priv.pem")).
Expand All @@ -10,7 +10,7 @@
%% API functions
%%====================================================================

request(Method, URI, Headers, Body) when is_binary(URI) and is_map(Headers) ->
request(Method, URI, Headers, Body, _Options) when is_binary(URI) and is_map(Headers) ->
fixture(Method, URI, Headers, Body).

%%====================================================================
Expand Down