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.

142 lines
4.3 KiB

  1. %% Vendored from hex_core v0.6.8, do not edit manually
  2. %% @hidden
  3. -module(r3_hex_api).
  4. -export([
  5. delete/2,
  6. get/2,
  7. post/3,
  8. put/3,
  9. encode_query_string/1,
  10. build_repository_path/2,
  11. build_organization_path/2,
  12. join_path_segments/1
  13. ]).
  14. -define(ERL_CONTENT_TYPE, <<"application/vnd.hex+erlang">>).
  15. -export_type([body/0, response/0]).
  16. -type response() :: {ok, {r3_hex_http:status(), r3_hex_http:headers(), body() | nil}} | {error, term()}.
  17. -type body() :: [body()] | #{binary() => body() | binary()}.
  18. get(Config, Path) ->
  19. request(Config, get, Path, undefined).
  20. post(Config, Path, Body) ->
  21. request(Config, post, Path, encode_body(Body)).
  22. put(Config, Path, Body) ->
  23. request(Config, put, Path, encode_body(Body)).
  24. delete(Config, Path) ->
  25. request(Config, delete, Path, undefined).
  26. %% @private
  27. encode_query_string(List) ->
  28. Pairs = lists:map(fun ({K, V}) -> {to_list(K), to_list(V)} end, List),
  29. list_to_binary(compose_query(Pairs)).
  30. %% OTP 21+
  31. -ifdef (OTP_RELEASE).
  32. compose_query(Pairs) ->
  33. uri_string:compose_query(Pairs).
  34. -else.
  35. compose_query(Pairs) ->
  36. String = join("&", lists:map(fun ({K, V}) -> K ++ "=" ++ V end, Pairs)),
  37. http_uri:encode(String).
  38. -endif.
  39. %% @private
  40. build_repository_path(#{api_repository := Repo}, Path) when is_binary(Repo) ->
  41. ["repos", Repo | Path];
  42. build_repository_path(#{api_repository := undefined}, Path) ->
  43. Path.
  44. %% @private
  45. build_organization_path(#{api_organization := Org}, Path) when is_binary(Org) ->
  46. ["orgs", Org | Path];
  47. build_organization_path(#{api_organization := undefined}, Path) ->
  48. Path.
  49. %% @private
  50. join_path_segments(Segments) ->
  51. iolist_to_binary(recompose(Segments)).
  52. %% OTP 21+
  53. -ifdef (OTP_RELEASE).
  54. recompose(Segments) ->
  55. Concatenated = join(<<"/">>, Segments),
  56. %% uri_string:recompose/1 accepts path segments as a list,
  57. %% both strings and binaries
  58. uri_string:recompose(#{path => Concatenated}).
  59. -else.
  60. recompose(Segments) ->
  61. join(<<"/">>, lists:map(fun encode_segment/1, Segments)).
  62. encode_segment(Binary) when is_binary(Binary) ->
  63. encode_segment(binary_to_list(Binary));
  64. encode_segment(String) when is_list(String) ->
  65. http_uri:encode(String).
  66. -endif.
  67. %%====================================================================
  68. %% Internal functions
  69. %%====================================================================
  70. request(Config, Method, PathSegments, Body) when is_list(PathSegments) ->
  71. Path = join_path_segments(PathSegments),
  72. request(Config, Method, Path, Body);
  73. request(Config, Method, Path, Body) when is_binary(Path) and is_map(Config) ->
  74. DefaultHeaders = make_headers(Config),
  75. ReqHeaders = maps:merge(maps:get(http_headers, Config, #{}), DefaultHeaders),
  76. ReqHeaders2 = put_new(<<"accept">>, ?ERL_CONTENT_TYPE, ReqHeaders),
  77. case r3_hex_http:request(Config, Method, build_url(Path, Config), ReqHeaders2, Body) of
  78. {ok, {Status, RespHeaders, RespBody}} ->
  79. ContentType = maps:get(<<"content-type">>, RespHeaders, <<"">>),
  80. case binary:match(ContentType, ?ERL_CONTENT_TYPE) of
  81. {_, _} ->
  82. {ok, {Status, RespHeaders, binary_to_term(RespBody)}};
  83. nomatch ->
  84. {ok, {Status, RespHeaders, nil}}
  85. end;
  86. Other ->
  87. Other
  88. end.
  89. build_url(Path, #{api_url := URI}) ->
  90. <<URI/binary, "/", Path/binary>>.
  91. encode_body({_ContentType, _Body} = Body) ->
  92. Body;
  93. encode_body(Body) ->
  94. {binary_to_list(?ERL_CONTENT_TYPE), term_to_binary(Body)}.
  95. %% TODO: copy-pasted from r3_hex_repo
  96. make_headers(Config) ->
  97. maps:fold(fun set_header/3, #{}, Config).
  98. set_header(api_key, Token, Headers) when is_binary(Token) -> maps:put(<<"authorization">>, Token, Headers);
  99. set_header(_, _, Headers) -> Headers.
  100. put_new(Key, Value, Map) ->
  101. case maps:find(Key, Map) of
  102. {ok, _} -> Map;
  103. error -> maps:put(Key, Value, Map)
  104. end.
  105. %% https://github.com/erlang/otp/blob/OTP-20.3/lib/stdlib/src/lists.erl#L1449:L1453
  106. join(_Sep, []) -> [];
  107. join(Sep, [H|T]) -> [H|join_prepend(Sep, T)].
  108. join_prepend(_Sep, []) -> [];
  109. join_prepend(Sep, [H|T]) -> [Sep,H|join_prepend(Sep,T)].
  110. to_list(A) when is_atom(A) -> atom_to_list(A);
  111. to_list(B) when is_binary(B) -> unicode:characters_to_list(B);
  112. to_list(I) when is_integer(I) -> integer_to_list(I);
  113. to_list(Str) -> unicode:characters_to_list(Str).