We solved this by building a layer to map HTTP+JSON calls onto RPCs.
The https://github.com/grpc-ecosystem/grpc-gateway project does this, as
does https://github.com/fullcontact/grpc-jersey (what we built). They both
map normal HTTP/JSON requests onto gRPC handlers by annotating RPC methods
with rules which expose them as HTTP/1.1 methods. e.x.:
service UserService {
rpc GetUser (GetUserRequest) returns (User) {
option (google.api.http).get = "/users/{id}";
}
rpc CreateUser (CreateUserRequest) returns (User) {
option (google.api.http) = {
post: "/users/"
body: "*"
};
}
}
We left RPC as RPC, and then mapped HTTP/1.1 (sometimes "REST", not all RPC
maps well as REST) onto it. REST resource verbs map well onto resource
management RPCs (e.x. createFoo/listFoo/getFoo/deleteFoo/updateFoo -> POST
/foo, GET /foo, GET /foo/{id}, DELETE /foo/{id}, PUT /foo/{id}).
gRPC is implemented on top of HTTP/2, certainly, but I wouldn't try to
interface with it directly. Treat it as any other binary protocol. gRPC's
use of HTTP trailers make it fairly unsuitable for interfacing with. I
don't believe you'll achieve much success trying to modify gRPC to produce
a REST-compatible API, even if you can have it produce JSON. Either use one
of the available projects or write a manual mapping layer.
Regards,
Michael
On Tuesday, January 10, 2017 at 6:17:59 PM UTC-7, [email protected]
wrote:
>
> I'm trying to design an API for our app. We're going to have to support a
> REST API, but I'd really also like to have a gRPC API as well.
>
> The question is -- how to design them so they're roughly the same? And in
> particular, what do we do about verbs / actions?
>
> The standard rule in designing a REST API is to use nouns, not verbs. This
> blog post explains it well:
>
>
> https://apigee.com/about/blog/technology/restful-api-design-nouns-are-good-verbs-are-bad
>
> You should have http methods that look like this:
>
> GET /dogs/1234 // get a dog
> POST /dogs // create a dog
> DELETE /dogs/1234 // delete a dog
>
> and not this:
>
> /createDog
> /deleteDog
>
> Unfortunately, gRPC makes standard REST-style calls impossible, because it
> doesn't support path parameters, and as far as I can tell it always uses
> the POST http method.
>
> So, I have two ways to modify the REST calls to make them gRPC compatible:
>
> 1. Put a verb in the endpoint:
>
> POST /dogs/create body={ id: 1234, name="Fluffy" }
>
> or 2. Put the verb in the body:
>
> POST /dogs body={ action:"create", id: 1234, name="Fluffy" }
>
> What do most people do?
>
>
--
*CONFIDENTIALITY NOTICE: This email message, and any documents, files or
previous e-mail messages attached to it is for the sole use of the intended
recipient(s) and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited. If you
are not the intended recipient, please contact the sender by reply email
and destroy all copies of the original message.*
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/21f2521a-04b6-4e63-a44c-33d4b78437ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.