json

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EventJSONHandleError event is emitted when an error occurs during JSON
	// output handling.
	EventJSONHandleError = "event_json_handle_error"

	// EventOutput event is emitted when an output is emitted.
	EventOutput = "event_client_output"
)

Variables

View Source
var InvalidOutputErrorType = apierror.NewAPIError("INVALID_OUTPUT_ERROR_TYPE")

InvalidOutputErrorType represents a generic error.

Functions

This section is empty.

Types

type APIOutput

type APIOutput[T any] struct {
	Payload *T                        `json:"payload,omitempty"`
	Error   *apierror.DefaultAPIError `json:"error,omitempty"`
}

APIOutput represents the output of a client request.

type JSONOutput

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

JSONOutput represents the output of a client request.

func NewJSONOutput

func NewJSONOutput(
	emitterLogger event.EmitterLogger, errorOrigin string,
) JSONOutput

NewJSONOutput returns a new JSONOutput.

Parameters:

  • emitterLogger: The emitter logger.
  • errorOrigin: The origin of the error.

Returns:

  • JSONOutput: The new JSONOutput.

func (JSONOutput) Handle

func (o JSONOutput) Handle(
	w http.ResponseWriter, r *http.Request, out any, outError error, status int,
) error

Handle marshals the output to JSON and writes it to the response. If the logger is not nil, it will log the output. Only APIErrors are allowed to be marshaled to JSON.

Parameters:

  • w: The response writer.
  • r: The request.
  • out: The output data.
  • outError: The output error.
  • status: The HTTP status code.

Returns:

  • error: An error if the request fails.

type RequestOption

type RequestOption func(*util.RequestData)

RequestOption defines a function type that modifies a RequestData.

func WithSpanID

func WithSpanID() RequestOption

WithSpanID is an option that adds an X-Span-ID header if not already present.

Returns:

  • RequestOption: The request option.

func WithoutSpanID

func WithoutSpanID() RequestOption

WithoutSpanID is an option that ensures the X-Span-ID header is not set.

Returns:

  • RequestOption: The request option.

type Response

type Response[Output any] struct {
	Response *http.Response // The HTTP response object.
	Output   *Output        // The output data of the API response.
}

Response represents the response from a client request.

func SendParsedRequest

func SendParsedRequest[Output any](
	ctx context.Context,
	client *http.Client,
	fullURL string,
	method string,
	parsedInput *util.RequestData,
) (*Response[Output], error)

SendParsedRequest sends a request to the target host using the parsed input. It will setup the input headers, cookies, and body based on the parsed input. Body is only set for non-GET requests. The URL parameters are added to the URL forming the full URL with query parameters.

Parameters:

  • ctx: The context for the request.
  • client: The client to use for the request.
  • fullURL: The URL with query parameters to send the request to.
  • method: The HTTP method to use for the request.
  • parsedInput: The parsed input to use for the request.

Returns:

  • *Response[Output]: The response from the request.
  • error: An error if the request fails.

func SendRequest

func SendRequest[Input any, Output any](
	ctx context.Context,
	host string,
	url string,
	method string,
	i *Input,
	loggerFactoryFn logging.CtxLoggerFactoryFn,
	opts ...RequestOption,
) (*Response[Output], error)

SendRequest sends a request to the target host. It will setup the input headers, cookies, and body based on the input. By default, WithSpanID is applied, unless overridden.

Parameters:

  • ctx: The context for the request.
  • host: The host to send the request to.
  • url: The URL to send the request to.
  • method: The HTTP method to use for the request.
  • i: The input to use for the request.
  • loggerFactoryFn: An optional function that returns a logger.
  • opts: Optional request options. By default, WithSpanID is applied.

Returns:

  • *Response[Output]: The response from the request.
  • error: An error if the request fails.

Jump to

Keyboard shortcuts

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