response

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Service = dependency.Service{
	Dependencies: fx.Provide(
		func() ResponderConstructor {
			return NewJSONResponder
		},
		NewFactory,
	),
	Constructor: func(factory ResponderFactory) ResponderProvider {
		return factory
	},
}

Service is the definition of the dependency

Functions

This section is empty.

Types

type JSONEncoder

type JSONEncoder interface {
	Encode(v interface{}) error
}

JSONEncoder is an interface that abstracts the encoding of JSON

type JSONResponder

type JSONResponder struct {
	Encoder JSONEncoder
	// contains filtered or unexported fields
}

JSONResponder is a responder that will respond with JSON responses

func (JSONResponder) Respond

func (r JSONResponder) Respond(statusCode int, value interface{})

Respond will take a given struct and respond with it as the body

func (JSONResponder) RespondStream

func (r JSONResponder) RespondStream(statusCode int, valueStream <-chan interface{})

RespondStream will stream a response of JSON values to the client

func (JSONResponder) RespondWithProblem

func (r JSONResponder) RespondWithProblem(statusCode int, detail string)

RespondWithProblem will respond with the given status code and detail, with an API problem

type Problem

type Problem struct {
	Status int    `json:"status"`
	Type   string `json:"type"`
	Title  string `json:"title"`
	Detail string `json:"detail"`
}

Problem is a struct that provides standard error details

func NewHTTPProblem

func NewHTTPProblem(statusCode int, detail string) Problem

NewHTTPProblem creates a new instance of a Problem for HTTP errors

type Responder

type Responder interface {
	RespondWithProblem(statusCode int, detail string)
	Respond(statusCode int, value interface{})
	RespondStream(statusCode int, valueStream <-chan interface{})
}

Responder is an interface that abstracts the production of the response away from handlers

func NewJSONResponder

func NewJSONResponder(logger *zap.Logger, rw http.ResponseWriter, r *http.Request) Responder

NewJSONResponder creates a new instance of the JSONResponder type for the given request

type ResponderConstructor

type ResponderConstructor func(logger *zap.Logger, rw http.ResponseWriter, r *http.Request) Responder

ResponderConstructor is a function that can create a new instance of a responder

type ResponderFactory

type ResponderFactory struct {
	Logger           *zap.Logger
	DefaultResponder ResponderConstructor
}

ResponderFactory is a factory that can create new Responders, it allows for a responder to be created in a handler and subsequently called.

func NewFactory

func NewFactory(logger *zap.Logger, defaultResponder ResponderConstructor) ResponderFactory

NewFactory creates a new instance of the ResponderFactory

func (ResponderFactory) Responder

Responder creates a new instance of a responder

type ResponderProvider

type ResponderProvider interface {
	Responder(rw http.ResponseWriter, r *http.Request) Responder
}

ResponderProvider is an interface that abstracts the providing of Responders

Jump to

Keyboard shortcuts

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