rest

package
v0.0.0-...-c5655c4 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2017 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package rest provides the building blocks for implementing the V1 HTTP REST API handlers

Index

Constants

View Source
const HeaderRequestID = "VH-RequestID"

HeaderRequestID is the header containing the request ID

Variables

This section is empty.

Functions

This section is empty.

Types

type Response

type Response struct {
	// The unique identifier, assigned by the server for the request
	RequestID uuid.UUID `json:"request_id"`
	// Method is the HTTP verb used in the request
	Method string `json:"method"`
	// RequestPath is the URI of the requested resource(s)
	RequestPath string `json:"request_path"`
	// Params should contains the request parameters as well as any parameters
	// that the particular API call fulfilled with default values
	Params map[string]interface{} `json:"params,omitempty"`
	// Success indicates the overall success of the request; if false, there are
	// one or more issues that occurred during the request.
	Success bool `json:"success"`
	// ProcessingTime indicates the number of seconds required to process and
	// fulfill the request
	ProcessingTime float64 `json:"processing_time"`
	// Result the result of a successful request
	Result json.RawMessage `json:"result,omitempty"`
	// ErrorMessage is a message returned for unsuccessful requests
	ErrorMessage string `json:"error_message,omitempty"`
	// contains filtered or unexported fields
}

Response is the container for JSON data returned by the V1 API

func NewResponse

func NewResponse(r *http.Request) *Response

NewResponse initializes a Response object, setting a UUID for the request, tracking the start time for the request and filling the request path and method. The Params field is not filled out automatically as the caller may choose to ignore some parameters provided.

func (*Response) GetResult

func (r *Response) GetResult(value interface{}) error

GetResult retrieves the raw JSON result from the Result field and decodes the value into the given value pointer

func (*Response) SetError

func (r *Response) SetError(message string)

SetError sets an error message an sets success to false

func (*Response) SetGenericServerError

func (r *Response) SetGenericServerError()

SetGenericServerError sets a generic error message indicating that the server encountered an error that prevented a successful response. The Success and ErrorMessage fields are set.

func (*Response) SetNotFoundError

func (r *Response) SetNotFoundError()

SetNotFoundError sets an error message indicating that one or more of the records refered to were not found in the system. The Success and ErrorMessage fields are set.

func (*Response) SetParams

func (r *Response) SetParams(in interface{}) error

SetParams translates the Query structure into a params for http response. Note: Currently, we exploit JSON package to achieve this. Yes, we may incur some cost in a Marshal and Unmarshal step, but this avoids ugly type assertion jumble. In another word, the code is cleaner in this manner.

func (*Response) SetResult

func (r *Response) SetResult(value interface{}) error

SetResult adds a value to the Result field.

func (*Response) SetUnauthenticatedError

func (r *Response) SetUnauthenticatedError()

SetUnauthenticatedError sets an error message indicating that the current authenticated user is not authorized for the current request. The Success and ErrorMessage fields are set.

func (*Response) SetUnauthorizedError

func (r *Response) SetUnauthorizedError()

SetUnauthorizedError sets an error message indicating that the current authenticated user is not authorized for the current request. The Success and ErrorMessage fields are set.

func (*Response) Write

func (r *Response) Write(status int, w http.ResponseWriter) error

Write writes the JSON form of the Response including the sending of the HTTP status code.

type ResultMap

type ResultMap map[string]interface{}

ResultMap is a type alias for setting a result simply

Jump to

Keyboard shortcuts

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