Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API V2: new API with the correct response codes #144

Open
josecelano opened this issue Dec 23, 2022 · 2 comments
Open

API V2: new API with the correct response codes #144

josecelano opened this issue Dec 23, 2022 · 2 comments
Labels
- User - Enjoyable to Use our Software Code Cleanup / Refactoring Tidying and Making Neat Hard Non-Trivial Needs Research We Need to Know More About This Optimization Make it Faster
Milestone

Comments

@josecelano
Copy link
Member

josecelano commented Dec 23, 2022

Parent issue: #141
It depends on: #143

We start building a new API using the /v2 prefix. For example: /v2/api/stats. We can use the same URLs but change the response codes.

Change the response codes to the right one: 200, 201, 404, etcetera. Right now, it only uses 200 and 500.

Potential changes

I think we could make other changes:

  • Refactor: create a custom Reject for all the errors. Like the one I have added: struct Unauthorized.
  • Refactor: move routes to their own mod routes.rs.
  • The authenticate function should also return a 401 response instead of token not valid when the token is present but not valid.
  • The GET /api/torrent/:info_hash endpoint should return a 404 when the torrent does not exist and the token is valid.
  • The endpoint GET /api/torrent/:info_hash should return a 404 when the torrent does not exist, and the token is invalid. See Insecure Direct Object References (IDOR)). We should not expose that the tracker has a given torrent because the tracker could be private.
  • The DELETE /api/whitelist/:info_hash endpoint should return: a "204 No Content" when it deletes the torrent and a "404 Not Found" when the torrent does not exist or the token is invalid.
  • The POST /api/whitelist/:info_hash endpoint should return a "201 Created" response with the location of the new resource GET /api/whitelist/:info_hash. In this case we would need to create that new endpoint. It could be empty. It could be used only to know if a torrent is whitelisted. Alternatively, we could consider the whitelist a unique resource and use a PUT /api/whitelist/:info_hash to add a new torrent to the list.
  • The POST /api/key/:seconds_valid endpoint should return a "201 Created".
  • The DELETE /api/key/:key should return "204 No Content".
  • The GET /api/whitelist/reload endpoint should be a POST, PUT or PATCH.

Notes

  • We can keep the old API in parallel until we release the new major version.

Long-term changes

These are changes not included in this issue, but they should be considered in future versions.

  • The GET /api/whitelist/reload endpoint is a little weird. It's like a remote command. I think I should have used POST /api/commands with the command you want to execute in the request body or POST /api/reload-whitelist.
  • Same for GET /api/keys/reload
  • We could use a header parameter for the API version instead of a URL prefix, like this.
  • Use a schema (most likely https://json-ld.org/).
  • Re-design the API and consider other endpoints like the ones suggested here by @dev1z
@josecelano josecelano added the Code Cleanup / Refactoring Tidying and Making Neat label Dec 23, 2022
@josecelano josecelano mentioned this issue Dec 23, 2022
4 tasks
@josecelano
Copy link
Member Author

@da2ce7 @WarmBeer

I've found this interesting issue tokio-rs/axum#50 where people are asking Axum to add support for generating OpenAPI/Swagger docs.

And some mentioned that you could use utopia. It has support for all the major web frameworks.

@josecelano josecelano changed the title New API with URL prefix and correct response codes API overhaul: New API with the correct response codes Mar 12, 2023
@josecelano josecelano changed the title API overhaul: New API with the correct response codes API overhaul: new API with the correct response codes Mar 12, 2023
@josecelano josecelano changed the title API overhaul: new API with the correct response codes API V2: new API with the correct response codes Mar 12, 2023
@da2ce7 da2ce7 added - User - Enjoyable to Use our Software Needs Research We Need to Know More About This Optimization Make it Faster labels Oct 10, 2023
@cgbosse cgbosse modified the milestones: v3.3.0, V3.2.0 Jan 16, 2024
@cgbosse cgbosse added the Hard Non-Trivial label Jan 16, 2024
@josecelano
Copy link
Member Author

The rqbit - bittorrent client in Rust uses this very useful API entrypoint:

{
  "apis": {
    "GET /": "list all available APIs",
    "GET /dht/stats": "DHT stats",
    "GET /dht/table": "DHT routing table",
    "GET /torrents": "List torrents (default torrent is 0)",
    "GET /torrents/{index}": "Torrent details",
    "GET /torrents/{index}/haves": "The bitfield of have pieces",
    "GET /torrents/{index}/peer_stats": "Per peer stats",
    "GET /torrents/{index}/stats/v1": "Torrent stats",
    "GET /web/": "Web UI",
    "POST /rust_log": "Set RUST_LOG to this post launch (for debugging)",
    "POST /torrents": "Add a torrent here. magnet: or http:// or a local file.",
    "POST /torrents/{index}/delete": "Forget about the torrent, remove the files",
    "POST /torrents/{index}/forget": "Forget about the torrent, keep the files",
    "POST /torrents/{index}/pause": "Pause torrent",
    "POST /torrents/{index}/start": "Resume torrent"
  },
  "server": "rqbit",
  "version": "5.4.1"
}

cc @da2ce7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- User - Enjoyable to Use our Software Code Cleanup / Refactoring Tidying and Making Neat Hard Non-Trivial Needs Research We Need to Know More About This Optimization Make it Faster
Projects
Status: Maintenance
Development

No branches or pull requests

3 participants