jsonapi

package
v0.0.0-...-e1b65c3 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

Package cloudeng.io/webapp/jsonapi

import cloudeng.io/webapp/jsonapi

Package jsonapi provides utilities for working with json REST APIs.

Functions

Func WriteError
func WriteError(rw http.ResponseWriter, err ErrorResponse, status int)

WriteError writes an ErrorResponse in JSON format to the http.ResponseWriter. It sets the appropriate HTTP status code and content type.

Func WriteErrorMsg
func WriteErrorMsg(rw http.ResponseWriter, msg string, status int)

WriteErrorMsg writes an error message in JSON format to the http.ResponseWriter using WriteErrror.

Types

Type Endpoint
type Endpoint[Req, Resp any] struct{}

Endpoint represents a JSON API endpoint with a request and response type. It provides methods to parse the request from an io.Reader and write the response to an io.Writer. If an error occurs during parsing or writing, it can write an error response in JSON format using the WriteError method. It is primarily intended to identify and document JSON API endpoints.

Methods
func (ep Endpoint[Req, Resp]) ParseRequest(rw http.ResponseWriter, r *http.Request, req *Req) error

ParseRequest reads the request body from the provided http.Request and decodes it into the Request field of the Endpoint. If decoding failes, it uses WriteError to write an error message to the client.

func (ep Endpoint[Req, Resp]) WriteResponse(rw http.ResponseWriter, resp Resp) error

WriteResponse writes the response in JSON format to the http.ResponseWriter. It sets the Content-Type header to "application/json" and writes the HTTP status code. If encoding the response fails, it uses WriteError to write an error message to the client.

Type ErrorResponse
type ErrorResponse struct {
	Message string `json:"message"`
}

ErrorResponse represents a JSON error response.

Documentation

Overview

Package jsonapi provides utilities for working with json REST APIs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteError

func WriteError(rw http.ResponseWriter, err ErrorResponse, status int)

WriteError writes an ErrorResponse in JSON format to the http.ResponseWriter. It sets the appropriate HTTP status code and content type.

func WriteErrorMsg

func WriteErrorMsg(rw http.ResponseWriter, msg string, status int)

WriteErrorMsg writes an error message in JSON format to the http.ResponseWriter using WriteErrror.

Types

type Endpoint

type Endpoint[Req, Resp any] struct{}

Endpoint represents a JSON API endpoint with a request and response type. It provides methods to parse the request from an io.Reader and write the response to an io.Writer. If an error occurs during parsing or writing, it can write an error response in JSON format using the WriteError method. It is primarily intended to identify and document JSON API endpoints.

func (Endpoint[Req, Resp]) ParseRequest

func (ep Endpoint[Req, Resp]) ParseRequest(rw http.ResponseWriter, r *http.Request, req *Req) error

ParseRequest reads the request body from the provided http.Request and decodes it into the Request field of the Endpoint. If decoding failes, it uses WriteError to write an error message to the client.

func (Endpoint[Req, Resp]) WriteResponse

func (ep Endpoint[Req, Resp]) WriteResponse(rw http.ResponseWriter, resp Resp) error

WriteResponse writes the response in JSON format to the http.ResponseWriter. It sets the Content-Type header to "application/json" and writes the HTTP status code. If encoding the response fails, it uses WriteError to write an error message to the client.

type ErrorResponse

type ErrorResponse struct {
	Message string `json:"message"`
}

ErrorResponse represents a JSON error response.

Jump to

Keyboard shortcuts

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