handlerutil

package
v2.2.6 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

This package implements helper functions to parse service requests and render service responses.

Although service is the only entry point into the functions of this module and the implementation of HTTP handlers are left completely to the developer, this package still provides utilities that assumes usage of Go's native HTTP stack, which proves to be the common scenario. This will make HTTP handler implementation even easier.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateRequest

func CreateRequest(request *http.Request) (cr *service.CreateRequest, closer func())

CreateRequest returns a parsed *service.CreateRequest directly from *http.Request, and a closer function which should be called after resource processing is done (preferably using defer).

func DeleteRequest

func DeleteRequest(request *http.Request) func(resourceId string) *service.DeleteRequest

DeleteRequest returns a function that will supply a complete built *service.DeleteRequest when given resourceId.

func GetRequestProjection

func GetRequestProjection(request *http.Request) (projection *crud.Projection, err error)

GetRequestProjection returns a nullable *crud.Projection structure that may encapsulate the attributes or excludedAttributes parameters present in the HTTP GET request.

func MatchCriteria

func MatchCriteria(request *http.Request) func(resource *prop.Resource) bool

MatchCriteria returns a function to be supplied as the match criteria argument in replace, patch and delete requests. It checks for If-Match and If-None-Match headers and supports asterisk (*) and comma delimited resource versions. The If-Match header takes precedence over If-None-Match header. If none of the headers are present, it returns a function that always returns true.

func PatchRequest

func PatchRequest(request *http.Request) (pr func(resourceId string) *service.PatchRequest, closer func())

PatchRequest returns a function that will supply a complete built *service.PatchRequest when given resourceId, and a closer function which should be called after resource processing is done (preferably using defer).

func QueryRequestFromGet

func QueryRequestFromGet(request *http.Request) (qr *service.QueryRequest, err error)

QueryRequestFromGet returns a parsed *service.QueryRequest from *http.Request using HTTP GET method, and any error during parsing.

func QueryRequestFromPost

func QueryRequestFromPost(request *http.Request) (qr *service.QueryRequest, closer func(), err error)

QueryRequestFromPost returns a parsed *service.QueryRequest from *http.Request using HTTP POST method, a closer function to be invoked when the search is finished, and any error during the parsing.

func ReplaceRequest

func ReplaceRequest(request *http.Request) (rr func(resourceId string) *service.ReplaceRequest, closer func())

ReplaceRequest returns a function that will supply a complete built *service.ReplaceRequest when given resourceId, and a closer function which should be called after resource processing is done (preferably using defer).

func WriteError

func WriteError(rw http.ResponseWriter, err error) error

WriteError writes the error to the http.ResponseWriter. Any error during the process will be returned. If the cause of the error (determined using errors.Unwrap) is a *spec.Error, the cause status and scimType will be used together with the error's message as detail. If the cause is not a *spec.Error, spec.ErrInternal is used instead. This method also writes the http status with the error's defined status, and set Content-Type header to application/scim+json.

func WriteResourceToResponse

func WriteResourceToResponse(rw http.ResponseWriter, resource *prop.Resource, options ...scimjson.Options) error

WriteResourceToResponse writes the given resource to http.ResponseWriter, respecting the attributes or excludedAttributes specified through options. Any error during the process will be returned. Apart from writing the JSON representation of the resource to body, this method also sets Content-Type header to application/scim+json; sets Location header to resource's meta.location field, if any; and sets ETag header to resource's meta.version field, if any. This method does not set response status, which should be set before calling this method.

func WriteSearchResultToResponse

func WriteSearchResultToResponse(rw http.ResponseWriter, searchResult *service.QueryResponse, options ...scimjson.Options) error

WriteSearchResultToResponse writes the search result to http.ResponseWrite, respecting the attribute or excludedAttributes specified through options. Any error during the process will be returned. This method also sets Content-Type header to application/scim+json. This method does not set response status, which should be set before calling this method.

Types

type SearchResultRendering

type SearchResultRendering struct {
	Schemas      []string          `json:"schemas"`
	TotalResults int               `json:"totalResults"`
	StartIndex   int               `json:"startIndex"`
	ItemsPerPage int               `json:"itemsPerPage"`
	Resources    []json.RawMessage `json:"Resources,omitempty"`
}

SearchResultRendering is the JSON rendering structure for search results. This is very similar to service.QueryResponse except that resources are pre-rendered to adapt for objects serialized using scim json mechanism or go's json mechanism.

Jump to

Keyboard shortcuts

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