io

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilRequest            = fmt.Errorf("nil request")
	ErrNilRequestBody        = fmt.Errorf("nil request body")
	ErrNotInitializedRequest = fmt.Errorf("request not initialized")
	ErrEmptyRequestBody      = fmt.Errorf("empty request body")
	ErrReadBody              = fmt.Errorf("failed to read request body")
	ErrEncodeBody            = fmt.Errorf("failed to encode request body")
	ErrDecodeBody            = fmt.Errorf("failed to decode request body")
	ErrHTTPStatusNotOk       = fmt.Errorf("no HTTP status OK")
)

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code       int    `json:"code"`
	Message    string `json:"message"`
	Err        string `json:"error,omitempty"`
	StatusCode int    `json:"-"`
}

APIError represents an error response from the API. It includes a code, message, and optional error string. The StatusCode field is used to set the HTTP status code for the response. It has some methods to manipulate the error message and write the error response to an http.ResponseWriter.

func NewAPIError

func NewAPIError(code, status int) *APIError

NewAPIError creates a new APIError instance with the provided code and status code. It initializes the error string and message to empty strings. This function is useful for creating a new APIError instance with specific error codes and status codes. It returns a pointer to the newly created APIError instance.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface for APIError. It returns a string representation of the error, including the code, message, error string, and status code. It can be used for logging or debugging purposes.

func (*APIError) With

func (e *APIError) With(msg string) *APIError

With appends a string message to the existing message in the APIError. If the existing message is empty, it sets it to the new message. This method is useful for chaining messages together for better debugging and logging. It returns the updated APIError instance and also updates the current instance.

func (*APIError) WithErr

func (e *APIError) WithErr(err error) *APIError

WithErr appends an error message to the existing error string in the APIError. If the existing error string is empty, it sets it to the new error message. This method is useful for chaining error messages together for better debugging and logging. It returns the updated APIError instance and also updates the current instance.

func (*APIError) Write

func (e *APIError) Write(w http.ResponseWriter)

WriteJSON writes the APIError as a JSON response to the provided http.ResponseWriter. It sets the Content-Type header to "application/json" and writes the status code and serialized error bytes to the response. If an error occurs during writing, it writes an internal server error response instead.

type Request

type Request[T any] struct {
	Data T
	// contains filtered or unexported fields
}

Request represents a request with a generic data type to be unmarshalled from the request body. It implements the Read method to read and unmarshal the request body into the Data field.

func RequestWith

func RequestWith[T any](data *T) *Request[T]

RequestWith creates a new Request[T] with the provided data. If the data is nil, it returns an empty Request[T], if not, the data is wrapped and the resulting request is marked as initialized. This method is useful for creating requests with different data types without needing to define separate request structs for each type.

func (*Request[T]) Read

func (req *Request[T]) Read(r *http.Request) error

Read reads the request body and unmarshals it into the Data field of the Request struct. It returns an error if the request body is nil or empty, or if there is an error during unmarshalling. If the Request struct is nil, it initializes a new instance of Request[T]. This method is useful for handling incoming requests in a generic way, allowing for different data types to be processed without needing to define separate request structs for each type.

func (*Request[T]) WriteJSON

func (req *Request[T]) WriteJSON(r *http.Request) error

WriteJSON writes the request data to the provided http.Request in JSON format. It sets the Content-Type header to "application/json" and replaces the request body with the JSON marshalled data. If the request is not initialized, it returns an error. This method is useful for sending requests with structured JSON data. It can be used in HTTP handlers or middleware to send requests with specific data types.

type Response

type Response[T any] struct {
	Data T
	// contains filtered or unexported fields
}

Response represents a response with a generic data type. It can be used to send JSON responses or plain text responses. The Data field holds the response data, and the empty field indicates whether the response is empty or not. It has methods to write the response to an http.ResponseWriter in JSON format or plain text format.

func OkResponse

func OkResponse(body ...byte) *Response[any]

OkResponse creates a new Response instance with the provided byte slice. If the byte slice is empty, it returns an empty response. This method is useful for creating responses with raw byte data. It returns a pointer to the Response instance. The empty field indicates whether the response is empty or not. If the byte slice is empty, the response is considered empty. If the byte slice is not empty, the response is considered non-empty.

func ResponseWith

func ResponseWith[T any](data *T) *Response[T]

ResponseWith creates a new Response instance with the provided data. If the data is nil, it returns an empty response. This method is useful for creating responses with different data types without needing to define separate response structs for each type. It returns a pointer to the Response instance.

func (*Response[T]) Read

func (r *Response[T]) Read(res *http.Response) (T, error)

Read reads the response data from the provided http.Response. It expects the response body to be in JSON format and unmarshals it into the provided generic type T. If the response status code is not 200 OK, it returns an error indicating the unexpected status code. If there is an error reading the response body or unmarshaling the JSON data, it returns an error. This method is useful for handling HTTP responses in a generic way, allowing you to read and process the response data without needing to know the specific type of the response data in advance.

func (*Response[T]) Write

func (r *Response[T]) Write(w http.ResponseWriter)

Write writes the response data to the provided http.ResponseWriter in plain text format. It sets the Content-Type header to "text/plain" and writes the response data as plain text. If the response is empty, it writes a plain text "OK" response with a 200 OK status code. If there is an error during response writing, it writes an error response with a 500 Internal Server Error status code. This method is useful for sending plain text responses to the client. It can be used in HTTP handlers or middleware to send simple text responses. It is a more generic method than WriteJSON, as it does not require the response data to be JSON-serializable.

func (*Response[T]) WriteJSON

func (r *Response[T]) WriteJSON(w http.ResponseWriter)

WriteJSON writes the response data to the provided http.ResponseWriter in JSON format. It sets the Content-Type header to "application/json" and writes the response data as JSON. If the response is empty, it writes a plain text "OK" response with a 200 OK status code. If there is an error during JSON encoding or response writing, it writes an error response with a 500 Internal Server Error status code. This method is useful for sending JSON responses to the client. It can be used in HTTP handlers or middleware to send structured JSON responses.

Jump to

Keyboard shortcuts

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