Skip to content

Commit

Permalink
Merge pull request #2213 from starbelly/update-hex-core-to-v0.6.5
Browse files Browse the repository at this point in the history
Update hex core to v0.6.8
  • Loading branch information
ferd authored Feb 5, 2020
2 parents a524fe3 + 46765c8 commit 310a4ad
Show file tree
Hide file tree
Showing 34 changed files with 1,034 additions and 637 deletions.
16 changes: 14 additions & 2 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"1.1.0",
{"1.2.0",
[{<<"bbmustache">>,{pkg,<<"bbmustache">>,<<"1.8.0">>},0},
{<<"certifi">>,{pkg,<<"certifi">>,<<"2.5.1">>},0},
{<<"cf">>,{pkg,<<"cf">>,<<"0.2.2">>},0},
Expand All @@ -22,5 +22,17 @@
{<<"parse_trans">>, <<"09765507A3C7590A784615CFD421D101AEC25098D50B89D7AA1D66646BC571C1">>},
{<<"providers">>, <<"70B4197869514344A8A60E2B2A4EF41CA03DEF43CFB1712ECF076A0F3C62F083">>},
{<<"relx">>, <<"AFC019320BB69881718576B3E4E1EB548C1FA3270717BA66A78004C98A77CD17">>},
{<<"ssl_verify_fun">>, <<"6EAF7AD16CB568BB01753DBBD7A95FF8B91C7979482B95F38443FE2C8852A79B">>}]}
{<<"ssl_verify_fun">>, <<"6EAF7AD16CB568BB01753DBBD7A95FF8B91C7979482B95F38443FE2C8852A79B">>}]},
{pkg_hash_ext,[
{<<"bbmustache">>, <<"190EA2206128BDFABF5D9200B8DF97F6511D9C62953655828E28C2BC79161252">>},
{<<"certifi">>, <<"805ABD97539CAF89EC6D4732C91E62BA9DA0CDA51AC462380BBD28EE697A8C42">>},
{<<"cf">>, <<"48283B3019BC7FAD56E7B23028A5DA4D3E6CD598A553AB2A99A2153BF5F19B21">>},
{<<"cth_readable">>, <<"8F799D7BFD444ABFE2B4C07CCB31C56E80043335E1ED3933A2DFBA35F8D97523">>},
{<<"erlware_commons">>, <<"7AADA93F368D0A0430122E39931B7FB4AC9E94DBF043CDC980AD4330FD9CD166">>},
{<<"eunit_formatters">>, <<"D6C8BA213424944E6E05BBC097C32001CDD0ABE3925D02454F229B20D68763C9">>},
{<<"getopt">>, <<"53E1AB83B9CEB65C9672D3E7A35B8092E9BDC9B3EE80721471A161C10C59959C">>},
{<<"parse_trans">>, <<"17EF63ABDE837AD30680EA7F857DD9E7CED9476CDD7B0394432AF4BFC241B960">>},
{<<"providers">>, <<"E45745ADE9C476A9A469EA0840E418AB19360DC44F01A233304E118A44486BA0">>},
{<<"relx">>, <<"6E0456139FC70BADE0C45FF8A8197C5E879A57FD792F771FC632B94C5AEC1EAC">>},
{<<"ssl_verify_fun">>, <<"13104D7897E38ED7F044C4DE953A6C28597D1C952075EB2E328BC6D6F2BFC496">>}]}
].
70 changes: 43 additions & 27 deletions src/r3_hex_api.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.5.1, do not edit manually
%% Vendored from hex_core v0.6.8, do not edit manually

%% @hidden

Expand All @@ -16,6 +16,11 @@
]).
-define(ERL_CONTENT_TYPE, <<"application/vnd.hex+erlang">>).

-export_type([body/0, response/0]).

-type response() :: {ok, {r3_hex_http:status(), r3_hex_http:headers(), body() | nil}} | {error, term()}.
-type body() :: [body()] | #{binary() => body() | binary()}.

get(Config, Path) ->
request(Config, get, Path, undefined).

Expand All @@ -28,26 +33,20 @@ put(Config, Path, Body) ->
delete(Config, Path) ->
request(Config, delete, Path, undefined).

-ifdef (OTP_RELEASE).
-if(?OTP_RELEASE >= 23).
-compile({nowarn_deprecated_function, [{http_uri, encode, 1}]}).
-endif.
-endif.

%% @private
encode_query_string(List) ->
QueryString =
join("&",
lists:map(fun
({K, V}) when is_atom(V) ->
atom_to_list(K) ++ "=" ++ atom_to_list(V);
({K, V}) when is_binary(V) ->
atom_to_list(K) ++ "=" ++ binary_to_list(V);
({K, V}) when is_integer(V) ->
atom_to_list(K) ++ "=" ++ integer_to_list(V)
end, List)),
Encoded = http_uri:encode(QueryString),
list_to_binary(Encoded).
Pairs = lists:map(fun ({K, V}) -> {to_list(K), to_list(V)} end, List),
list_to_binary(compose_query(Pairs)).

