// Copyright 2020-2021 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Code generated by protoc-gen-twirp v7.1.0, DO NOT EDIT.
// source: buf/alpha/registry/v1alpha1/repository_branch.proto

package registryv1alpha1

import bytes "bytes"
import strings "strings"
import context "context"
import fmt "fmt"
import ioutil "io/ioutil"
import http "net/http"
import strconv "strconv"

import jsonpb "github.com/golang/protobuf/jsonpb"
import proto "github.com/golang/protobuf/proto"
import twirp "github.com/twitchtv/twirp"
import ctxsetters "github.com/twitchtv/twirp/ctxsetters"

// This is a compile-time assertion to ensure that this generated file
// is compatible with the twirp package used in your project.
// A compilation error at this line likely means your copy of the
// twirp package needs to be updated.
const _ = twirp.TwirpPackageIsVersion7

// =================================
// RepositoryBranchService Interface
// =================================

// RepositoryBranchService is the Repository branch service.
// All methods on the Repository branch service require authentication.
type RepositoryBranchService interface {
	// CreateRepositoryBranch creates a new repository branch.
	CreateRepositoryBranch(context.Context, *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error)

	// ListRepositoryBranches lists the repository branches associated with a Repository.
	ListRepositoryBranches(context.Context, *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error)
}

// =======================================
// RepositoryBranchService Protobuf Client
// =======================================

type repositoryBranchServiceProtobufClient struct {
	client      HTTPClient
	urls        [2]string
	interceptor twirp.Interceptor
	opts        twirp.ClientOptions
}

// NewRepositoryBranchServiceProtobufClient creates a Protobuf client that implements the RepositoryBranchService interface.
// It communicates using Protobuf and can be configured with a custom HTTPClient.
func NewRepositoryBranchServiceProtobufClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) RepositoryBranchService {
	if c, ok := client.(*http.Client); ok {
		client = withoutRedirects(c)
	}

	clientOpts := twirp.ClientOptions{}
	for _, o := range opts {
		o(&clientOpts)
	}

	// Build method URLs: <baseURL>[<prefix>]/<package>.<Service>/<Method>
	serviceURL := sanitizeBaseURL(baseURL)
	serviceURL += baseServicePath(clientOpts.PathPrefix(), "buf.alpha.registry.v1alpha1", "RepositoryBranchService")
	urls := [2]string{
		serviceURL + "CreateRepositoryBranch",
		serviceURL + "ListRepositoryBranches",
	}

	return &repositoryBranchServiceProtobufClient{
		client:      client,
		urls:        urls,
		interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...),
		opts:        clientOpts,
	}
}

func (c *repositoryBranchServiceProtobufClient) CreateRepositoryBranch(ctx context.Context, in *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error) {
	ctx = ctxsetters.WithPackageName(ctx, "buf.alpha.registry.v1alpha1")
	ctx = ctxsetters.WithServiceName(ctx, "RepositoryBranchService")
	ctx = ctxsetters.WithMethodName(ctx, "CreateRepositoryBranch")
	caller := c.callCreateRepositoryBranch
	if c.interceptor != nil {
		caller = func(ctx context.Context, req *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error) {
			resp, err := c.interceptor(
				func(ctx context.Context, req interface{}) (interface{}, error) {
					typedReq, ok := req.(*CreateRepositoryBranchRequest)
					if !ok {
						return nil, twirp.InternalError("failed type assertion req.(*CreateRepositoryBranchRequest) when calling interceptor")
					}
					return c.callCreateRepositoryBranch(ctx, typedReq)
				},
			)(ctx, req)
			if resp != nil {
				typedResp, ok := resp.(*CreateRepositoryBranchResponse)
				if !ok {
					return nil, twirp.InternalError("failed type assertion resp.(*CreateRepositoryBranchResponse) when calling interceptor")
				}
				return typedResp, err
			}
			return nil, err
		}
	}
	return caller(ctx, in)
}

func (c *repositoryBranchServiceProtobufClient) callCreateRepositoryBranch(ctx context.Context, in *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error) {
	out := new(CreateRepositoryBranchResponse)
	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
}

