Documentation ¶
Overview ¶
Package client provides a REST client for communicating with a GRPC gateway. This client is specifically made to work with github.com/johnsiilver/grpc/server .
If not using with our server, you need to use:
- Use the CustomHeaders() in order to remove "Content-Type": "application/grpc-gateway"
- Pass CompressRequests(nil) to remove gzip compression, unless your server supports it
Usage example:
u, err := url.Parse("http://208.244.233.1:8080") if err != nil { // Do something } resty := New(u) resp := &pb.YourResp{} err = resty.Call( context.Background(), "/v1/snippetsService/save", // This is the URL of the REST call, defined in your proto file &pb.YourReq{...}, // The protocol buffer you are sending resp, // The protocol buffer message you should receive ) if err != nil { // Do something }
Index ¶
Constants ¶
const ContentType = "application/grpc-gateway"
ContentType is the HTTP request header's content type for requests going to the grpc-gateway. If you are binding multiple services listeners to a port, you will need to route requests with this content-type to your runtime.ServeMux.
Variables ¶
This section is empty.
Functions ¶
func DefaultHeaders ¶
DefaultHeaders returns the default headers used in our GRPC REST client. It is recommended that if you want to apply custom headers, you modify a header generated by this function.
Types ¶
type Compressor ¶
type Compressor func(ctx context.Context, path string, headers http.Header, r io.Reader) (*http.Request, error)
Compressor creates an *http.Request that compresses the Body content. The path is the URL path, headers are the request headers and r is the io.Reader that we will write the request body with.
type Decompressor ¶
Decompressor takes an io.Reader returns an io.Reader that decompresses the content.
type GRPC ¶
type GRPC struct {
// contains filtered or unexported fields
}
GRPC is a REST client for talking with a GRPC gateway. The backend handlers will receive content-type: application/grpc-gateway.
func New ¶
New is the constructor for talking with the GRPC gateway. endpoint is a url that starts with http/https and ends with :port (https://192.168.1.1:8082) representing the grpc-gateway endpoint.
func (*GRPC) Call ¶
func (g *GRPC) Call(ctx context.Context, path string, args proto.Message, result proto.Message) error
Call calls a non-streaming RPC at path, where path is the relative URL of the call (/v1/snippetsService/save). Args is the proto message that will be sent and result is the proto message that is expected to be returned. Call will honor timeouts and cancels from the Context object.
type Option ¶
type Option func(g *GRPC)
Option provides an optional argument to the New() constructor.
func CompressRequests ¶
func CompressRequests(h Compressor) Option
CompressRequests will compress all http.Request.Body content with the Compression handler provided here. The REST service will need need to be able to understand the Content-Encoding and decompress it before grpc-gateway receives the message.
func CustomClient ¶
CustomClient allows providing a custom http client for contacting GRPC's gateway proxy. By default, we use a client with defaults set (we do not use the built in client).
func CustomHeaders ¶
CustomHeaders provides a custom header to be sent on each call. The default headers set only one value, "content-type", which is set to the constant ContentType defined in this file. This is also useful for setting JS Fetch() call options when doing WASM: https://github.com/golang/go/wiki/WebAssembly#configuring-fetch-options-while-using-nethttp
func DecompressResponse ¶
func DecompressResponse(acceptEncoding string, h Decompressor) Option
DecompressResponse allows decompression of responses sent from the server with the "acceptEncoding" using the provided Decompressor. By default, we already support gzip("gzip") and deflate("deflate"). You may provide this option multiple times for multiple acceptEncodings. Because servers tend to decompress based on order in the Accept-Encoding array, the order is important. The first use of this will be the first Accept-Encoding in the array (gzip, defalte will always be at the end). If using our server, it will choose to encode the response in whatever the request is encoded in first and then in its own preference order.