Skip to content

Commit 907f240

Browse files
Pi-Clalpil
authored andcommitted
Document where to find APIs
1 parent aa0ae05 commit 907f240

File tree

2 files changed

+116
-7
lines changed

2 files changed

+116
-7
lines changed

CONTRIBUTING.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# How to Contribute
2+
3+
## Adding a New API Function
4+
5+
1. Figure out what you want to do
6+
- Go to https://hexdocs.pm/hex/Mix.Tasks.Hex.html and find what you want to do
7+
2. Once you find the page, click on the code icon on the top-right to go to the corresponding source code like so: https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.owner.ex#L125
8+
```elixir
9+
defp transfer_owner(organization, package, owner) do
10+
auth = Mix.Tasks.Hex.auth_info(:write)
11+
Hex.Shell.info("Transferring ownership to #{owner} for #{package}")
12+
13+
case Hex.API.Package.Owner.add(organization, package, owner, "full", true, auth) do
14+
{:ok, {code, _body, _headers}} when code in 200..299 ->
15+
:ok
16+
17+
other ->
18+
Hex.Shell.error("Transferring ownership failed")
19+
Hex.Utils.print_error_result(other)
20+
end
21+
end
22+
```
23+
24+
3. The API function this is calling is `Hex.API.Package.Owner.add` so we go to https://github.com/hexpm/hex/blob/main/lib/hex/api/package.ex, scroll down to the defmodule for Owner and then find the `add` function:
25+
```elixir
26+
def add(repo, package, owner, level, transfer, auth) when package != "" do
27+
Hex.API.check_write_api()
28+
29+
owner = URI.encode_www_form(owner)
30+
path = "packages/#{URI.encode(package)}/owners/#{URI.encode(owner)}"
31+
params = %{level: level, transfer: transfer}
32+
API.erlang_put_request(repo, path, params, auth)
33+
end
34+
```
35+
`path` tells us what path and variables we will be using while `params` tells us what the body of the request should contain. The `API.erlang_put_request` at the bottom of this tells us the method of our request needs to be `PUT`. Now we have all of the information we need to create our request function like so:
36+
```elixir
37+
pub fn transfer_owner_request(
38+
package_name: &str,
39+
owner: &str,
40+
api_key: &str,
41+
config: &Config,
42+
) -> http::Request<Vec<u8>> {
43+
let body = json!({
44+
"level": OwnerLevel::Full.to_string(),
45+
"transfer": true,
46+
});
47+
48+
config
49+
.api_request(
50+
Method::PUT,
51+
&format!("packages/{}/owners/{}", package_name, owner),
52+
Some(api_key),
53+
)
54+
.body(body.to_string().into_bytes())
55+
.expect("transfer_owner_request request")
56+
}
57+
```
58+
Note that the `api_key` and `config` fields will always be present in these request functions while the other fields are tailored to the specific request we want to make.
59+
60+
4. TODO: How to figure out what to write for the response function?

src/lib.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ fn make_request(
8888
}
8989

9090
/// Create a request that creates a Hex API key.
91+
///
92+
/// API Docs:
93+
///
94+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.ex#L137
95+
///
96+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/key.ex#L6
9197
pub fn create_api_key_request(
9298
username: &str,
9399
password: &str,
@@ -125,6 +131,12 @@ pub fn create_api_key_response(response: http::Response<Vec<u8>>) -> Result<Stri
125131
}
126132

127133
/// Create a request that deletes an Hex API key.
134+
///
135+
/// API Docs:
136+
///
137+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.user.ex#L291
138+
///
139+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/key.ex#L15
128140
pub fn remove_api_key_request(
129141
name_of_key_to_delete: &str,
130142
api_key: &str,
@@ -137,7 +149,7 @@ pub fn remove_api_key_request(
137149
Some(api_key),
138150
)
139151
.body(vec![])
140-
.expect("get_package_tarball_request request")
152+
.expect("remove_api_key_request request")
141153
}
142154

143155
/// Parses a request that deleted a Hex API key.
@@ -152,6 +164,12 @@ pub fn remove_api_key_response(response: http::Response<Vec<u8>>) -> Result<(),
152164
}
153165

154166
/// Retire an existing package release from Hex.
167+
///
168+
/// API Docs:
169+
///
170+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.retire.ex#L75
171+
///
172+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/release.ex#L28
155173
pub fn retire_release_request(
156174
package: &str,
157175
version: &str,
@@ -171,7 +189,7 @@ pub fn retire_release_request(
171189
Some(api_key),
172190
)
173191
.body(body.to_string().into_bytes())
174-
.expect("require_release_request request")
192+
.expect("retire_release_request request")
175193
}
176194

