Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

173 Zeilen
6.6 KiB

  1. %% Vendored from hex_core v0.7.0, do not edit manually
  2. -module(r3_hex_api_release).
  3. -export([
  4. delete/3,
  5. get/3,
  6. publish/2,
  7. publish/3,
  8. retire/4,
  9. unretire/3
  10. ]).
  11. -export_type([publish_params/0, retirement_params/0, retirement_reason/0]).
  12. -type publish_params() :: [{replace, boolean()}].
  13. -type retirement_reason() :: other | invalid | security | deprecated | renamed.
  14. -ifdef(OTP_19).
  15. -type retirement_params() :: #{reason := retirement_reason(), message => binary()}.
  16. -else.
  17. -type retirement_params() :: #{reason => retirement_reason(), message => binary()}.
  18. -endif.
  19. %% @doc
  20. %% Gets a package release.
  21. %%
  22. %% Examples:
  23. %%
  24. %% ```
  25. %% > r3_hex_api_release:get(r3_hex_core:default_config(), <<"package">>, <<"1.0.0">>).
  26. %% {ok, {200, ..., #{
  27. %% <<"checksum">> => <<"540d210d81f56f17f64309a4896430e727972499b37bd59342dc08d61dff74d8">>,
  28. %% <<"docs_html_url">> => <<"https://hexdocs.pm/package/1.0.0/">>,
  29. %% <<"downloads">> => 740,<<"has_docs">> => true,
  30. %% <<"html_url">> => <<"https://hex.pm/packages/package/1.0.0">>,
  31. %% <<"inserted_at">> => <<"2014-12-09T18:32:03Z">>,
  32. %% <<"meta">> =>
  33. %% #{<<"app">> => <<"package">>,
  34. %% <<"build_tools">> => [<<"mix">>]},
  35. %% <<"package_url">> => <<"https://hex.pm/api/packages/package">>,
  36. %% <<"publisher">> => nil,<<"requirements">> => #{},
  37. %% <<"retirement">> => nil,
  38. %% <<"updated_at">> => <<"2019-07-28T21:12:11Z">>,
  39. %% <<"url">> => <<"https://hex.pm/api/packages/package/releases/1.0.0">>,
  40. %% <<"version">> => <<"1.0.0">>
  41. %% }}}
  42. %% '''
  43. %% @end
  44. -spec get(r3_hex_core:config(), binary(), binary()) -> r3_hex_api:response().
  45. get(Config, Name, Version) when is_map(Config) and is_binary(Name) and is_binary(Version) ->
  46. Path = r3_hex_api:build_repository_path(Config, ["packages", Name, "releases", Version]),
  47. r3_hex_api:get(Config, Path).
  48. %% @doc
  49. %% Publishes a new package release.
  50. %%
  51. %% Examples:
  52. %%
  53. %% ```
  54. %% > r3_hex_api_release:publish(r3_hex_core:default_config(), Tarball).
  55. %% {ok, {200, ..., #{
  56. %% <<"checksum">> => <<"540d210d81f56f17f64309a4896430e727972499b37bd59342dc08d61dff74d8">>,
  57. %% <<"docs_html_url">> => <<"https://hexdocs.pm/package/1.0.0/">>,
  58. %% <<"downloads">> => 740,<<"has_docs">> => true,
  59. %% <<"html_url">> => <<"https://hex.pm/packages/package/1.0.0">>,
  60. %% <<"inserted_at">> => <<"2014-12-09T18:32:03Z">>,
  61. %% <<"meta">> =>
  62. %% #{<<"app">> => <<"package">>,
  63. %% <<"build_tools">> => [<<"mix">>]},
  64. %% <<"package_url">> => <<"https://hex.pm/api/packages/package">>,
  65. %% <<"publisher">> => nil,<<"requirements">> => #{},
  66. %% <<"retirement">> => nil,
  67. %% <<"updated_at">> => <<"2019-07-28T21:12:11Z">>,
  68. %% <<"url">> => <<"https://hex.pm/api/packages/package/releases/1.0.0">>,
  69. %% <<"version">> => <<"1.0.0">>
  70. %% }}}
  71. %% '''
  72. %% @end
  73. -spec publish(r3_hex_core:config(), binary()) -> r3_hex_api:response().
  74. publish(Config, Tarball) -> publish(Config, Tarball, []).
  75. %% @doc
  76. %% Publishes a new package release with query parameters.
  77. %%
  78. %% Supported query params :
  79. %% - replace : boolean
  80. %%
  81. %% Examples:
  82. %%
  83. %% ```
  84. %% > r3_hex_api_release:publish(r3_hex_core:default_config(), Tarball, [{replace, true}]).
  85. %% {ok, {201, ..., #{
  86. %% <<"checksum">> => <<"540d210d81f56f17f64309a4896430e727972499b37bd59342dc08d61dff74d8">>,
  87. %% <<"docs_html_url">> => <<"https://hexdocs.pm/package/1.0.0/">>,
  88. %% <<"downloads">> => 740,<<"has_docs">> => true,
  89. %% <<"html_url">> => <<"https://hex.pm/packages/package/1.0.0">>,
  90. %% <<"inserted_at">> => <<"2014-12-09T18:32:03Z">>,
  91. %% <<"meta">> =>
  92. %% #{<<"app">> => <<"package">>,
  93. %% <<"build_tools">> => [<<"mix">>]},
  94. %% <<"package_url">> => <<"https://hex.pm/api/packages/package">>,
  95. %% <<"publisher">> => nil,<<"requirements">> => #{},
  96. %% <<"retirement">> => nil,
  97. %% <<"updated_at">> => <<"2019-07-28T21:12:11Z">>,
  98. %% <<"url">> => <<"https://hex.pm/api/packages/package/releases/1.0.0">>,
  99. %% <<"version">> => <<"1.0.0">>
  100. %% }}}
  101. %% '''
  102. %% @end
  103. -spec publish(hexcore:config(), binary(), publish_params()) -> r3_hex_api:response().
  104. publish(Config, Tarball, Params) when is_map(Config) andalso is_binary(Tarball) andalso is_list(Params)->
  105. QueryString = r3_hex_api:encode_query_string([{replace, proplists:get_value(replace, Params, false)}]),
  106. Path = r3_hex_api:join_path_segments(r3_hex_api:build_repository_path(Config, ["publish"])),
  107. PathWithQuery = <<Path/binary, "?", QueryString/binary>>,
  108. TarballContentType = "application/octet-stream",
  109. Config2 = put_header(<<"content-length">>, integer_to_binary(byte_size(Tarball)), Config),
  110. Body = {TarballContentType, Tarball},
  111. r3_hex_api:post(Config2, PathWithQuery, Body).
  112. %% @doc
  113. %% Deletes a package release.
  114. %%
  115. %% Examples:
  116. %%
  117. %% ```
  118. %% > r3_hex_api_release:delete(r3_hex_core:default_config(), <<"package">>, <<"1.0.0">>).
  119. %% {ok, {204, ..., nil}}
  120. %% '''
  121. %% @end
  122. -spec delete(r3_hex_core:config(), binary(), binary()) -> r3_hex_api:response().
  123. delete(Config, Name, Version) when is_map(Config) and is_binary(Name) and is_binary(Version) ->
  124. Path = r3_hex_api:build_repository_path(Config, ["packages", Name, "releases", Version]),
  125. r3_hex_api:delete(Config, Path).
  126. %% @doc
  127. %% Retires a package release.
  128. %%
  129. %% Examples:
  130. %%
  131. %% ```
  132. %% > r3_hex_api_release:retire(r3_hex_core:default_config(), <<"package">>, <<"1.0.0">>, Params).
  133. %% {ok, {204, ..., nil}}
  134. %% '''
  135. %% @end
  136. -spec retire(r3_hex_core:config(), binary(), binary(), retirement_params()) -> r3_hex_api:response().
  137. retire(Config, Name, Version, Params) when is_map(Config) and is_binary(Name) and is_binary(Version) ->
  138. Path = r3_hex_api:build_repository_path(Config, ["packages", Name, "releases", Version, "retire"]),
  139. r3_hex_api:post(Config, Path, Params).
  140. %% @doc
  141. %% Unretires a package release.
  142. %%
  143. %% Examples:
  144. %%
  145. %% ```
  146. %% > r3_hex_api_release:unretire(r3_hex_core:default_config(), <<"package">>, <<"1.0.0">>).
  147. %% {ok, {204, ..., nil}}
  148. %% '''
  149. %% @end
  150. -spec unretire(r3_hex_core:config(), binary(), binary()) -> r3_hex_api:response().
  151. unretire(Config, Name, Version) when is_map(Config) and is_binary(Name) and is_binary(Version) ->
  152. Path = r3_hex_api:build_repository_path(Config, ["packages", Name, "releases", Version, "retire"]),
  153. r3_hex_api:delete(Config, Path).
  154. %%====================================================================
  155. %% Internal functions
  156. %%====================================================================
  157. put_header(Name, Value, Config) ->
  158. Headers = maps:get(http_headers, Config, #{}),
  159. Headers2 = maps:put(Name, Value, Headers),
  160. maps:put(http_headers, Headers2, Config).