func (c *repositoryBranchServiceProtobufClient) ListRepositoryBranches(ctx context.Context, in *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error) {
	ctx = ctxsetters.WithPackageName(ctx, "buf.alpha.registry.v1alpha1")
	ctx = ctxsetters.WithServiceName(ctx, "RepositoryBranchService")
	ctx = ctxsetters.WithMethodName(ctx, "ListRepositoryBranches")
	caller := c.callListRepositoryBranches
	if c.interceptor != nil {
		caller = func(ctx context.Context, req *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error) {
			resp, err := c.interceptor(
				func(ctx context.Context, req interface{}) (interface{}, error) {
					typedReq, ok := req.(*ListRepositoryBranchesRequest)
					if !ok {
						return nil, twirp.InternalError("failed type assertion req.(*ListRepositoryBranchesRequest) when calling interceptor")
					}
					return c.callListRepositoryBranches(ctx, typedReq)
				},
			)(ctx, req)
			if resp != nil {
				typedResp, ok := resp.(*ListRepositoryBranchesResponse)
				if !ok {
					return nil, twirp.InternalError("failed type assertion resp.(*ListRepositoryBranchesResponse) when calling interceptor")
				}
				return typedResp, err
			}
			return nil, err
		}
	}
	return caller(ctx, in)
}

func (c *repositoryBranchServiceProtobufClient) callListRepositoryBranches(ctx context.Context, in *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error) {
	out := new(ListRepositoryBranchesResponse)
	ctx, err := doProtobufRequest(ctx, c.client, c.opts.Hooks, c.urls[1], 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
}

// ===================================
// RepositoryBranchService JSON Client
// ===================================

type repositoryBranchServiceJSONClient struct {
	client      HTTPClient
	urls        [2]string
	interceptor twirp.Interceptor
	opts        twirp.ClientOptions
}

// NewRepositoryBranchServiceJSONClient creates a JSON client that implements the RepositoryBranchService interface.
// It communicates using JSON and can be configured with a custom HTTPClient.
func NewRepositoryBranchServiceJSONClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) RepositoryBranchService {
	if c, ok := client.(*http.Client); ok {
		client = withoutRedirects(c)
	}

	clientOpts := twirp.ClientOptions{}
	for _, o := range opts {
		o(&clientOpts)
	}

	// Build method URLs: <baseURL>[<prefix>]/<package>.<Service>/<Method>
	serviceURL := sanitizeBaseURL(baseURL)
	serviceURL += baseServicePath(clientOpts.PathPrefix(), "buf.alpha.registry.v1alpha1", "RepositoryBranchService")
	urls := [2]string{
		serviceURL + "CreateRepositoryBranch",
		serviceURL + "ListRepositoryBranches",
	}

	return &repositoryBranchServiceJSONClient{
		client:      client,
		urls:        urls,
		interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...),
		opts:        clientOpts,
	}
}

func (c *repositoryBranchServiceJSONClient) CreateRepositoryBranch(ctx context.Context, in *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error) {
	ctx = ctxsetters.WithPackageName(ctx, "buf.alpha.registry.v1alpha1")
	ctx = ctxsetters.WithServiceName(ctx, "RepositoryBranchService")
	ctx = ctxsetters.WithMethodName(ctx, "CreateRepositoryBranch")
	caller := c.callCreateRepositoryBranch
	if c.interceptor != nil {
		caller = func(ctx context.Context, req *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error) {
			resp, err := c.interceptor(
				func(ctx context.Context, req interface{}) (interface{}, error) {
					typedReq, ok := req.(*CreateRepositoryBranchRequest)
					if !ok {
						return nil, twirp.InternalError("failed type assertion req.(*CreateRepositoryBranchRequest) when calling interceptor")
					}
					return c.callCreateRepositoryBranch(ctx, typedReq)
				},
			)(ctx, req)
			if resp != nil {
				typedResp, ok := resp.(*CreateRepositoryBranchResponse)
				if !ok {
					return nil, twirp.InternalError("failed type assertion resp.(*CreateRepositoryBranchResponse) when calling interceptor")
				}
				return typedResp, err
			}
			return nil, err
		}
	}
	return caller(ctx, in)
}

func (c *repositoryBranchServiceJSONClient) callCreateRepositoryBranch(ctx context.Context, in *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error) {
	out := new(CreateRepositoryBranchResponse)
	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
}

func (c *repositoryBranchServiceJSONClient) ListRepositoryBranches(ctx context.Context, in *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error) {
	ctx = ctxsetters.WithPackageName(ctx, "buf.alpha.registry.v1alpha1")
	ctx = ctxsetters.WithServiceName(ctx, "RepositoryBranchService")
	ctx = ctxsetters.WithMethodName(ctx, "ListRepositoryBranches")
	caller := c.callListRepositoryBranches
	if c.interceptor != nil {
		caller = func(ctx context.Context, req *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error) {
			resp, err := c.interceptor(
				func(ctx context.Context, req interface{}) (interface{}, error) {
					typedReq, ok := req.(*ListRepositoryBranchesRequest)
					if !ok {
						return nil, twirp.InternalError("failed type assertion req.(*ListRepositoryBranchesRequest) when calling interceptor")
					}
					return c.callListRepositoryBranches(ctx, typedReq)
				},
			)(ctx, req)
			if resp != nil {
				typedResp, ok := resp.(*ListRepositoryBranchesResponse)
				if !ok {
					return nil, twirp.InternalError("failed type assertion resp.(*ListRepositoryBranchesResponse) when calling interceptor")
				}
				return typedResp, err
			}
			return nil, err
		}
	}
	return caller(ctx, in)
}

func (c *repositoryBranchServiceJSONClient) callListRepositoryBranches(ctx context.Context, in *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error) {
	out := new(ListRepositoryBranchesResponse)
	ctx, err := doJSONRequest(ctx, c.client, c.opts.Hooks, c.urls[1], 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
}

// ======================================
// RepositoryBranchService Server Handler
// ======================================

type repositoryBranchServiceServer struct {
	RepositoryBranchService
	interceptor      twirp.Interceptor
	hooks            *twirp.ServerHooks
	pathPrefix       string // prefix for routing
	jsonSkipDefaults bool   // do not include unpopulated fields (default values) in the response
}

// NewRepositoryBranchServiceServer 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 NewRepositoryBranchServiceServer(svc RepositoryBranchService, opts ...interface{}) TwirpServer {
	serverOpts := twirp.ServerOptions{}
	for _, opt := range opts {
		switch o := opt.(type) {
		case twirp.ServerOption:
			o(&serverOpts)
		case *twirp.ServerHooks: // backwards compatibility, allow to specify hooks as an argument
			twirp.WithServerHooks(o)(&serverOpts)
		case nil: // backwards compatibility, allow nil value for the argument
			continue
		default:
			panic(fmt.Sprintf("Invalid option type %T on NewRepositoryBranchServiceServer", o))
		}
	}

	return &repositoryBranchServiceServer{
		RepositoryBranchService: svc,
		pathPrefix:              serverOpts.PathPrefix(),
		interceptor:             twirp.ChainInterceptors(serverOpts.Interceptors...),
		hooks:                   serverOpts.Hooks,
		jsonSkipDefaults:        serverOpts.JSONSkipDefaults,
	}
}

// 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 *repositoryBranchServiceServer) writeError(ctx context.Context, resp http.ResponseWriter, err error) {
	writeError(ctx, resp, err, s.hooks)
}

// RepositoryBranchServicePathPrefix is a convenience constant that could used to identify URL paths.
// Should be used with caution, it only matches routes generated by Twirp Go clients,
// that add a "/twirp" prefix by default, and use CamelCase service and method names.
// More info: https://twitchtv.github.io/twirp/docs/routing.html
const RepositoryBranchServicePathPrefix = "/twirp/buf.alpha.registry.v1alpha1.RepositoryBranchService/"

func (s *repositoryBranchServiceServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	ctx := req.Context()
	ctx = ctxsetters.WithPackageName(ctx, "buf.alpha.registry.v1alpha1")
	ctx = ctxsetters.WithServiceName(ctx, "RepositoryBranchService")
	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>]/<package>.<Service>/<Method>
	prefix, pkgService, method := parseTwirpPath(req.URL.Path)
	if pkgService != "buf.alpha.registry.v1alpha1.RepositoryBranchService" {
		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 "CreateRepositoryBranch":
		s.serveCreateRepositoryBranch(ctx, resp, req)
		return
	case "ListRepositoryBranches":
		s.serveListRepositoryBranches(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 *repositoryBranchServiceServer) serveCreateRepositoryBranch(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.serveCreateRepositoryBranchJSON(ctx, resp, req)
	case "application/protobuf":
		s.serveCreateRepositoryBranchProtobuf(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 *repositoryBranchServiceServer) serveCreateRepositoryBranchJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
	var err error
	ctx = ctxsetters.WithMethodName(ctx, "CreateRepositoryBranch")
	ctx, err = callRequestRouted(ctx, s.hooks)
	if err != nil {
		s.writeError(ctx, resp, err)
		return
	}

	reqContent := new(CreateRepositoryBranchRequest)
	unmarshaler := jsonpb.Unmarshaler{AllowUnknownFields: true}
	if err = unmarshaler.Unmarshal(req.Body, reqContent); err != nil {
		s.writeError(ctx, resp, malformedRequestError("the json request could not be decoded"))
		return
	}

	handler := s.RepositoryBranchService.CreateRepositoryBranch
	if s.interceptor != nil {
		handler = func(ctx context.Context, req *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error) {
			resp, err := s.interceptor(
				func(ctx context.Context, req interface{}) (interface{}, error) {
					typedReq, ok := req.(*CreateRepositoryBranchRequest)
					if !ok {
						return nil, twirp.InternalError("failed type assertion req.(*CreateRepositoryBranchRequest) when calling interceptor")
					}
					return s.RepositoryBranchService.CreateRepositoryBranch(ctx, typedReq)
				},
			)(ctx, req)
			if resp != nil {
				typedResp, ok := resp.(*CreateRepositoryBranchResponse)
				if !ok {
					return nil, twirp.InternalError("failed type assertion resp.(*CreateRepositoryBranchResponse) when calling interceptor")
				}
				return typedResp, err
			}
			return nil, err
		}
	}

	// Call service method
	var respContent *CreateRepositoryBranchResponse
	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 *CreateRepositoryBranchResponse and nil error while calling CreateRepositoryBranch. nil responses are not supported"))
		return
	}

	ctx = callResponsePrepared(ctx, s.hooks)

	var buf bytes.Buffer
	marshaler := &jsonpb.Marshaler{OrigName: true, EmitDefaults: !s.jsonSkipDefaults}
	if err = marshaler.Marshal(&buf, respContent); err != nil {
		s.writeError(ctx, resp, wrapInternal(err, "failed to marshal json response"))
		return
	}

	ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK)
	respBytes := buf.Bytes()
	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 *repositoryBranchServiceServer) serveCreateRepositoryBranchProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
	var err error
	ctx = ctxsetters.WithMethodName(ctx, "CreateRepositoryBranch")
	ctx, err = callRequestRouted(ctx, s.hooks)
	if err != nil {
		s.writeError(ctx, resp, err)
		return
	}

	buf, err := ioutil.ReadAll(req.Body)
	if err != nil {
		s.writeError(ctx, resp, wrapInternal(err, "failed to read request body"))
		return
	}
	reqContent := new(CreateRepositoryBranchRequest)
	if err = proto.Unmarshal(buf, reqContent); err != nil {
		s.writeError(ctx, resp, malformedRequestError("the protobuf request could not be decoded"))
		return
	}

	handler := s.RepositoryBranchService.CreateRepositoryBranch
	if s.interceptor != nil {
		handler = func(ctx context.Context, req *CreateRepositoryBranchRequest) (*CreateRepositoryBranchResponse, error) {
			resp, err := s.interceptor(
				func(ctx context.Context, req interface{}) (interface{}, error) {
					typedReq, ok := req.(*CreateRepositoryBranchRequest)
					if !ok {
						return nil, twirp.InternalError("failed type assertion req.(*CreateRepositoryBranchRequest) when calling interceptor")
					}
					return s.RepositoryBranchService.CreateRepositoryBranch(ctx, typedReq)
				},
			)(ctx, req)
			if resp != nil {
				typedResp, ok := resp.(*CreateRepositoryBranchResponse)
				if !ok {
					return nil, twirp.InternalError("failed type assertion resp.(*CreateRepositoryBranchResponse) when calling interceptor")
				}
				return typedResp, err
			}
			return nil, err
		}
	}

	// Call service method
	var respContent *CreateRepositoryBranchResponse
	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 *CreateRepositoryBranchResponse and nil error while calling CreateRepositoryBranch. nil responses are not supported"))
		return
	}

	ctx = callResponsePrepared(ctx, s.hooks)

	respBytes, err := proto.Marshal(respContent)
	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 *repositoryBranchServiceServer) serveListRepositoryBranches(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.serveListRepositoryBranchesJSON(ctx, resp, req)
	case "application/protobuf":
		s.serveListRepositoryBranchesProtobuf(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 *repositoryBranchServiceServer) serveListRepositoryBranchesJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
	var err error
	ctx = ctxsetters.WithMethodName(ctx, "ListRepositoryBranches")
	ctx, err = callRequestRouted(ctx, s.hooks)
	if err != nil {
		s.writeError(ctx, resp, err)
		return
	}

	reqContent := new(ListRepositoryBranchesRequest)
	unmarshaler := jsonpb.Unmarshaler{AllowUnknownFields: true}
	if err = unmarshaler.Unmarshal(req.Body, reqContent); err != nil {
		s.writeError(ctx, resp, malformedRequestError("the json request could not be decoded"))
		return
	}

	handler := s.RepositoryBranchService.ListRepositoryBranches
	if s.interceptor != nil {
		handler = func(ctx context.Context, req *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error) {
			resp, err := s.interceptor(
				func(ctx context.Context, req interface{}) (interface{}, error) {
					typedReq, ok := req.(*ListRepositoryBranchesRequest)
					if !ok {
						return nil, twirp.InternalError("failed type assertion req.(*ListRepositoryBranchesRequest) when calling interceptor")
					}
					return s.RepositoryBranchService.ListRepositoryBranches(ctx, typedReq)
				},
			)(ctx, req)
			if resp != nil {
				typedResp, ok := resp.(*ListRepositoryBranchesResponse)
				if !ok {
					return nil, twirp.InternalError("failed type assertion resp.(*ListRepositoryBranchesResponse) when calling interceptor")
				}
				return typedResp, err
			}
			return nil, err
		}
	}

	// Call service method
	var respContent *ListRepositoryBranchesResponse
	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 *ListRepositoryBranchesResponse and nil error while calling ListRepositoryBranches. nil responses are not supported"))
		return
	}

	ctx = callResponsePrepared(ctx, s.hooks)

	var buf bytes.Buffer
	marshaler := &jsonpb.Marshaler{OrigName: true, EmitDefaults: !s.jsonSkipDefaults}
	if err = marshaler.Marshal(&buf, respContent); err != nil {
		s.writeError(ctx, resp, wrapInternal(err, "failed to marshal json response"))
		return
	}

	ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK)
	respBytes := buf.Bytes()
	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 *repositoryBranchServiceServer) serveListRepositoryBranchesProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
	var err error
	ctx = ctxsetters.WithMethodName(ctx, "ListRepositoryBranches")
	ctx, err = callRequestRouted(ctx, s.hooks)
	if err != nil {
		s.writeError(ctx, resp, err)
		return
	}

	buf, err := ioutil.ReadAll(req.Body)
	if err != nil {
		s.writeError(ctx, resp, wrapInternal(err, "failed to read request body"))
		return
	}
	reqContent := new(ListRepositoryBranchesRequest)
	if err = proto.Unmarshal(buf, reqContent); err != nil {
		s.writeError(ctx, resp, malformedRequestError("the protobuf request could not be decoded"))
		return
	}

	handler := s.RepositoryBranchService.ListRepositoryBranches
	if s.interceptor != nil {
		handler = func(ctx context.Context, req *ListRepositoryBranchesRequest) (*ListRepositoryBranchesResponse, error) {
			resp, err := s.interceptor(
				func(ctx context.Context, req interface{}) (interface{}, error) {
					typedReq, ok := req.(*ListRepositoryBranchesRequest)
					if !ok {
						return nil, twirp.InternalError("failed type assertion req.(*ListRepositoryBranchesRequest) when calling interceptor")
					}
					return s.RepositoryBranchService.ListRepositoryBranches(ctx, typedReq)
				},
			)(ctx, req)
			if resp != nil {
				typedResp, ok := resp.(*ListRepositoryBranchesResponse)
				if !ok {
					return nil, twirp.InternalError("failed type assertion resp.(*ListRepositoryBranchesResponse) when calling interceptor")
				}
				return typedResp, err
			}
			return nil, err
		}
	}

	// Call service method
	var respContent *ListRepositoryBranchesResponse
	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 *ListRepositoryBranchesResponse and nil error while calling ListRepositoryBranches. nil responses are not supported"))
		return
	}

	ctx = callResponsePrepared(ctx, s.hooks)

	respBytes, err := proto.Marshal(respContent)
	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 *repositoryBranchServiceServer) ServiceDescriptor() ([]byte, int) {
	return twirpFileDescriptor4, 0
}

func (s *repositoryBranchServiceServer) ProtocGenTwirpVersion() string {
	return "v7.1.0"
}

// PathPrefix returns the base service path, in the form: "/<prefix>/<package>.<Service>/"
// that is everything in a Twirp route except for the <Method>. This can be used for routing,
// for example to identify the requests that are targeted to this service in a mux.
func (s *repositoryBranchServiceServer) PathPrefix() string {
	return baseServicePath(s.pathPrefix, "buf.alpha.registry.v1alpha1", "RepositoryBranchService")
}

var twirpFileDescriptor4 = []byte{
	// 504 bytes of a gzipped FileDescriptorProto
	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x54, 0xd1, 0x6e, 0xd3, 0x30,
	0x14, 0x55, 0xb2, 0x02, 0xdb,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