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

lib.NewRequestHandler removes boilerplate from api package #1682

Merged
merged 2 commits into from
Mar 6, 2021

Conversation

b5
Copy link
Member

@b5 b5 commented Mar 5, 2021

building on ideas from #1650, this adds the first method where the HTTP handler is generated by package lib, but mapped to an endpoint by package api.

This PR is mainly to establish the patten & talk through repercussions. Right now this only works for formats where the response format is well known / singular.

@b5 b5 added the API JSON API issues label Mar 5, 2021
@b5 b5 requested review from Arqu, dustmop and ramfox March 5, 2021 16:41
@b5 b5 self-assigned this Mar 5, 2021
@b5 b5 changed the title B5/dispatch request handler lib.NewRequestHandler removes boilerplate from api package Mar 5, 2021
Copy link
Contributor

@Arqu Arqu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from some nits, LGTM but will leave to @dustmop to take a look at parts relevant to him.

But very smooth 🍸

// switch on HTTP verbs. I think we should use tricks like this that leverage
// the gorilla/mux package until we get a better sense of how our API uses
// HTTP verbs
func handleRefRouteMethods(m *mux.Router, ae lib.APIEndpoint, f http.HandlerFunc, methods ...string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

api/api_test.go Outdated
@@ -251,7 +251,7 @@ func TestServerReadOnlyRoutes(t *testing.T) {
{"POST", "/registry/profile/new", 403},
{"POST", "/registry/profile/prove", 403},
{"GET", "/checkout/", 403},
{"GET", "/status/", 403},
// {"GET", "/status/", 403},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work with {"GET", "/status", 403}, ie remove the trailing / as gorilla is strict about slashes by default.

// UnmarshalParams deserialzes a lib req params stuct pointer from an HTTP
// request
//
// Deprecated: This is only exported for one-off handlers in the API package
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

lib/http.go Outdated
}

if err := UnmarshalParams(r, p); err != nil {
log.Debugw("unmarshal dataset save error", "err", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an odd message, maybe unmarshal request params error

Copy link
Contributor

@dustmop dustmop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation seems totally agreeable to me.

I have a suggestion for how to handle some of the outstanding questions, such as how to correlate endpoints to methods, and how to determine which http verb to use: 963e650

@@ -107,7 +107,7 @@ const (
// fsi endpoints

// AEStatus returns the filesystem dataset status
AEStatus = APIEndpoint("/status/{path:.*}")
AEStatus = APIEndpoint("/status")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean /status cannot be used with a reference? That seems to contradict the unit test just above in api/fsi_test.go. Also, though that's how it works currently, it's actually a bug that should be fixed, see https://github.com/qri-io/qri/blob/master/cmd/status.go#L70 and #1246.

Copy link
Contributor

@Arqu Arqu Mar 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can, but the route expansion is handled by handleRefRouteMethods in api/api.go where it registers all the variants, this basically just represents the "route prefix" as we used to carry around.

So for now it works pretty much as it used to, still contradicting #1246 and is a fix for another day/PR I guess.

Leveraging our new patterns to have lib begin to generate it's own request
handlers
@b5 b5 force-pushed the b5/dispatch_request_handler branch from cf03f29 to 5ef1821 Compare March 6, 2021 03:38
rework link params processing & add method-specific-and-ref-specific
handler function
@b5 b5 force-pushed the b5/dispatch_request_handler branch from 5ef1821 to 27c187c Compare March 6, 2021 11:49
@b5 b5 merged commit 2b95e84 into master Mar 6, 2021
@b5 b5 deleted the b5/dispatch_request_handler branch March 6, 2021 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API JSON API issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants