jsonresp

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2019 License: BSD-3-Clause Imports: 4 Imported by: 6

README

JSON Response

GoDoc Build Status Go Report Card

The json-resp package contains a small set of functions that are used to marshall and unmarshall response data and errors in JSON format.

Quick Start

Install the CircleCI Local CLI. See the Continuous Integration section below for more detail.

To build and test:

circleci build

Continuous Integration

This package uses CircleCI for Continuous Integration (CI). It runs automatically on commits and pull requests involving a protected branch. All CI checks must pass before a merge to a proected branch can be performed.

The CI checks are typically run in the cloud without user intervention. If desired, the CI checks can also be run locally using the CircleCI Local CLI.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadError added in v0.5.0

func ReadError(r io.Reader) error

ReadError attempts to unmarshal JSON-encoded error details from the supplied reader. It returns nil if an error could not be parsed from the response, or if the parsed error was nil.

func ReadResponse

func ReadResponse(r io.Reader, v interface{}) error

ReadResponse reads a JSON response, and unmarshals the supplied data.

func WriteError

func WriteError(w http.ResponseWriter, error string, code int) error

WriteError encodes the supplied error in a response, and writes to w.

func WriteResponse

func WriteResponse(w http.ResponseWriter, data interface{}, code int) error

WriteResponse encodes the supplied data in a response, and writes to w.

func WriteResponsePage added in v0.2.0

func WriteResponsePage(w http.ResponseWriter, data interface{}, pd *PageDetails, code int) error

WriteResponsePage encodes the supplied data in a paged JSON response, and writes to w.

Types

type Error

type Error struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

Error describes an error condition.

func NewError

func NewError(code int, message string) *Error

NewError returns an error that contains the given code and message.

func (*Error) Error

func (e *Error) Error() string

type PageDetails added in v0.2.0

type PageDetails struct {
	Prev      string `json:"prev,omitempty"`
	Next      string `json:"next,omitempty"`
	TotalSize int    `json:"totalSize,omitempty"`
}

PageDetails specifies paging information.

func ReadResponsePage added in v0.2.0

func ReadResponsePage(r io.Reader, v interface{}) (pd *PageDetails, err error)

ReadResponsePage reads a paged JSON response, and unmarshals the supplied data.

type Response

type Response struct {
	Data  interface{}  `json:"data,omitempty"`
	Page  *PageDetails `json:"page,omitempty"`
	Error *Error       `json:"error,omitempty"`
}

Response is the top level container of all of our REST API responses.

Jump to

Keyboard shortcuts

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