177195
/// Parses a request that retired a release.
@@ -186,6 +204,12 @@ pub fn retire_release_response(response: http::Response<Vec<u8>>) -> Result<(),
186204
}
187205

188206
/// Un-retire an existing retired package release from Hex.
207+
///
208+
/// API Docs:
209+
///
210+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.retire.ex#L89
211+
///
212+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/release.ex#L35
189213
pub fn unretire_release_request(
190214
package: &str,
191215
version: &str,
@@ -199,7 +223,7 @@ pub fn unretire_release_request(
199223
Some(api_key),
200224
)
201225
.body(vec![])
202-
.expect("require_release_request request")
226+
.expect("unretire_release_request request")
203227
}
204228

205229
/// Parses a request that un-retired a package version.
@@ -215,7 +239,7 @@ pub fn unretire_release_response(response: http::Response<Vec<u8>>) -> Result<()
215239

216240
/// Create a request that get the names and versions of all of the packages on
217241
/// the package registry.
218-
///
242+
/// TODO: Where are the API docs for this?
219243
pub fn get_repository_versions_request(
220244
api_key: Option<&str>,
221245
config: &Config,
@@ -224,7 +248,7 @@ pub fn get_repository_versions_request(
224248
.repository_request(Method::GET, "versions", api_key)
225249
.header("accept", "application/json")
226250
.body(vec![])
227-
.expect("create_api_key_request request")
251+
.expect("get_repository_versions_request request")
228252
}
229253

230254
/// Parse a request that get the names and versions of all of the packages on
@@ -269,6 +293,11 @@ pub fn get_repository_versions_response(
269293

270294
/// Create a request to get the information for a package in the repository.
271295
///
296+
/// API Docs:
297+
///
298+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.package.ex#L348
299+
///
300+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/package.ex#L36
272301
pub fn get_package_request(
273302
name: &str,
274303
api_key: Option<&str>,
@@ -321,7 +350,7 @@ pub fn get_package_response(
321350
}
322351

323352
/// Create a request to download a version of a package as a tarball
324-
///
353+
/// TODO: Where are the API docs for this?
325354
pub fn get_package_tarball_request(
326355
name: &str,
327356
version: &str,
@@ -358,6 +387,11 @@ pub fn get_package_tarball_response(
358387
Ok(body)
359388
}
360389

390+
/// API Docs:
391+
///
392+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.publish.ex#L384
393+
///
394+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/release_docs.ex#L19
361395
pub fn remove_docs_request(
362396
package_name: &str,
363397
version: &str,
@@ -373,7 +407,7 @@ pub fn remove_docs_request(
373407
Some(api_key),
374408
)
375409
.body(vec![])
376-
.expect("get_package_tarball_request request"))
410+
.expect("remove_docs_request request"))
377411
}
378412

379413
pub fn remove_docs_response(response: http::Response<Vec<u8>>) -> Result<(), ApiError> {
@@ -388,6 +422,11 @@ pub fn remove_docs_response(response: http::Response<Vec<u8>>) -> Result<(), Api
388422
}
389423
}
390424

425+
/// API Docs:
426+
///
427+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.publish.ex#L429
428+
///
429+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/release_docs.ex#L11
391430
pub fn publish_docs_request(
392431
package_name: &str,
393432
version: &str,
@@ -421,6 +460,11 @@ pub fn publish_docs_response(response: http::Response<Vec<u8>>) -> Result<(), Ap
421460
}
422461
}
423462

463+
/// API Docs:
464+
///
465+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.publish.ex#L512
466+
///
467+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/release.ex#L13
424468
pub fn publish_package_request(
425469
release_tarball: Vec<u8>,
426470
api_key: &str,
@@ -459,6 +503,11 @@ pub fn publish_package_response(response: http::Response<Vec<u8>>) -> Result<(),
459503
}
460504
}
461505

506+
/// API Docs:
507+
///
508+
/// https://github.com/hexpm/hex/blob/main/lib/mix/tasks/hex.publish.ex#L371
509+
///
510+
/// https://github.com/hexpm/hex/blob/main/lib/hex/api/release.ex#L21
462511
pub fn revert_release_request(
463512
package_name: &str,
464513
version: &str,

0 commit comments

Comments
 (0)