Implement HTTP API using generics (fixes #380) #14
No reviewers
Labels
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Blocks
Reference: LemmyNet/lemmy#14
Loading…
Reference in New Issue
No description provided.
Delete Branch "nutomic/lemmy:http-api"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The first commit is the working implementation, we can go with that but I dont like that it needs a bunch of boilerplate in the route handler. I tried to simplify that with higher order functions in the second commit, but that is not compiling yet.
Had to add this because the git hook was throwing ``async fn
is not permitted in the 2015 edition
.Once this is implemented, we can also use the http api for performance testing and see which specific api calls are slow.
I would like to put each route in one line without line breaks, because thats much easier to read. But no idea how to tell that to the formatter.
There is a lot of room for improvement here. For example, the URLs should not contain any verb, it should be like
/api/v1/site
with POST executing creation, GET doing a get, etc. Also it should be like/api/v1/community/:name
, passing the name in the URL. But we can leave that for later.I agree with the first part. The
/api/v1/community/:name
way of doing things is for explicitly http only things, but since we have to support websocket as well with these, we might as well have the get body json be identical.Or at least I can't think of a way to have identical JSON request body's for both.
We should strictly type this. The responses can now look like the requests, with
{op: op, data: {...}}
at the top level. This is easy to change on the front end.I looked through and verified that I never use the
op
only theerror
on APIError. So this should be okay, although I would rather have it there for debugging purposes.One possibility, is for all these to just return
Err(format_err!("the_i18n_error_messag"));
Then on the websocket code, we could have aWebSocketError
type (that includes the op), and the http could have anHttpErrorType
, (that doesn't include it).Then we could transform those errors into the form they need to be in, given the call.
Yep, putting paramaters into the path means we have to duplicate all the param structs (or put all parameters into the path). And either way it would need a lot of boilerplate.
Well the way it works after my changes is that
op
is only in the websocket, and the api module doesnt know about that at all. However I believe thatserver::to_json_string
should also add the op to all error messages (untested).Can you explain this more, or give an example? I'm not sure what you want to type more strictly, or how.
Right now you're manipulating the json below to insert an op into it. We should have an upper level struct for this like
Good idea, that makes the code much cleaner. Unfortunately its not working on the frontend, because the data is wrapped one level deeper. So this would need some changes on the frontend, or some way of doing this without the
data
field.That's okay, I can alter the front end. It makes sense anyway since the op is only needed for websocket, and should be at the top level.
I got the front end part of it done, but I don't think you have Pull requests enabled on your fork so I can't add my commits to your
http-api
branch.My changes are here: https://yerbamate.dev/dessalines/lemmy/src/branch/dessalines-http-api
It looks to me like PRs are enabled, but maybe things are weird because my repo is a fork of yours. Anyway it sounds confusing to merge your changes into my PR, and then back into your repo. Why dont you just squash and merge my changes locally, then merge yours after that?
Ya could be. I think github has an easy way for the original master to add commits to PRs, but I couldn't figure out how to do that here.
Sure thing, but there's some other stuff we gotta add before I merge into master, esp the API docs. And I gotta test some of the endpoints yet.
I'll work off this: https://github.com/dessalines/lemmy/pull/442
I'm working on the docs for this rn.
This is wrong:
.route("/api/v1/post/replies", web::get().to(route::<GetReplies, GetRepliesResponse>))
It needs to be under
/api/v1/user/replies
Also these should probably be
PUT
, any one with either save or edit:.route("/api/v1/post/save", web::post().to(route::<SavePost, PostResponse>))
.route("/api/v1/comment/save", web::post().to(route::<SaveComment, CommentResponse>))
.route("/api/v1/user/save_user_settings", web::post().to(route::<SaveUserSettings, LoginResponse>));
PUT /user/mention
.route("/api/v1/user/mentions", web::put().to(route::<EditUserMention, UserMentionResponse>))
Some of these are _, while some are dashes: ban-user. They should all be underscore.
/search
should probably be a GET, esp if things like/modlog
are.Damn we are all over the place. Should I fix this on my branch?
Ya, once you do another commit I'll pull it in to that other one. I'm not sure why gitea won't let us collaborate on a PR.
done