%% OTP 21+
-ifdef (OTP_RELEASE).
compose_query(Pairs) ->
uri_string:compose_query(Pairs).
-else.
compose_query(Pairs) ->
String = join("&", lists:map(fun ({K, V}) -> K ++ "=" ++ V end, Pairs)),
http_uri:encode(String).
-endif.

%% @private
build_repository_path(#{api_repository := Repo}, Path) when is_binary(Repo) ->
Expand All @@ -63,7 +62,24 @@ build_organization_path(#{api_organization := undefined}, Path) ->

%% @private
join_path_segments(Segments) ->
erlang:iolist_to_binary(join(<<"/">>, lists:map(fun encode/1, Segments))).
iolist_to_binary(recompose(Segments)).

%% OTP 21+
-ifdef (OTP_RELEASE).
recompose(Segments) ->
Concatenated = join(<<"/">>, Segments),
%% uri_string:recompose/1 accepts path segments as a list,
%% both strings and binaries
uri_string:recompose(#{path => Concatenated}).
-else.
recompose(Segments) ->
join(<<"/">>, lists:map(fun encode_segment/1, Segments)).

encode_segment(Binary) when is_binary(Binary) ->
encode_segment(binary_to_list(Binary));
encode_segment(String) when is_list(String) ->
http_uri:encode(String).
-endif.

%%====================================================================
%% Internal functions
Expand All @@ -78,25 +94,20 @@ request(Config, Method, Path, Body) when is_binary(Path) and is_map(Config) ->
ReqHeaders2 = put_new(<<"accept">>, ?ERL_CONTENT_TYPE, ReqHeaders),

case r3_hex_http:request(Config, Method, build_url(Path, Config), ReqHeaders2, Body) of
{ok, {Status, RespHeaders, RespBody}} = Response ->
{ok, {Status, RespHeaders, RespBody}} ->
ContentType = maps:get(<<"content-type">>, RespHeaders, <<"">>),
case binary:match(ContentType, ?ERL_CONTENT_TYPE) of
{_, _} ->
{ok, {Status, RespHeaders, binary_to_term(RespBody)}};

nomatch ->
Response
{ok, {Status, RespHeaders, nil}}
end;

Other ->
Other
end.

encode(Binary) when is_binary(Binary) ->
encode(binary_to_list(Binary));
encode(String) when is_list(String) ->
http_uri:encode(String).

build_url(Path, #{api_url := URI}) ->
<<URI/binary, "/", Path/binary>>.

Expand Down Expand Up @@ -124,3 +135,8 @@ join(Sep, [H|T]) -> [H|join_prepend(Sep, T)].

join_prepend(_Sep, []) -> [];
join_prepend(Sep, [H|T]) -> [Sep,H|join_prepend(Sep,T)].

to_list(A) when is_atom(A) -> atom_to_list(A);
to_list(B) when is_binary(B) -> unicode:characters_to_list(B);
to_list(I) when is_integer(I) -> integer_to_list(I);
to_list(Str) -> unicode:characters_to_list(Str).
136 changes: 132 additions & 4 deletions src/r3_hex_api_key.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.5.1, do not edit manually
%% Vendored from hex_core v0.6.8, do not edit manually

-module(r3_hex_api_key).
-export([
Expand All @@ -9,23 +9,151 @@
delete_all/1
]).

-export_type([permission/0]).

-type permission() :: api_permission() | repo_permission() | repos_permission().
-ifdef(OTP_19).
-type api_permission() :: #{domain := api, resource => read | write}.
-type repo_permission() :: #{domain := repository, resource := binary()}.
-type repos_permission() :: #{domain := repositories}.
-else.
-type api_permission() :: #{domain => api, resource => read | write}.
-type repo_permission() :: #{domain => repository, resource => binary()}.
-type repos_permission() :: #{domain => repositories}.
-endif.

%% @doc
%% Lists the user's or organization's API and repository keys.
%%
%% Examples:
%%
%% ```
%% > r3_hex_api_key:list(r3_hex_core:default_config()).
%% {ok, {200, ..., [#{
%% <<"authing_key">> => true,
%% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
%% <<"last_use">> =>
%% #{<<"ip">> => <<"1.2.3.4">>,
%% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
%% <<"name">> => <<"hex_core">>,
%% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
%% <<"revoked_at">> => nil,
%% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"url">> => <<"https://hex.pm/api/keys/test">>},
%% }]}}
%% '''
%% @end
-spec list(r3_hex_core:config()) -> r3_hex_api:response().
list(Config) when is_map(Config) ->
Path = r3_hex_api:build_organization_path(Config, ["keys"]),
r3_hex_api:get(Config, Path).

get(Config, Name) when is_map(Config) ->
%% @doc
%% Gets an API or repository key by name.
%%
%% Examples:
%%
%% ```
%% > r3_hex_api_key:get(r3_hex_core:default_config(), <<"test">>).
%% {ok, {200, ..., #{
%% <<"authing_key">> => true,
%% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
%% <<"last_use">> =>
%% #{<<"ip">> => <<"1.2.3.4">>,
%% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
%% <<"name">> => <<"hex_core">>,
%% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
%% <<"revoked_at">> => nil,
%% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"url">> => <<"https://hex.pm/api/keys/test">>},
%% }}}
%% '''
%% @end
-spec get(r3_hex_core:config(), binary()) -> r3_hex_api:response().
get(Config, Name) when is_map(Config) and is_binary(Name) ->
Path = r3_hex_api:build_organization_path(Config, ["keys", Name]),
r3_hex_api:get(Config, Path).

add(Config, Name, Permissions) when is_map(Config) ->
%% @doc
%% Adds a new API or repository key.
%%
%% Examples:
%%
%% ```
%% > r3_hex_api_key:add(r3_hex_core:default_config(), <<"test">>, [...]).
%% {ok, {200, ..., #{
%% <<"authing_key">> => true,
%% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
%% <<"last_use">> =>
%% #{<<"ip">> => <<"1.2.3.4">>,
%% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
%% <<"name">> => <<"hex_core">>,
%% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
%% <<"revoked_at">> => nil,
%% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"url">> => <<"https://hex.pm/api/keys/test">>},
%% }}}
%% '''
%% @end
-spec add(r3_hex_core:config(), binary(), [permission()]) -> r3_hex_api:response().
add(Config, Name, Permissions) when is_map(Config) and is_binary(Name) and is_list(Permissions) ->
Path = r3_hex_api:build_organization_path(Config, ["keys"]),
Params = #{<<"name">> => Name, <<"permissions">> => Permissions},
r3_hex_api:post(Config, Path, Params).

delete(Config, Name) when is_map(Config) ->
%% @doc
%% Deletes an API or repository key.
%%
%% Examples:
%%
%% ```
%% > r3_hex_api_key:delete(r3_hex_core:default_config(), <<"test">>).
%% {ok, {200, ..., #{
%% <<"authing_key">> => true,
%% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
%% <<"last_use">> =>
%% #{<<"ip">> => <<"1.2.3.4">>,
%% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
%% <<"name">> => <<"hex_core">>,
%% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
%% <<"revoked_at">> => nil,
%% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"url">> => <<"https://hex.pm/api/keys/test">>},
%% }}}
%% '''
%% @end
-spec delete(r3_hex_core:config(), binary()) -> r3_hex_api:response().
delete(Config, Name) when is_map(Config) and is_binary(Name) ->
Path = r3_hex_api:build_organization_path(Config, ["keys", Name]),
r3_hex_api:delete(Config, Path).

%% @doc
%% Deletes all API and repository keys associated with the account.
%%
%% Examples:
%%
%% ```
%% > r3_hex_api_key:delete_all(r3_hex_core:default_config()).
%% {ok, {200, ..., [#{
%% <<"authing_key">> => true,
%% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
%% <<"last_use">> =>
%% #{<<"ip">> => <<"1.2.3.4">>,
%% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
%% <<"name">> => <<"hex_core">>,
%% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
%% <<"revoked_at">> => nil,
%% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
%% <<"url">> => <<"https://hex.pm/api/keys/test">>},
%% }]}}
%% '''
%% @end
-spec delete_all(r3_hex_core:config()) -> r3_hex_api:response().
delete_all(Config) when is_map(Config) ->
Path = r3_hex_api:build_organization_path(Config, ["keys"]),
r3_hex_api:delete(Config, Path).
10 changes: 6 additions & 4 deletions src/r3_hex_api_package.erl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
%% Vendored from hex_core v0.5.1, do not edit manually
%% Vendored from hex_core v0.6.8, do not edit manually

-module(r3_hex_api_package).
-export([get/2, search/3]).

%% @doc
%% Gets package.
%% Gets a package.
%%
%% Examples:
%%
Expand All @@ -26,7 +26,8 @@
%% ]}}}
%% '''
%% @end
get(Config, Name) when is_binary(Name) and is_map(Config) ->
-spec get(r3_hex_core:config(), binary()) -> r3_hex_api:response().
get(Config, Name) when is_map(Config) and is_binary(Name)->
Path = r3_hex_api:build_repository_path(Config, ["packages", Name]),
r3_hex_api:get(Config, Path).

Expand All @@ -42,7 +43,8 @@ get(Config, Name) when is_binary(Name) and is_map(Config) ->
%% ...
%% ]}}
%% '''
search(Config, Query, SearchParams) when is_binary(Query) and is_list(SearchParams) and is_map(Config) ->
-spec search(r3_hex_core:config(), binary(), list(binary())) -> r3_hex_api:response().
search(Config, Query, SearchParams) when is_map(Config) and is_binary(Query) and is_list(SearchParams) ->
QueryString = r3_hex_api:encode_query_string([{search, Query} | SearchParams]),
Path = r3_hex_api:join_path_segments(r3_hex_api:build_repository_path(Config, ["packages"])),
PathQuery = <<Path/binary, "?", QueryString/binary>>,
Expand Down
Loading

0 comments on commit 310a4ad

Please sign in to comment.