response

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MetaBadRequest = Meta{Status: "error", StatusCode: 400, Message: "Bad Request"}

MetaBadRequest is used for unknown errors.

View Source
var MetaNotFound = Meta{Status: "error", StatusCode: 998, Message: "Not Found"}

MetaNotFound is returned when trying to access not existing resources.

View Source
var MetaOK = Meta{Status: "ok", StatusCode: 100, Message: "OK"}

MetaOK is the default ok response.

View Source
var MetaServerError = Meta{Status: "error", StatusCode: 996, Message: "Server Error"}

MetaServerError is returned on server errors.

View Source
var MetaUnauthorized = Meta{Status: "error", StatusCode: 997, Message: "Unauthorised"}

MetaUnauthorized is returned on unauthorized requests.

View Source
var MetaUnknownError = Meta{Status: "error", StatusCode: 999, Message: "Unknown Error"}

MetaUnknownError is used for unknown errors.

Functions

func APIVersion

func APIVersion(ctx context.Context) string

APIVersion retrieves the api version from the context.

func OcsV1StatusCodes

func OcsV1StatusCodes(meta Meta) int

OcsV1StatusCodes returns the http status codes for the OCS API v1.

func OcsV2StatusCodes

func OcsV2StatusCodes(meta Meta) int

OcsV2StatusCodes maps the OCS codes to http status codes for the ocs API v2.

func VersionCtx

func VersionCtx(next http.Handler) http.Handler

WithAPIVersion puts the api version in the context.

func WriteOCSData

func WriteOCSData(w http.ResponseWriter, r *http.Request, m Meta, d interface{}, err error)

WriteOCSData handles writing ocs data in json and xml.

func WriteOCSError

func WriteOCSError(w http.ResponseWriter, r *http.Request, c int, m string, err error)

WriteOCSError handles writing error ocs responses.

func WriteOCSResponse

func WriteOCSResponse(w http.ResponseWriter, r *http.Request, res Response, err error)

WriteOCSResponse handles writing ocs responses in json and xml.

func WriteOCSSuccess

func WriteOCSSuccess(w http.ResponseWriter, r *http.Request, d interface{})

WriteOCSSuccess handles writing successful ocs response data.

Types

type Meta

type Meta struct {
	Status       string `json:"status" xml:"status"`
	StatusCode   int    `json:"statuscode" xml:"statuscode"`
	Message      string `json:"message" xml:"message"`
	TotalItems   string `json:"totalitems,omitempty" xml:"totalitems,omitempty"`
	ItemsPerPage string `json:"itemsperpage,omitempty" xml:"itemsperpage,omitempty"`
}

Meta holds response metadata.

type Payload

type Payload struct {
	XMLName struct{}    `json:"-" xml:"ocs"`
	Meta    Meta        `json:"meta" xml:"meta"`
	Data    interface{} `json:"data,omitempty" xml:"data,omitempty"`
}

Payload combines response metadata and data.

func (Payload) MarshalXML

func (p Payload) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error)

MarshalXML handles ocs specific wrapping of array members in 'element' tags for the data.

type Response

type Response struct {
	OCS *Payload `json:"ocs"`
}

Response is the top level response structure.

Jump to

Keyboard shortcuts

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