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.

159 lines
5.6 KiB

  1. %% Vendored from hex_core v0.6.8, do not edit manually
  2. -module(r3_hex_api_key).
  3. -export([
  4. list/1,
  5. get/2,
  6. add/3,
  7. delete/2,
  8. delete_all/1
  9. ]).
  10. -export_type([permission/0]).
  11. -type permission() :: api_permission() | repo_permission() | repos_permission().
  12. -ifdef(OTP_19).
  13. -type api_permission() :: #{domain := api, resource => read | write}.
  14. -type repo_permission() :: #{domain := repository, resource := binary()}.
  15. -type repos_permission() :: #{domain := repositories}.
  16. -else.
  17. -type api_permission() :: #{domain => api, resource => read | write}.
  18. -type repo_permission() :: #{domain => repository, resource => binary()}.
  19. -type repos_permission() :: #{domain => repositories}.
  20. -endif.
  21. %% @doc
  22. %% Lists the user's or organization's API and repository keys.
  23. %%
  24. %% Examples:
  25. %%
  26. %% ```
  27. %% > r3_hex_api_key:list(r3_hex_core:default_config()).
  28. %% {ok, {200, ..., [#{
  29. %% <<"authing_key">> => true,
  30. %% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
  31. %% <<"last_use">> =>
  32. %% #{<<"ip">> => <<"1.2.3.4">>,
  33. %% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
  34. %% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
  35. %% <<"name">> => <<"hex_core">>,
  36. %% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
  37. %% <<"revoked_at">> => nil,
  38. %% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
  39. %% <<"url">> => <<"https://hex.pm/api/keys/test">>},
  40. %% }]}}
  41. %% '''
  42. %% @end
  43. -spec list(r3_hex_core:config()) -> r3_hex_api:response().
  44. list(Config) when is_map(Config) ->
  45. Path = r3_hex_api:build_organization_path(Config, ["keys"]),
  46. r3_hex_api:get(Config, Path).
  47. %% @doc
  48. %% Gets an API or repository key by name.
  49. %%
  50. %% Examples:
  51. %%
  52. %% ```
  53. %% > r3_hex_api_key:get(r3_hex_core:default_config(), <<"test">>).
  54. %% {ok, {200, ..., #{
  55. %% <<"authing_key">> => true,
  56. %% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
  57. %% <<"last_use">> =>
  58. %% #{<<"ip">> => <<"1.2.3.4">>,
  59. %% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
  60. %% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
  61. %% <<"name">> => <<"hex_core">>,
  62. %% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
  63. %% <<"revoked_at">> => nil,
  64. %% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
  65. %% <<"url">> => <<"https://hex.pm/api/keys/test">>},
  66. %% }}}
  67. %% '''
  68. %% @end
  69. -spec get(r3_hex_core:config(), binary()) -> r3_hex_api:response().
  70. get(Config, Name) when is_map(Config) and is_binary(Name) ->
  71. Path = r3_hex_api:build_organization_path(Config, ["keys", Name]),
  72. r3_hex_api:get(Config, Path).
  73. %% @doc
  74. %% Adds a new API or repository key.
  75. %%
  76. %% Examples:
  77. %%
  78. %% ```
  79. %% > r3_hex_api_key:add(r3_hex_core:default_config(), <<"test">>, [...]).
  80. %% {ok, {200, ..., #{
  81. %% <<"authing_key">> => true,
  82. %% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
  83. %% <<"last_use">> =>
  84. %% #{<<"ip">> => <<"1.2.3.4">>,
  85. %% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
  86. %% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
  87. %% <<"name">> => <<"hex_core">>,
  88. %% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
  89. %% <<"revoked_at">> => nil,
  90. %% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
  91. %% <<"url">> => <<"https://hex.pm/api/keys/test">>},
  92. %% }}}
  93. %% '''
  94. %% @end
  95. -spec add(r3_hex_core:config(), binary(), [permission()]) -> r3_hex_api:response().
  96. add(Config, Name, Permissions) when is_map(Config) and is_binary(Name) and is_list(Permissions) ->
  97. Path = r3_hex_api:build_organization_path(Config, ["keys"]),
  98. Params = #{<<"name">> => Name, <<"permissions">> => Permissions},
  99. r3_hex_api:post(Config, Path, Params).
  100. %% @doc
  101. %% Deletes an API or repository key.
  102. %%
  103. %% Examples:
  104. %%
  105. %% ```
  106. %% > r3_hex_api_key:delete(r3_hex_core:default_config(), <<"test">>).
  107. %% {ok, {200, ..., #{
  108. %% <<"authing_key">> => true,
  109. %% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
  110. %% <<"last_use">> =>
  111. %% #{<<"ip">> => <<"1.2.3.4">>,
  112. %% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
  113. %% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
  114. %% <<"name">> => <<"hex_core">>,
  115. %% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
  116. %% <<"revoked_at">> => nil,
  117. %% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
  118. %% <<"url">> => <<"https://hex.pm/api/keys/test">>},
  119. %% }}}
  120. %% '''
  121. %% @end
  122. -spec delete(r3_hex_core:config(), binary()) -> r3_hex_api:response().
  123. delete(Config, Name) when is_map(Config) and is_binary(Name) ->
  124. Path = r3_hex_api:build_organization_path(Config, ["keys", Name]),
  125. r3_hex_api:delete(Config, Path).
  126. %% @doc
  127. %% Deletes all API and repository keys associated with the account.
  128. %%
  129. %% Examples:
  130. %%
  131. %% ```
  132. %% > r3_hex_api_key:delete_all(r3_hex_core:default_config()).
  133. %% {ok, {200, ..., [#{
  134. %% <<"authing_key">> => true,
  135. %% <<"inserted_at">> => <<"2019-02-27T11:15:32Z">>,
  136. %% <<"last_use">> =>
  137. %% #{<<"ip">> => <<"1.2.3.4">>,
  138. %% <<"used_at">> => <<"2019-02-27T14:38:54Z">>,
  139. %% <<"user_agent">> => <<"hex_core/0.5.0 (httpc) (OTP/21) (erts/10.2)">>},
  140. %% <<"name">> => <<"hex_core">>,
  141. %% <<"permissions">> => [#{<<"domain">> => <<"api">>,<<"resource">> => <<"read">>}],
  142. %% <<"revoked_at">> => nil,
  143. %% <<"updated_at">> => <<"2019-02-27T14:38:54Z">>,
  144. %% <<"url">> => <<"https://hex.pm/api/keys/test">>},
  145. %% }]}}
  146. %% '''
  147. %% @end
  148. -spec delete_all(r3_hex_core:config()) -> r3_hex_api:response().
  149. delete_all(Config) when is_map(Config) ->
  150. Path = r3_hex_api:build_organization_path(Config, ["keys"]),
  151. r3_hex_api:delete(Config, Path).