Documentation ¶
Index ¶
Constants ¶
View Source
const ( // APIKeyHeaderKey is used to pass the API Key to the backend APIKeyHeaderKey = "x-api-key" // TestHeaderKey is used to force backend to return non-OK status. // Refer to tests/endpoints/bookstore_grpc/grpc_server.js for detail. TestHeaderKey = "x-grpc-test" )
Variables ¶
View Source
var MakeBookstoreV2GrpcCall = func(addr, method string, header http.Header) (string, error) { var opts []grpc.DialOption opts = append(opts, grpc.WithInsecure()) conn, err := grpc.Dial(addr, opts...) if err != nil { return "", fmt.Errorf("failed to connect to server: %v", err) } defer conn.Close() cli := bsgrpcv2.NewBookstoreClient(conn) ctx := context.Background() for key, valueList := range header { for _, value := range valueList { ctx = metadata.AppendToOutgoingContext(ctx, key, value) } } var respMsg proto.Message switch method { case "GetShelf": req := &bspbv2.GetShelfRequest{ Shelf: 100, } respMsg, err = cli.GetShelf(ctx, req) default: return "", fmt.Errorf("unexpected method called") } if err != nil { return "", fmt.Errorf("%v got unexpected error: %v", method, err) } var marshaler jsonpb.Marshaler return marshaler.MarshalToString(respMsg) }
View Source
var MakeHttpCallWithBody = func(addr, httpMethod, method, token string, bodyBytes []byte) (string, error) { var cli http.Client var req *http.Request if bodyBytes == nil { req, _ = http.NewRequest(httpMethod, fmt.Sprintf("http://%s%s", addr, method), nil) } else { req, _ = http.NewRequest(httpMethod, fmt.Sprintf("http://%s%s", addr, method), bytes.NewBuffer(bodyBytes)) } req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) resp, err := cli.Do(req) if err != nil { return "", fmt.Errorf("http got error: %v", err) } defer resp.Body.Close() content, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } if resp.StatusCode != http.StatusOK { return "", fmt.Errorf("http response status is not 200 OK: %s, %s", resp.Status, utils.RpcStatusDeterministicJsonFormat(content)) } return string(content), nil }
View Source
var MakeHttpCallWithDecode = func(addr, httpMethod, method, token string, header http.Header) (string, string, error) { var cli http.Client req, _ := http.NewRequest(httpMethod, fmt.Sprintf("http://%s%s", addr, method), nil) if token != "" { req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) } addAllHeaders(req, header) resp, err := cli.Do(req) if err != nil { return "", "", fmt.Errorf("http got error: %v", err) } defer resp.Body.Close() encoding := getContentEncoding(resp.Header) var content []byte if encoding == "gzip" { gzipR, err := gzip.NewReader(resp.Body) if err != nil { return "", "", err } content, err = ioutil.ReadAll(gzipR) } else if encoding == "br" { content, err = ioutil.ReadAll(cbrotli.NewReader(resp.Body)) } else { content, err = ioutil.ReadAll(resp.Body) } if err != nil { return "", "", err } if resp.StatusCode != http.StatusOK { return "", "", fmt.Errorf("http response status is not 200 OK: %s, %s", resp.Status, utils.RpcStatusDeterministicJsonFormat(content)) } return string(content), encoding, nil }
Functions ¶
func EncodeGRPCWebRequestBody ¶
EncodeGRPCWebRequestBody returns an encoded reader given a request message.
func MakeCall ¶
func MakeCall(clientProtocol, addr, httpMethod, method, token string, header http.Header) (string, error)
MakeCall returns response in JSON. For gRPC-web, use MakeGRPCWebCall instead.
For HTTP, add client.TestHeaderKey to header force the backend to return non-OK status.
func MakeTokenInQueryCall ¶
Types ¶
type GRPCWebTrailer ¶
GRPCWebTrailer represents key-value pairs in gRPC-Web trailer.
func DecodeGRPCWebResponseBody ¶
func DecodeGRPCWebResponseBody(body io.Reader) ([]byte, GRPCWebTrailer, error)
DecodeGRPCWebResponseBody returns a decoded message and a trailer given a request body.
func MakeGRPCWebCall ¶
func MakeGRPCWebCall(addr, method, token string, header http.Header) (string, GRPCWebTrailer, error)
MakeGRPCWebCall returns response in JSON and gRPC-Web trailer. Add client.TestHeaderKey to header to force the backend to return a non-OK status.
Click to show internal directories.
Click to hide internal directories.