// Code generated by protoc-gen-twirp v8.1.3, DO NOT EDIT. // source: spec/proto/client_query.proto package protocol import context "context" import fmt "fmt" import http "net/http" import io "io" import json "encoding/json" import strconv "strconv" import strings "strings" import protojson "google.golang.org/protobuf/encoding/protojson" import twirp "github.com/twitchtv/twirp" import ctxsetters "github.com/twitchtv/twirp/ctxsetters" // Version compatibility assertion. // If the constant is not defined in the package, that likely means // the package needs to be updated to work with this generated code. // See https://twitchtv.github.io/twirp/docs/version_matrix.html const _ = twirp.TwirpPackageMinVersion_8_1_0 // ============================ // ClientQueryService Interface // ============================ type ClientQueryService interface { ListTunnels(context.Context, *ListTunnelsRequest) (*ListTunnelsResponse, error) } // ================================== // ClientQueryService Protobuf Client // ================================== type clientQueryServiceProtobufClient struct { client HTTPClient urls [1]string interceptor twirp.Interceptor opts twirp.ClientOptions } // NewClientQueryServiceProtobufClient creates a Protobuf client that implements the ClientQueryService interface. // It communicates using Protobuf and can be configured with a custom HTTPClient. func NewClientQueryServiceProtobufClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) ClientQueryService { if c, ok := client.(*http.Client); ok { client = withoutRedirects(c) } clientOpts := twirp.ClientOptions{} for _, o := range opts { o(&clientOpts) } // Using ReadOpt allows backwards and forwards compatibility with new options in the future literalURLs := false _ = clientOpts.ReadOpt("literalURLs", &literalURLs) var pathPrefix string if ok := clientOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { pathPrefix = "/twirp" // default prefix } // Build method URLs: []/./ serviceURL := sanitizeBaseURL(baseURL) serviceURL += baseServicePath(pathPrefix, "protocol", "ClientQueryService") urls := [1]string{ serviceURL + "ListTunnels", } return &clientQueryServiceProtobufClient{ client: client, urls: urls, interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...), opts: clientOpts, } } func (c *clientQueryServiceProtobufClient) ListTunnels(ctx context.Context, in *ListTunnelsRequest) (*ListTunnelsResponse, error) { ctx = ctxsetters.WithPackageName(ctx, "protocol") ctx = ctxsetters.WithServiceName(ctx, "ClientQueryService") ctx = ctxsetters.WithMethodName(ctx, "ListTunnels") caller := c.callListTunnels if c.interceptor != nil { caller = func(ctx context.Context, req *ListTunnelsRequest) (*ListTunnelsResponse, error) { resp, err := c.interceptor( func(ctx context.Context, req interface{}) (interface{}, error) { typedReq, ok := req.(*ListTunnelsRequest) if !ok { return nil, twirp.InternalError("failed type assertion req.(*ListTunnelsRequest) when calling interceptor") } return c.callListTunnels(ctx, typedReq) }, )(ctx, req) if resp != nil { typedResp, ok := resp.(*ListTunnelsResponse) if !ok { return nil, twirp.InternalError("failed type assertion resp.(*ListTunnelsResponse) when calling interceptor") } return typedResp, err } return nil, err } } return caller(ctx, in) } func (c *clientQueryServiceProtobufClient) callListTunnels(ctx context.Context, in *ListTunnelsRequest) (*ListTunnelsResponse, error) { out := new(ListTunnelsResponse) ctx, err := doProtobufRequest(ctx, c.client, c.opts.Hooks, c.urls[0], in, out) if err != nil { twerr, ok := err.(twirp.Error) if !ok { twerr = twirp.InternalErrorWith(err) } callClientError(ctx, c.opts.Hooks, twerr) return nil, err } callClientResponseReceived(ctx, c.opts.Hooks) return out, nil } // ============================== // ClientQueryService JSON Client // ============================== type clientQueryServiceJSONClient struct { client HTTPClient urls [1]string interceptor twirp.Interceptor opts twirp.ClientOptions } // NewClientQueryServiceJSONClient creates a JSON client that implements the ClientQueryService interface. // It communicates using JSON and can be configured with a custom HTTPClient. func NewClientQueryServiceJSONClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) ClientQueryService { if c, ok := client.(*http.Client); ok { client = withoutRedirects(c) } clientOpts := twirp.ClientOptions{} for _, o := range opts { o(&clientOpts) } // Using ReadOpt allows backwards and forwards compatibility with new options in the future literalURLs := false _ = clientOpts.ReadOpt("literalURLs", &literalURLs) var pathPrefix string if ok := clientOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { pathPrefix = "/twirp" // default prefix } // Build method URLs: []/./ serviceURL := sanitizeBaseURL(baseURL) serviceURL += baseServicePath(pathPrefix, "protocol", "ClientQueryService") urls := [1]string{ serviceURL + "ListTunnels", } return &clientQueryServiceJSONClient{ client: client, urls: urls, interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...), opts: clientOpts, } } func (c *clientQueryServiceJSONClient) ListTunnels(ctx context.Context, in *ListTunnelsRequest) (*ListTunnelsResponse, error) { ctx = ctxsetters.WithPackageName(ctx, "protocol") ctx = ctxsetters.WithServiceName(ctx, "ClientQueryService") ctx = ctxsetters.WithMethodName(ctx, "ListTunnels") caller := c.callListTunnels if c.interceptor != nil { caller = func(ctx context.Context, req *ListTunnelsRequest) (*ListTunnelsResponse, error) { resp, err := c.interceptor( func(ctx context.Context, req interface{}) (interface{}, error) { typedReq, ok := req.(*ListTunnelsRequest) if !ok { return nil, twirp.InternalError("failed type assertion req.(*ListTunnelsRequest) when calling interceptor") } return c.callListTunnels(ctx, typedReq) }, )(ctx, req) if resp != nil { typedResp, ok := resp.(*ListTunnelsResponse) if !ok { return nil, twirp.InternalError("failed type assertion resp.(*ListTunnelsResponse) when calling interceptor") } return typedResp, err } return nil, err } } return caller(ctx, in) } func (c *clientQueryServiceJSONClient) callListTunnels(ctx context.Context, in *ListTunnelsRequest) (*ListTunnelsResponse, error) { out := new(ListTunnelsResponse) ctx, err := doJSONRequest(ctx, c.client, c.opts.Hooks, c.urls[0], in, out) if err != nil { twerr, ok := err.(twirp.Error) if !ok { twerr = twirp.InternalErrorWith(err) } callClientError(ctx, c.opts.Hooks, twerr) return nil, err } callClientResponseReceived(ctx, c.opts.Hooks) return out, nil } // ================================= // ClientQueryService Server Handler // ================================= type clientQueryServiceServer struct { ClientQueryService interceptor twirp.Interceptor hooks *twirp.ServerHooks pathPrefix string // prefix for routing jsonSkipDefaults bool // do not include unpopulated fields (default values) in the response jsonCamelCase bool // JSON fields are serialized as lowerCamelCase rather than keeping the original proto names } // NewClientQueryServiceServer builds a TwirpServer that can be used as an http.Handler to handle // HTTP requests that are routed to the right method in the provided svc implementation. // The opts are twirp.ServerOption modifiers, for example twirp.WithServerHooks(hooks). func NewClientQueryServiceServer(svc ClientQueryService, opts ...interface{}) TwirpServer { serverOpts := newServerOpts(opts) // Using ReadOpt allows backwards and forwards compatibility with new options in the future jsonSkipDefaults := false _ = serverOpts.ReadOpt("jsonSkipDefaults", &jsonSkipDefaults) jsonCamelCase := false _ = serverOpts.ReadOpt("jsonCamelCase", &jsonCamelCase) var pathPrefix string if ok := serverOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { pathPrefix = "/twirp" // default prefix } return &clientQueryServiceServer{ ClientQueryService: svc, hooks: serverOpts.Hooks, interceptor: twirp.ChainInterceptors(serverOpts.Interceptors...), pathPrefix: pathPrefix, jsonSkipDefaults: jsonSkipDefaults, jsonCamelCase: jsonCamelCase, } } // writeError writes an HTTP response with a valid Twirp error format, and triggers hooks. // If err is not a twirp.Error, it will get wrapped with twirp.InternalErrorWith(err) func (s *clientQueryServiceServer) writeError(ctx context.Context, resp http.ResponseWriter, err error) { writeError(ctx, resp, err, s.hooks) } // handleRequestBodyError is used to handle error when the twirp server cannot read request func (s *clientQueryServiceServer) handleRequestBodyError(ctx context.Context, resp http.ResponseWriter, msg string, err error) { if context.Canceled == ctx.Err() { s.writeError(ctx, resp, twirp.NewError(twirp.Canceled, "failed to read request: context canceled")) return } if context.DeadlineExceeded == ctx.Err() { s.writeError(ctx, resp, twirp.NewError(twirp.DeadlineExceeded, "failed to read request: deadline exceeded")) return } s.writeError(ctx, resp, twirp.WrapError(malformedRequestError(msg), err)) } // ClientQueryServicePathPrefix is a convenience constant that may identify URL paths. // Should be used with caution, it only matches routes generated by Twirp Go clients, // with the default "/twirp" prefix and default CamelCase service and method names. // More info: https://twitchtv.github.io/twirp/docs/routing.html const ClientQueryServicePathPrefix = "/twirp/protocol.ClientQueryService/" func (s *clientQueryServiceServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) { ctx := req.Context() ctx = ctxsetters.WithPackageName(ctx, "protocol") ctx = ctxsetters.WithServiceName(ctx, "ClientQueryService") ctx = ctxsetters.WithResponseWriter(ctx, resp) var err error ctx, err = callRequestReceived(ctx, s.hooks) if err != nil { s.writeError(ctx, resp, err) return } if req.Method != "POST" { msg := fmt.Sprintf("unsupported method %q (only POST is allowed)", req.Method) s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) return } // Verify path format: []/./ prefix, pkgService, method := parseTwirpPath(req.URL.Path) if pkgService != "protocol.ClientQueryService" { msg := fmt.Sprintf("no handler for path %q", req.URL.Path) s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) return } if prefix != s.pathPrefix { msg := fmt.Sprintf("invalid path prefix %q, expected %q, on path %q", prefix, s.pathPrefix, req.URL.Path) s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) return } switch method { case "ListTunnels": s.serveListTunnels(ctx, resp, req) return default: msg := fmt.Sprintf("no handler for path %q", req.URL.Path) s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) return } } func (s *clientQueryServiceServer) serveListTunnels(ctx context.Context, resp http.ResponseWriter, req *http.Request) { header := req.Header.Get("Content-Type") i := strings.Index(header, ";") if i == -1 { i = len(header) } switch strings.TrimSpace(strings.ToLower(header[:i])) { case "application/json": s.serveListTunnelsJSON(ctx, resp, req) case "application/protobuf": s.serveListTunnelsProtobuf(ctx, resp, req) default: msg := fmt.Sprintf("unexpected Content-Type: %q", req.Header.Get("Content-Type")) twerr := badRouteError(msg, req.Method, req.URL.Path) s.writeError(ctx, resp, twerr) } } func (s *clientQueryServiceServer) serveListTunnelsJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) { var err error ctx = ctxsetters.WithMethodName(ctx, "ListTunnels") ctx, err = callRequestRouted(ctx, s.hooks) if err != nil { s.writeError(ctx, resp, err) return } d := json.NewDecoder(req.Body) rawReqBody := json.RawMessage{} if err := d.Decode(&rawReqBody); err != nil { s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) return } reqContent := new(ListTunnelsRequest) unmarshaler := protojson.UnmarshalOptions{DiscardUnknown: true} if err = unmarshaler.Unmarshal(rawReqBody, reqContent); err != nil { s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) return } handler := s.ClientQueryService.ListTunnels if s.interceptor != nil { handler = func(ctx context.Context, req *ListTunnelsRequest) (*ListTunnelsResponse, error) { resp, err := s.interceptor( func(ctx context.Context, req interface{}) (interface{}, error) { typedReq, ok := req.(*ListTunnelsRequest) if !ok { return nil, twirp.InternalError("failed type assertion req.(*ListTunnelsRequest) when calling interceptor") } return s.ClientQueryService.ListTunnels(ctx, typedReq) }, )(ctx, req) if resp != nil { typedResp, ok := resp.(*ListTunnelsResponse) if !ok { return nil, twirp.InternalError("failed type assertion resp.(*ListTunnelsResponse) when calling interceptor") } return typedResp, err } return nil, err } } // Call service method var respContent *ListTunnelsResponse func() { defer ensurePanicResponses(ctx, resp, s.hooks) respContent, err = handler(ctx, reqContent) }() if err != nil { s.writeError(ctx, resp, err) return } if respContent == nil { s.writeError(ctx, resp, twirp.InternalError("received a nil *ListTunnelsResponse and nil error while calling ListTunnels. nil responses are not supported")) return } ctx = callResponsePrepared(ctx, s.hooks) marshaler := &protojson.MarshalOptions{UseProtoNames: !s.jsonCamelCase, EmitUnpopulated: !s.jsonSkipDefaults} respBytes, err := marshaler.Marshal(respContent) if err != nil { s.writeError(ctx, resp, wrapInternal(err, "failed to marshal json response")) return } ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) resp.Header().Set("Content-Type", "application/json") resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) resp.WriteHeader(http.StatusOK) if n, err := resp.Write(respBytes); err != nil { msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) twerr := twirp.NewError(twirp.Unknown, msg) ctx = callError(ctx, s.hooks, twerr) } callResponseSent(ctx, s.hooks) } func (s *clientQueryServiceServer) serveListTunnelsProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) { var err error ctx = ctxsetters.WithMethodName(ctx, "ListTunnels") ctx, err = callRequestRouted(ctx, s.hooks) if err != nil { s.writeError(ctx, resp, err) return } buf, err := io.ReadAll(req.Body) if err != nil { s.handleRequestBodyError(ctx, resp, "failed to read request body", err) return } reqContent := new(ListTunnelsRequest) if err = reqContent.UnmarshalVT(buf); err != nil { s.writeError(ctx, resp, malformedRequestError("the protobuf request could not be decoded")) return } handler := s.ClientQueryService.ListTunnels if s.interceptor != nil { handler = func(ctx context.Context, req *ListTunnelsRequest) (*ListTunnelsResponse, error) { resp, err := s.interceptor( func(ctx context.Context, req interface{}) (interface{}, error) { typedReq, ok := req.(*ListTunnelsRequest) if !ok { return nil, twirp.InternalError("failed type assertion req.(*ListTunnelsRequest) when calling interceptor") } return s.ClientQueryService.ListTunnels(ctx, typedReq) }, )(ctx, req) if resp != nil { typedResp, ok := resp.(*ListTunnelsResponse) if !ok { return nil, twirp.InternalError("failed type assertion resp.(*ListTunnelsResponse) when calling interceptor") } return typedResp, err } return nil, err } } // Call service method var respContent *ListTunnelsResponse func() { defer ensurePanicResponses(ctx, resp, s.hooks) respContent, err = handler(ctx, reqContent) }() if err != nil { s.writeError(ctx, resp, err) return } if respContent == nil { s.writeError(ctx, resp, twirp.InternalError("received a nil *ListTunnelsResponse and nil error while calling ListTunnels. nil responses are not supported")) return } ctx = callResponsePrepared(ctx, s.hooks) respBytes, err := respContent.MarshalVT() if err != nil { s.writeError(ctx, resp, wrapInternal(err, "failed to marshal proto response")) return } ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) resp.Header().Set("Content-Type", "application/protobuf") resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) resp.WriteHeader(http.StatusOK) if n, err := resp.Write(respBytes); err != nil { msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) twerr := twirp.NewError(twirp.Unknown, msg) ctx = callError(ctx, s.hooks, twerr) } callResponseSent(ctx, s.hooks) } func (s *clientQueryServiceServer) ServiceDescriptor() ([]byte, int) { return twirpFileDescriptor1, 0 } func (s *clientQueryServiceServer) ProtocGenTwirpVersion() string { return "v8.1.3" } // PathPrefix returns the base service path, in the form: "//./" // that is everything in a Twirp route except for the . This can be used for routing, // for example to identify the requests that are targeted to this service in a mux. func (s *clientQueryServiceServer) PathPrefix() string { return baseServicePath(s.pathPrefix, "protocol", "ClientQueryService") } var twirpFileDescriptor1 = []byte{ // 236 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x90, 0xc1, 0x4a, 0xc3, 0x40, 0x10, 0x86, 0x89, 0x42, 0xad, 0x53, 0x4f, 0xa3, 0x94, 0x50, 0x2c, 0x94, 0x5c, 0x2c, 0x08, 0x1b, 0xa9, 0x6f, 0x50, 0x0f, 0x8a, 0x78, 0x31, 0x7a, 0xf2, 0xa2, 0x71, 0x19, 0x62, 0x20, 0xdd, 0xd9, 0xee, 0x4c, 0x04, 0xdf, 0x5e, 0xba, 0x69, 0x6d, 0x44, 0x3c, 0xed, 0xce, 0xff, 0x2d, 0x3f, 0xdf, 0x0e, 0x4c, 0xc5, 0x93, 0xcd, 0x7d, 0x60, 0xe5, 0xdc, 0x36, 0x35, 0x39, 0x7d, 0x5d, 0xb7, 0x14, 0xbe, 0x4c, 0x8c, 0x70, 0x18, 0x0f, 0xcb, 0x4d, 0xb6, 0x84, 0x93, 0x9b, 0xc8, 0x9f, 0x5b, 0xe7, 0xa8, 0xc1, 0x09, 0x0c, 0x3f, 0x58, 0xd4, 0x95, 0x2b, 0x4a, 0x93, 0x59, 0x32, 0x3f, 0x2e, 0x7e, 0x66, 0x1c, 0xc3, 0x40, 0xcb, 0x50, 0x91, 0xa6, 0x07, 0x91, 0x6c, 0xa7, 0xec, 0x0c, 0xf0, 0xa1, 0x96, 0x6d, 0x83, 0x14, 0xb4, 0x6e, 0x49, 0x34, 0xbb, 0x85, 0xd3, 0x5f, 0xa9, 0x78, 0x76, 0x42, 0x78, 0x05, 0x47, 0xda, 0x45, 0x69, 0x32, 0x3b, 0x9c, 0x8f, 0x16, 0x63, 0xb3, 0x93, 0x31, 0x7d, 0x93, 0x62, 0xf7, 0x6c, 0xf1, 0x06, 0xd8, 0x81, 0xc7, 0xcd, 0x0f, 0x9e, 0x28, 0x7c, 0xd6, 0x96, 0xf0, 0x1e, 0x46, 0xbd, 0x7a, 0x3c, 0xdf, 0xb7, 0xfc, 0x75, 0x99, 0x4c, 0xff, 0xa1, 0x9d, 0xd3, 0xf2, 0xf2, 0x2e, 0x79, 0xb9, 0xa8, 0xd8, 0xac, 0xea, 0x50, 0x56, 0x24, 0xbe, 0xb4, 0x64, 0x2c, 0xe7, 0x9b, 0x1d, 0x2a, 0x85, 0x7c, 0xbf, 0x4b, 0xcb, 0xcd, 0xfb, 0x20, 0xde, 0xae, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xca, 0x06, 0x49, 0xa5, 0x63, 0x01, 0x00, 0x00, }