-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce hex_core HTTP adapter for rebar3 (3.13.x)
Based on #2561, an adaptation of 751fa91 for rebar3 3.13.x. Per https://ferd.ca/you-ve-got-to-upgrade-rebar3.html: > [The patch](17848f4#diff-4b5af60b80cd23cfd32adbf1b5e7647c8d89a8d7046c89cee2be5a5ae14dc95fR14) is not that complex on new versions and should be relatively easy to port to older versions if anyone ends up wanting to help. This should work for anyone on rebar3 3.13.2 either due to running an older OTP or still needing the old pre-compile hook behavior (#2211 (comment)).
- Loading branch information
1 parent
73c3613
commit 24c5980
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
%% Derived from hex_core v0.5.1 for extra flexibility. | ||
|
||
-module(rebar_httpc_adapter). | ||
-behaviour(r3_hex_http). | ||
-export([request/5]). | ||
|
||
%%==================================================================== | ||
%% API functions | ||
%%==================================================================== | ||
|
||
request(Method, URI, ReqHeaders, Body, AdapterConfig) -> | ||
Profile = maps:get(profile, AdapterConfig, default), | ||
Request = build_request(URI, ReqHeaders, Body), | ||
SSLOpts = [{ssl, rebar_utils:ssl_opts(URI)}], | ||
case httpc:request(Method, Request, SSLOpts, [{body_format, binary}], Profile) of | ||
{ok, {{_, StatusCode, _}, RespHeaders, RespBody}} -> | ||
RespHeaders2 = load_headers(RespHeaders), | ||
{ok, {StatusCode, RespHeaders2, RespBody}}; | ||
{error, Reason} -> {error, Reason} | ||
end. | ||
|
||
%%==================================================================== | ||
%% Internal functions | ||
%%==================================================================== | ||
|
||
build_request(URI, ReqHeaders, Body) -> | ||
build_request2(binary_to_list(URI), dump_headers(ReqHeaders), Body). | ||
|
||
build_request2(URI, ReqHeaders, undefined) -> | ||
{URI, ReqHeaders}; | ||
build_request2(URI, ReqHeaders, {ContentType, Body}) -> | ||
{URI, ReqHeaders, ContentType, Body}. | ||
|
||
dump_headers(Map) -> | ||
maps:fold(fun(K, V, Acc) -> | ||
[{binary_to_list(K), binary_to_list(V)} | Acc] end, [], Map). | ||
|
||
load_headers(List) -> | ||
lists:foldl(fun({K, V}, Acc) -> | ||
maps:put(list_to_binary(K), list_to_binary(V), Acc) end, #{}, List). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters