// Code generated by protoc-gen-twirp v5.3.0, DO NOT EDIT.
// source: code.justin.tv/eventbus/controlplane/rpc/audit_logs.proto

package rpc

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

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"

// ===================
// AuditLogs Interface
// ===================

type AuditLogs interface {
	GetAuditLogsForService(context.Context, *GetAuditLogsForServiceReq) (*GetAuditLogsForServiceResp, error)
}

// =========================
// AuditLogs Protobuf Client
// =========================

type auditLogsProtobufClient struct {
	client HTTPClient
	urls   [1]string
}

// NewAuditLogsProtobufClient creates a Protobuf client that implements the AuditLogs interface.
// It communicates using Protobuf and can be configured with a custom HTTPClient.
func NewAuditLogsProtobufClient(addr string, client HTTPClient) AuditLogs {
	prefix := urlBase(addr) + AuditLogsPathPrefix
	urls := [1]string{
		prefix + "GetAuditLogsForService",
	}
	if httpClient, ok := client.(*http.Client); ok {
		return &auditLogsProtobufClient{
			client: withoutRedirects(httpClient),
			urls:   urls,
		}
	}
	return &auditLogsProtobufClient{
		client: client,
		urls:   urls,
	}
}

func (c *auditLogsProtobufClient) GetAuditLogsForService(ctx context.Context, in *GetAuditLogsForServiceReq) (*GetAuditLogsForServiceResp, error) {
	ctx = ctxsetters.WithPackageName(ctx, "code.justin.tv.eventbus.controlplane")
	ctx = ctxsetters.WithServiceName(ctx, "AuditLogs")
	ctx = ctxsetters.WithMethodName(ctx, "GetAuditLogsForService")
	out := new(GetAuditLogsForServiceResp)
	err := doProtobufRequest(ctx, c.client, c.urls[0], in, out)
	return out, err
}

// =====================
// AuditLogs JSON Client
// =====================

type auditLogsJSONClient struct {
	client HTTPClient
	urls   [1]string
}

// NewAuditLogsJSONClient creates a JSON client that implements the AuditLogs interface.
// It communicates using JSON and can be configured with a custom HTTPClient.
func NewAuditLogsJSONClient(addr string, client HTTPClient) AuditLogs {
	prefix := urlBase(addr) + AuditLogsPathPrefix
	urls := [1]string{
		prefix + "GetAuditLogsForService",
	}
	if httpClient, ok := client.(*http.Client); ok {
		return &auditLogsJSONClient{
			client: withoutRedirects(httpClient),
			urls:   urls,
		}
	}
	return &auditLogsJSONClient{
		client: client,
		urls:   urls,
	}
}

func (c *auditLogsJSONClient) GetAuditLogsForService(ctx context.Context, in *GetAuditLogsForServiceReq) (*GetAuditLogsForServiceResp, error) {
	ctx = ctxsetters.WithPackageName(ctx, "code.justin.tv.eventbus.controlplane")
	ctx = ctxsetters.WithServiceName(ctx, "AuditLogs")
	ctx = ctxsetters.WithMethodName(ctx, "GetAuditLogsForService")
	out := new(GetAuditLogsForServiceResp)
	err := doJSONRequest(ctx, c.client, c.urls[0], in, out)
	return out, err
}

// ========================
// AuditLogs Server Handler
// ========================

type auditLogsServer struct {
	AuditLogs
	hooks *twirp.ServerHooks
}

func NewAuditLogsServer(svc AuditLogs, hooks *twirp.ServerHooks) TwirpServer {
	return &auditLogsServer{
		AuditLogs: svc,
		hooks:     hooks,
	}
}

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

// AuditLogsPathPrefix is used for all URL paths on a twirp AuditLogs server.
// Requests are always: POST AuditLogsPathPrefix/method
// It can be used in an HTTP mux to route twirp requests along with non-twirp requests on other routes.
const AuditLogsPathPrefix = "/twirp/code.justin.tv.eventbus.controlplane.AuditLogs/"

func (s *auditLogsServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	ctx := req.Context()
	ctx = ctxsetters.WithPackageName(ctx, "code.justin.tv.eventbus.controlplane")
	ctx = ctxsetters.WithServiceName(ctx, "AuditLogs")
	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)
		err = badRouteError(msg, req.Method, req.URL.Path)
		s.writeError(ctx, resp, err)
		return
	}

	switch req.URL.Path {
	case "/twirp/code.justin.tv.eventbus.controlplane.AuditLogs/GetAuditLogsForService":
		s.serveGetAuditLogsForService(ctx, resp, req)
		return
	default:
		msg := fmt.Sprintf("no handler for path %q", req.URL.Path)
		err = badRouteError(msg, req.Method, req.URL.Path)
		s.writeError(ctx, resp, err)
		return
	}
}

func (s *auditLogsServer) serveGetAuditLogsForService(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.serveGetAuditLogsForServiceJSON(ctx, resp, req)
	case "application/protobuf":
		s.serveGetAuditLogsForServiceProtobuf(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 *auditLogsServer) serveGetAuditLogsForServiceJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
	var err error
	ctx = ctxsetters.WithMethodName(ctx, "GetAuditLogsForService")
	ctx, err = callRequestRouted(ctx, s.hooks)
	if err != nil {
		s.writeError(ctx, resp, err)
		return
	}

	reqContent := new(GetAuditLogsForServiceReq)
	unmarshaler := jsonpb.Unmarshaler{AllowUnknownFields: true}
	if err = unmarshaler.Unmarshal(req.Body, reqContent); err != nil {
		err = wrapErr(err, "failed to parse request json")
		s.writeError(ctx, resp, twirp.InternalErrorWith(err))
		return
	}

	// Call service method
	var respContent *GetAuditLogsForServiceResp
	func() {
		defer func() {
			// In case of a panic, serve a 500 error and then panic.
			if r := recover(); r != nil {
				s.writeError(ctx, resp, twirp.InternalError("Internal service panic"))
				panic(r)
			}
		}()
		respContent, err = s.GetAuditLogsForService(ctx, reqContent)
	}()

	if err != nil {
		s.writeError(ctx, resp, err)
		return
	}
	if respContent == nil {
		s.writeError(ctx, resp, twirp.InternalError("received a nil *GetAuditLogsForServiceResp and nil error while calling GetAuditLogsForService. nil responses are not supported"))
		return
	}

	ctx = callResponsePrepared(ctx, s.hooks)

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

	ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK)
	resp.Header().Set("Content-Type", "application/json")
	resp.WriteHeader(http.StatusOK)

	respBytes := buf.Bytes()
	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)
		callError(ctx, s.hooks, twerr)
	}
	callResponseSent(ctx, s.hooks)
}

func (s *auditLogsServer) serveGetAuditLogsForServiceProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
	var err error
	ctx = ctxsetters.WithMethodName(ctx, "GetAuditLogsForService")
	ctx, err = callRequestRouted(ctx, s.hooks)
	if err != nil {
		s.writeError(ctx, resp, err)
		return
	}

	buf, err := ioutil.ReadAll(req.Body)
	if err != nil {
		err = wrapErr(err, "failed to read request body")
		s.writeError(ctx, resp, twirp.InternalErrorWith(err))
		return
	}
	reqContent := new(GetAuditLogsForServiceReq)
	if err = proto.Unmarshal(buf, reqContent); err != nil {
		err = wrapErr(err, "failed to parse request proto")
		s.writeError(ctx, resp, twirp.InternalErrorWith(err))
		return
	}

	// Call service method
	var respContent *GetAuditLogsForServiceResp
	func() {
		defer func() {
			// In case of a panic, serve a 500 error and then panic.
			if r := recover(); r != nil {
				s.writeError(ctx, resp, twirp.InternalError("Internal service panic"))
				panic(r)
			}
		}()
		respContent, err = s.GetAuditLogsForService(ctx, reqContent)
	}()

	if err != nil {
		s.writeError(ctx, resp, err)
		return
	}
	if respContent == nil {
		s.writeError(ctx, resp, twirp.InternalError("received a nil *GetAuditLogsForServiceResp and nil error while calling GetAuditLogsForService. nil responses are not supported"))
		return
	}

	ctx = callResponsePrepared(ctx, s.hooks)

	respBytes, err := proto.Marshal(respContent)
	if err != nil {
		err = wrapErr(err, "failed to marshal proto response")
		s.writeError(ctx, resp, twirp.InternalErrorWith(err))
		return
	}

	ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK)
	resp.Header().Set("Content-Type", "application/protobuf")
	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)
		callError(ctx, s.hooks, twerr)
	}
	callResponseSent(ctx, s.hooks)
}

func (s *auditLogsServer) ServiceDescriptor() ([]byte, int) {
	return twirpFileDescriptor2, 0
}

func (s *auditLogsServer) ProtocGenTwirpVersion() string {
	return "v5.3.0"
}

var twirpFileDescriptor2 = []byte{
	// 392 bytes of a gzipped FileDescriptorProto
	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x52, 0xcb, 0x6b, 0xdb, 0x30,
	0x1c, 0xc6, 0xc9, 0x92, 0xc5, 0xca, 0x76, 0x11, 0x23, 0x68, 0x1e, 0x83, 0x90, 0xed, 0x90, 0x93,
	0x04, 0xd9, 0x65, 0x8f, 0xc3, 0x1e, 0x87, 0x8d, 0xc1, 0xb6, 0x83, 0x97, 0x53, 0x2f, 0x46, 0x96,
	0x7f, 0x71, 0xdd, 0xda, 0x96, 0xaa, 0x47, 0x20, 0x7f, 0x4b, 0xff, 0x81, 0xf6, 0xbf, 0x2c, 0x96,
	0xe3, 0xa4, 0x81, 0x86, 0x06, 0x7a, 0xf3,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