tryit

package
v0.0.0-...-583a8e8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package tryit provides "Try It" functionality for invoking Connect/gRPC services.

Index

Constants

This section is empty.

Variables

View Source
var SensitiveHeaders = []string{
	"authorization",
	"cookie",
	"set-cookie",
	"proxy-authorization",
	"www-authenticate",
	"x-api-key",
	"api-key",
}

SensitiveHeaders is a list of headers that should never be logged or displayed.

Functions

func FilterHeaders

func FilterHeaders(headers map[string]string, allowlist []string) map[string]string

FilterHeaders filters headers through an allowlist. If the allowlist is empty, all headers are allowed. Returns a new map with only allowed headers (case-insensitive matching).

func IsSensitiveHeader

func IsSensitiveHeader(name string) bool

IsSensitiveHeader returns true if the header name is considered sensitive.

func MergeHeaders

func MergeHeaders(base, override map[string]string) map[string]string

MergeHeaders merges two header maps, with override taking precedence. Case is preserved from the override map.

func RedactSensitiveHeaders

func RedactSensitiveHeaders(headers map[string][]string) map[string][]string

RedactSensitiveHeaders removes sensitive header values from a header map. Returns a new map with sensitive values replaced with "[REDACTED]".

func RedactSensitiveHeadersSingle

func RedactSensitiveHeadersSingle(headers map[string]string) map[string]string

RedactSensitiveHeadersSingle is like RedactSensitiveHeaders but for map[string]string.

func ValidateJSONSize

func ValidateJSONSize(jsonBody string, maxBytes int64) error

ValidateJSONSize checks if the JSON body size is within limits.

Types

type ConnectInvoker

type ConnectInvoker struct {
	// contains filtered or unexported fields
}

ConnectInvoker implements the Invoker interface for the Connect protocol.

func NewConnectInvoker

func NewConnectInvoker() *ConnectInvoker

NewConnectInvoker creates a new Connect invoker.

func (*ConnectInvoker) Invoke

func (c *ConnectInvoker) Invoke(ctx context.Context, req *Request) (*Response, error)

Invoke executes a Connect RPC. Connect uses HTTP POST with JSON encoding to /{package}.{Service}/{Method}.

type GRPCInvoker

type GRPCInvoker struct {
}

GRPCInvoker implements the Invoker interface for the gRPC protocol.

func NewGRPCInvoker

func NewGRPCInvoker() *GRPCInvoker

NewGRPCInvoker creates a new gRPC invoker.

func (*GRPCInvoker) Invoke

func (g *GRPCInvoker) Invoke(ctx context.Context, req *Request) (*Response, error)

Invoke executes a gRPC RPC using dynamic invocation.

type GRPCWebInvoker

type GRPCWebInvoker struct {
	// contains filtered or unexported fields
}

GRPCWebInvoker implements the Invoker interface for the gRPC-Web protocol.

func NewGRPCWebInvoker

func NewGRPCWebInvoker() *GRPCWebInvoker

NewGRPCWebInvoker creates a new gRPC-Web invoker.

func (*GRPCWebInvoker) Invoke

func (g *GRPCWebInvoker) Invoke(ctx context.Context, req *Request) (*Response, error)

Invoke executes a gRPC-Web RPC. gRPC-Web uses HTTP POST with binary protobuf encoding to {baseURL}/{package.Service/Method}.

type InvocationError

type InvocationError struct {
	// Code is the error code (gRPC code or HTTP status code).
	Code int

	// Message is the error message.
	Message string

	// Details contains additional error details (e.g., gRPC error details).
	Details []string
}

InvocationError represents detailed error information from an invocation.

type Invoker

type Invoker interface {
	// Invoke executes an RPC and returns the response.
	Invoke(ctx context.Context, req *Request) (*Response, error)
}

Invoker represents a transport-agnostic RPC invoker.

type Request

type Request struct {
	// Environment is the name of the configured environment to invoke against.
	Environment string

	// MethodDescriptor is the protobuf method descriptor for the RPC.
	MethodDescriptor protoreflect.MethodDescriptor

	// JSONBody is the user-provided JSON request body (to be converted to protobuf).
	JSONBody string

	// Headers are additional HTTP/metadata headers to include with the request.
	// These should already be filtered through the header allowlist.
	Headers map[string]string

	// BaseURL is the base URL of the upstream service (from environment config).
	BaseURL string

	// Timeout is the maximum duration for the request.
	Timeout time.Duration

	// InsecureSkipVerify indicates whether to skip TLS certificate verification.
	InsecureSkipVerify bool
}

Request represents a "Try It" invocation request.

func (*Request) InputMessageDescriptor

func (r *Request) InputMessageDescriptor() protoreflect.MessageDescriptor

InputMessageDescriptor returns the descriptor for the input message type.

func (*Request) MethodFullName

func (r *Request) MethodFullName() string

MethodFullName returns the fully-qualified method name in the format "package.Service/Method".

func (*Request) OutputMessageDescriptor

func (r *Request) OutputMessageDescriptor() protoreflect.MessageDescriptor

OutputMessageDescriptor returns the descriptor for the output message type.

func (*Request) Validate

func (r *Request) Validate() error

Validate validates that a Request has all required fields.

type Response

type Response struct {
	// Status is the HTTP status code (for Connect) or gRPC status code.
	Status int

	// StatusText is a human-readable status description.
	StatusText string

	// Headers are the response headers/metadata returned by the server.
	// Sensitive headers should be redacted before returning to the user.
	Headers map[string][]string

	// JSONBody is the response body converted to JSON for display.
	JSONBody string

	// Latency is the total time taken for the request (including network and processing).
	Latency time.Duration

	// Error contains error details if the invocation failed.
	Error *InvocationError
}

Response represents the result of an RPC invocation.

type Transport

type Transport string

Transport represents the RPC transport protocol.

const (
	// TransportConnect represents the Connect protocol (JSON over HTTP).
	TransportConnect Transport = "connect"

	// TransportGRPC represents the gRPC protocol.
	TransportGRPC Transport = "grpc"

	// TransportGRPCWeb represents the gRPC-Web protocol.
	TransportGRPCWeb Transport = "grpc-web"
)

func ParseTransport

func ParseTransport(s string) (Transport, error)

ParseTransport converts a string to a Transport type.

func (Transport) String

func (t Transport) String() string

String returns the string representation of the transport.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL