handler

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDisabledDeliveryKind defines an error caused by a disabled delivery kind.
	// TODO: move to receiver handler, or use invalid resource
	ErrDisabledDeliveryKind = errors.New("disabled delivery kind")

	// ErrDisabledMessageKind defines an error caused by a disabled message kind.
	// TODO: move to receiver handler, or use invalid resource
	ErrDisabledMessageKind = errors.New("disabled message kind")

	// ErrInvalidAPIVersion defines an error caused by a request to an API endpoint with an invalid
	// version ID.
	ErrInvalidAPIVersion = errors.New("invalid API version")

	// ErrInvalidAuthToken defines an error caused by a malformed Authorization header, for example
	// a basic instead of a bearer token. This is not the same as authorizer.ErrInvalidJWT, which
	// handles the actual JWT and leads to an actual 403 FORBIDDEN.
	ErrInvalidAuthToken = errors.New("invalid auth token")

	// ErrInvalidMethodOrPath defines an error caused by a request that doesn't match anything, for
	// example a request to an invalid path, or a POST request to a specific resource ID.
	ErrInvalidMethodOrPath = errors.New("invalid method or path")

	// ErrInvalidQuery defines an error caused by an invalid or malformed query. This does not
	// include validation errors.
	ErrInvalidQuery = errors.New("invalid query")

	ErrInvalidRequestBody = errors.New("invalid request body")

	// ErrInvalidResource defines an error caused by an invalid resource in the provided path, for
	// example because of an invalid namespace id or code, or a resource that is in another
	// namespace.
	ErrInvalidResource = errors.New("invalid resource")

	// ErrInvalidResourceID defines an error caused by an invalid resource id in the provided path.
	// This only includes the main resource id, not a sub-resource (eg. a namespace id).
	ErrInvalidResourceID = errors.New("invalid resource id")

	// ErrInvalidResourceIDOrCode defines an error caused by a value that is supposed to uniquely
	// match a given resource, but which does not match either an ID, nor a code field.
	ErrInvalidResourceIDOrCode = errors.New("invalid resource id or code")

	// ErrInvalidResourceScope defines an error caused by a mismatch between the user scope
	// provided in the path, and the namespace related to the current resource.
	ErrInvalidResourceScope = errors.New("invalid resource scope")
)

Functions

func APIVersion

func APIVersion(ctx context.Context) string

func APIVersionMiddleware

func APIVersionMiddleware(h http.Handler) http.Handler

APIVersionMiddleware validates that the specified api version, if any, is equal to v1.

func ErrorStatus

func ErrorStatus(err error) int

ErrorStatus maps an error to its HTTP status code.

func JWTMiddleware

func JWTMiddleware(h http.Handler) http.Handler

JWTMiddleware parses and validates the request's JWT token.

JWT claims should contain something similar to the following:

{
  "roles": [
    { "namespaceId": "00000000-0000-0000-0001-000000000001", "code": "ns_writer" }
    { "namespaceId": "00000000-0000-0000-0001-000000000003", "code": "ns_reader" }
  ]
}

func ParseOData

func ParseOData(params map[string]string, rt resource.Type) (*query.Query, error)

func ParseODataFilters

func ParseODataFilters(value string, rt resource.Type) ([]*query.Filter, error)

ParseODataFilters parses the specified string value into an array of *Filter structs.

The $filter system query option restricts the set of items returned.

Example 46: return all Products whose Price is less than $10.00

GET http://host/service/Products?$filter=Price lt 10.00

As a more concrete example, when receiving the following string

"status eq 'delivered' and deliverTime le '2000-01-02T03:04:05'"

this function is expected to return the following filters, in the same order:

* { Operator: OpEqual, FieldName: "status", Operand: "delivered" } * { Operator: OpLessThan, FieldName: "deliverTime", Operand: "2000-01-02T03:04:05" }

Only the following operators are currently supported:

Comparison Operators

Operator|Description|Example ---|---|--- eq|Equal|Address/City eq 'Redmond' ne|Not equal|Address/City ne 'London' gt|Greater than|Price gt 20 ge|Greater than or equal|Price ge 10 lt|Less than|Price lt 20 le|Less than or equal|Price le 100 in|Is a member of|Address/City in ('Redmond', 'London')

Logical Operators

Operator|Description|Example ---|---|--- and|Logical and|Price le 200 and Price gt 3.5

As this algorithm is currently coded, the following queries are valid, even though they probably shouldn't be:

"and status eq 'pending'"
"status eq 'pending' and"

This function will return an error if the value could not be parsed, or if a resulting filter is invalid.

ref. https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_SystemQueryOptionfilter ref. https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#_Toc31360954 ref. https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#ABNF

func ParseODataOrderBy

func ParseODataOrderBy(value string, rt resource.Type) ([]*query.Order, error)

ParseODataOrderBy parses the specified string value into an array of *Order structs.

The $orderby System Query option specifies the order in which items are returned from the service.

The value of the $orderby System Query option contains a comma-separated list of expressions whose primitive result values are used to sort the items. A special case of such an expression is a property path terminating on a primitive property. A type cast using the qualified entity type name is required to order by a property defined on a derived type. Only aliases defined in the metadata document of the service can be used in URLs.

The expression can include the suffix asc for ascending or desc for descending, separated from the property name by one or more spaces. If asc or desc is not specified, the service MUST order by the specified property in ascending order. 4.01 services MUST support case-insensitive values for asc and desc. Clients that want to work with 4.0 services MUST use lower case values.

Null values come before non-null values when sorting in ascending order and after non-null values when sorting in descending order.

Items are sorted by the result values of the first expression, and then items with the same value for the first expression are sorted by the result value of the second expression, and so on.

The Boolean value false comes before the value true in ascending order.

Services SHOULD order language-dependent strings according to the content-language of the response, and SHOULD annotate string properties with language-dependent order with the term Core.IsLanguageDependent, see [OData-VocCore].

Values of type Edm.Stream or any of the Geo types cannot be sorted.

Example 50: return all Products ordered by release date in ascending order, then by rating in descending order

GET http://host/service/Products?$orderby=ReleaseDate asc, Rating desc

Related entities may be ordered by specifying $orderby within the $expand clause.

Example 51: return all Categories, and their Products ordered according to release date and in descending order of rating

GET http://host/service/Categories?$expand=Products($orderby=ReleaseDate asc, Rating desc)

$count may be used within a $orderby expression to order the returned items according to the exact count of related entities or items within a collection-valued property.

Example 52: return all Categories ordered by the number of Products within each category

GET http://host/service/Categories?$orderby=Products/$count

ref. https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_SystemQueryOptionorderby

func ParseODataSkip

func ParseODataSkip(value string) (int, error)

parseODataSkip parses the specified string value into an integer.

The $skip system query option specifies a non-negative integer n that excludes the first n items of the queried collection from the result. The service returns items starting at position n+1.

Example 54: return products starting with the 6th product of the Products entity set

GET http://host/service/Products?$skip=5

Where $top and $skip are used together, $skip MUST be applied before $top, regardless of the order in which they appear in the request.

Example 55: return the third through seventh products of the Products entity set

GET http://host/service/Products?$top=5&$skip=2

If no unique ordering is imposed through an $orderby query option, the service MUST impose a stable ordering across requests that include $skip.

ref. https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_SystemQueryOptionskip

func ParseODataTop

func ParseODataTop(value string) (int, error)

parseODataTop parses the specified string value into an integer.

The $top system query option specifies a non-negative integer n that limits the number of items returned from a collection. The service returns the number of available items up to but not greater than the specified value n.

Example 53: return only the first five products of the Products entity set

GET http://host/service/Products?$top=5

If no unique ordering is imposed through an $orderby query option, the service MUST impose a stable ordering across requests that include $top.

ref. https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_SystemQueryOptiontop

func ParseRequestBody

func ParseRequestBody(body io.Reader, out interface{}) error

func RequestID

func RequestID(ctx context.Context) string

func RequestIDMiddleware

func RequestIDMiddleware(h http.Handler) http.Handler

RequestIDMiddleware retrieves the request ID from the HTTP headers, or generates a new one. The resulting request ID is placed into the request's context.

func RequestParams

func RequestParams(r *http.Request) map[string]string

func SetAPIVersion

func SetAPIVersion(ctx context.Context, apiVersion string) context.Context

func SetRequestID

func SetRequestID(ctx context.Context, requestID string) context.Context

func WriteErrorResponse

func WriteErrorResponse(w http.ResponseWriter, reqErr error, meta map[string]interface{})

WriteErrorResponse writes the body of a failed request's response.

func WriteResponse

func WriteResponse(
	w http.ResponseWriter,
	status int,
	data interface{},
	meta map[string]interface{},
)

WriteResponse builds the successful response body and writes it to the provided w writer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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