Documentation ¶
Overview ¶
Package rest provides the building blocks for implementing the V1 HTTP REST API handlers
Index ¶
- Constants
- type Response
- func (r *Response) GetResult(value interface{}) error
- func (r *Response) SetError(message string)
- func (r *Response) SetGenericServerError()
- func (r *Response) SetNotFoundError()
- func (r *Response) SetParams(in interface{}) error
- func (r *Response) SetResult(value interface{}) error
- func (r *Response) SetUnauthenticatedError()
- func (r *Response) SetUnauthorizedError()
- func (r *Response) Write(status int, w http.ResponseWriter) error
- type ResultMap
Constants ¶
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 ¶
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 ¶
GetResult retrieves the raw JSON result from the Result field and decodes the value into the given value pointer
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 ¶
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) 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.