Skip to content

Commit aa33cc8

Browse files
michaelklishinwojtekmach
authored andcommitted
hex_api: transition away from http_uri (#82)
1 parent 306e6a6 commit aa33cc8

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/hex_api.erl

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ delete(Config, Path) ->
3333

3434
%% @private
3535
encode_query_string(List) ->
36-
QueryString =
37-
join("&",
38-
lists:map(fun
39-
({K, V}) when is_atom(V) ->
40-
atom_to_list(K) ++ "=" ++ atom_to_list(V);
41-
({K, V}) when is_binary(V) ->
42-
atom_to_list(K) ++ "=" ++ binary_to_list(V);
43-
({K, V}) when is_integer(V) ->
44-
atom_to_list(K) ++ "=" ++ integer_to_list(V)
45-
end, List)),
46-
Encoded = http_uri:encode(QueryString),
47-
list_to_binary(Encoded).
36+
Pairs = lists:map(fun ({K, V}) -> {to_list(K), to_list(V)} end, List),
37+
list_to_binary(compose_query(Pairs)).
38+
39+
%% OTP 21+
40+
-ifdef (OTP_RELEASE).
41+
compose_query(Pairs) ->
42+
uri_string:compose_query(Pairs).
43+
-else.
44+
compose_query(Pairs) ->
45+
String = join("&", lists:map(fun ({K, V}) -> K ++ "=" ++ V end, Pairs)),
46+
http_uri:encode(String).
47+
-endif.
4848

4949
%% @private
5050
build_repository_path(#{api_repository := Repo}, Path) when is_binary(Repo) ->
@@ -60,7 +60,24 @@ build_organization_path(#{api_organization := undefined}, Path) ->
6060

6161
%% @private
6262
join_path_segments(Segments) ->
63-
erlang:iolist_to_binary(join(<<"/">>, lists:map(fun encode/1, Segments))).
63+
iolist_to_binary(recompose(Segments)).
64+
65+
%% OTP 21+
66+
-ifdef (OTP_RELEASE).
67+
recompose(Segments) ->
68+
Concatenated = join(<<"/">>, Segments),
69+
%% uri_string:recompose/1 accepts path segments as a list,
70+
%% both strings and binaries
71+
uri_string:recompose(#{path => Concatenated}).
72+
-else.
73+
recompose(Segments) ->
74+
join(<<"/">>, lists:map(fun encode_segment/1, Segments)).
75+
76+
encode_segment(Binary) when is_binary(Binary) ->
77+
encode_segment(binary_to_list(Binary));
78+
encode_segment(String) when is_list(String) ->
79+
http_uri:encode(String).
80+
-endif.
6481

6582
%%====================================================================
6683
%% Internal functions
@@ -89,11 +106,6 @@ request(Config, Method, Path, Body) when is_binary(Path) and is_map(Config) ->
89106
Other
90107
end.
91108

92-
encode(Binary) when is_binary(Binary) ->
93-
encode(binary_to_list(Binary));
94-
encode(String) when is_list(String) ->
95-
http_uri:encode(String).
96-
97109
build_url(Path, #{api_url := URI}) ->
98110
<<URI/binary, "/", Path/binary>>.
99111

@@ -121,3 +133,8 @@ join(Sep, [H|T]) -> [H|join_prepend(Sep, T)].
121133

122134
join_prepend(_Sep, []) -> [];
123135
join_prepend(Sep, [H|T]) -> [Sep,H|join_prepend(Sep,T)].
136+
137+
to_list(A) when is_atom(A) -> atom_to_list(A);
138+
to_list(B) when is_binary(B) -> unicode:characters_to_list(B);
139+
to_list(I) when is_integer(I) -> integer_to_list(I);
140+
to_list(Str) -> unicode:characters_to_list(Str).

0 commit comments

Comments
 (0)