You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 line
1.5 KiB

  1. %% Vendored from hex_core v0.5.1, do not edit manually
  2. %% @hidden
  3. -module(r3_hex_http_httpc).
  4. -behaviour(r3_hex_http).
  5. -export([request/5]).
  6. %%====================================================================
  7. %% API functions
  8. %%====================================================================
  9. request(Method, URI, ReqHeaders, Body, AdapterConfig) ->
  10. Profile = maps:get(profile, AdapterConfig, default),
  11. Request = build_request(URI, ReqHeaders, Body),
  12. case httpc:request(Method, Request, [{ssl, rebar_utils:ssl_opts(URI)}],
  13. [{body_format, binary}], Profile) of
  14. {ok, {{_, StatusCode, _}, RespHeaders, RespBody}} ->
  15. RespHeaders2 = load_headers(RespHeaders),
  16. {ok, {StatusCode, RespHeaders2, RespBody}};
  17. {error, Reason} -> {error, Reason}
  18. end.
  19. %%====================================================================
  20. %% Internal functions
  21. %%====================================================================
  22. build_request(URI, ReqHeaders, Body) ->
  23. build_request2(binary_to_list(URI), dump_headers(ReqHeaders), Body).
  24. build_request2(URI, ReqHeaders, undefined) ->
  25. {URI, ReqHeaders};
  26. build_request2(URI, ReqHeaders, {ContentType, Body}) ->
  27. {URI, ReqHeaders, ContentType, Body}.
  28. dump_headers(Map) ->
  29. maps:fold(fun(K, V, Acc) ->
  30. [{binary_to_list(K), binary_to_list(V)} | Acc] end, [], Map).
  31. load_headers(List) ->
  32. lists:foldl(fun({K, V}, Acc) ->
  33. maps:put(list_to_binary(K), list_to_binary(V), Acc) end, #{}, List).