rest

package
v0.0.0-...-339e9c5 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2015 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeJson = "application/json"
	ContentTypeText = "text/plain"
)

Variables

This section is empty.

Functions

func Bytes

func Bytes(resp *EndpointResponse, err error) ([]byte, error)

Bytes, is a helper method to reduce the number of lines to get a byte array out of the EndpointResponse

func Encode

func Encode(v url.Values) string

Encode - Returns a URL that is encoded in a way that is actually used in practice, preseves as many special characters as possible. The default encoding from GO makes no sense as it encodes even valid values such as + , :

func FullApiWithHandlers

func FullApiWithHandlers(apiSpec interface{}) map[ServerResource]Handler

func GetHeaderValue

func GetHeaderValue(key string, headers map[string][]string) string

func LocalRequest

func LocalRequest(cr *ClientRequest) (*Request, *Response)

currently used primarily for testing, however, could be used to make request to the local process for cases where it made more sense to delpoy a typically remote resource locally

Types

type Client

type Client struct {
	Headers   map[string][]string
	Endpoints []*ResourceEndpoint
	Encoder   func(v interface{}) ([]byte, error)
	Decoder   func(data []byte, v interface{}) error
	Tracer    retrace.RequestTracer
}

Client executes REST calls

func NewClient

func NewClient() *Client

func (*Client) Fetch

func (c *Client) Fetch(r *ClientRequest, ctx *RequestContext, v interface{}) (*EndpointResponse, error)

Fetch makes it easier to Unmarshal the response from Do

func (*Client) NewHttpRequest

func (c *Client) NewHttpRequest(cr *ClientRequest) (*http.Request, error)

newHttpRequest converts a ClientRequest into a http.Request

func (*Client) Send

func (*Client) SetHeader

func (t *Client) SetHeader(h interface{}) error

SetHeader should receive a flat struct with no nested types. It will also overwrite existing headers.

type ClientRequest

type ClientRequest struct {
	// Resource is the path to the resource not including the scheme, domain or port
	Resource string
	// One of the six Verbs aka Methods defined for RESTful calls
	Verb string
	// Definition is useful for metrics, monitoring & tracing
	Definition ServerResource
	// Body applies to POST, PUT & PATCH
	Body interface{}
	// Headers for this request.  If a header name already exists in the client,
	// these headers will override existing on the client;
	Headers map[string][]string
	// contains filtered or unexported fields
}

ClientRequest is ideal created from a ResourceSpec, although, there's nothing to prevent you from using it directly.

func (*ClientRequest) AddHeader

func (cr *ClientRequest) AddHeader(key, value string) *ClientRequest

func (*ClientRequest) SetHeaders

func (cr *ClientRequest) SetHeaders(headers map[string][]string) *ClientRequest

SetHeaders is a Fluent Method that Sets multiple headers on the existing ClientRequest headers, overwriting any header with the same name

type ClientResource

type ClientResource interface {
	Get(args interface{}) *ClientRequest
	Post(args interface{}, body interface{}) *ClientRequest
	Put(args interface{}, body interface{}) *ClientRequest
	Patch(args interface{}, body interface{}) *ClientRequest
	Delete(args interface{}) *ClientRequest
	Head(args interface{}) *ClientRequest
}

type DeleteHandler

type DeleteHandler interface {
	Delete(*Request, Responder)
}

type EndpointResponse

type EndpointResponse struct {
	HttpResponse *http.Response
}

func (*EndpointResponse) Bytes

func (er *EndpointResponse) Bytes() ([]byte, error)

type FrozenCommunication

type FrozenCommunication struct {
	Request  *FrozenRequest  `json:"request"`
	Response *FrozenResponse `json:"response"`
}

type FrozenRequest

type FrozenRequest struct {
	Headers  map[string][]string `json:"headers"`
	Verb     string              `json:"verb"`
	Resource string              `json:"resource"`
	Body     json.RawMessage     `json:"body"`

	ResourceT string        `json:"resourceT,omitempty"`
	Requestor *FrozenSender `json:"requestor,omitempty"`
}

type FrozenResponse

type FrozenResponse struct {
	Headers    map[string][]string `json:"headers"`
	StatusCode string              `json:"statusCode"`
	Status     string              `json:"status"`
	Body       json.RawMessage     `json:"body"`
}

type FrozenSender

type FrozenSender struct {
	Device   string `json:"device,omitempty"`
	User     string `json:"user,omitempty"`
	Account  string `json:"account,omitempty"`
	Location string `json:"location,omitempty"`
}

type FullResource

type FullResource interface {
	ClientResource
	ServerResource
}

type GetHandler

type GetHandler interface {
	Get(*Request, Responder)
}

type Handler

type Handler interface {
}

RestHandler is a marker interface

type HandlerFunc

type HandlerFunc func(*Request, Responder)

type HeadHandler

type HeadHandler interface {
	Head(*Request, Responder)
}

type IdIntArg

type IdIntArg struct {
	Id int `json:"id,string" url:"id"`
}

type PatchHandler

type PatchHandler interface {
	Patch(*Request, Responder)
}

type PostHandler

type PostHandler interface {
	Post(*Request, Responder)
}

type PutHandler

type PutHandler interface {
	Put(*Request, Responder)
}

type Request

type Request struct {
	Raw        *http.Request
	Context    *RequestContext
	Definition ServerResource
	Args       interface{} //map?
	Body       interface{}
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(raw *http.Request, ctx *RequestContext, spec ServerResource) *Request

func (*Request) Annotate

func (r *Request) Annotate(f retrace.From, k string, v interface{})

func (*Request) Bytes

func (r *Request) Bytes() ([]byte, error)

Bytes returns the body of the request as a []byte

func (*Request) ContentType

func (r *Request) ContentType() string

func (*Request) DecodeArgs

func (r *Request) DecodeArgs(argValues map[string]string) error

type RequestContext

type RequestContext struct {
	Trace retrace.Tracer
	// contains filtered or unexported fields
}

func NewRequestContext

func NewRequestContext() *RequestContext

func (*RequestContext) Add

func (r *RequestContext) Add(ns string, key string, value interface{})

func (*RequestContext) Get

func (r *RequestContext) Get(ns string, key string) (value interface{}, found bool)

func (*RequestContext) Remove

func (r *RequestContext) Remove(ns string, key string)

type ResourceDef

type ResourceDef struct {
	ResourceT    string // /sync/order
	ResourceArgs reflect.Type
	Verb         string   // GET POST
	Headers      []string //TODO: change to reflect.Type
	RequestBody  reflect.Type
	ResponseBody reflect.Type
	// where else would be put content type, if not here?
	RequestContentTypes  []string
	ResponseContentTypes []string
	// contains filtered or unexported fields
}

ResourceDef in a specification of the Resource. Maybe rename to ResourceSpec

func (*ResourceDef) GetPath

func (rd *ResourceDef) GetPath(args interface{}) string

type ResourceEndpoint

type ResourceEndpoint struct {
	Host         string `json:"host"`         // Can be host or host:port
	Scheme       string `json:"scheme"`       //http, https, websocket,
	ResourceRoot string `json:"resourceRoot"` // Root could be /api or /v1
}

type ResourceSpec

type ResourceSpec struct {
	// contains filtered or unexported fields
}

func NewResourceSpec

func NewResourceSpec(defaultContentType string) *ResourceSpec

func (*ResourceSpec) Delete

func (rs *ResourceSpec) Delete(args interface{}) *ClientRequest

func (*ResourceSpec) Get

func (rs *ResourceSpec) Get(args interface{}) *ClientRequest

func (*ResourceSpec) Head

func (rs *ResourceSpec) Head(args interface{}) *ClientRequest

func (*ResourceSpec) Patch

func (rs *ResourceSpec) Patch(args interface{}, body interface{}) *ClientRequest

func (*ResourceSpec) Post

func (rs *ResourceSpec) Post(args interface{}, body interface{}) *ClientRequest

func (*ResourceSpec) Put

func (rs *ResourceSpec) Put(args interface{}, body interface{}) *ClientRequest

func (*ResourceSpec) ServeAll

func (rs *ResourceSpec) ServeAll() ([]ServerResource, Handler)

func (*ResourceSpec) Use

func (r *ResourceSpec) Use(def *ResourceDef) *ResourceSpec

func (*ResourceSpec) WithHandler

func (r *ResourceSpec) WithHandler(handler Handler) *ResourceSpec

type Responder

type Responder interface {
	AddHeader(key, value string)
	SetBody(interface{})
	SetContentType(ct string)
	SetStatus(statusCode int, statusMessage string, err error)
}

type Response

type Response struct {
	Body        interface{}
	Status      int
	Message     string
	Headers     map[string]string
	ContentType string
	Error       error
}

func NewResponse

func NewResponse() *Response

func (*Response) AddHeader

func (r *Response) AddHeader(key, value string)

func (*Response) IsBinary

func (r *Response) IsBinary() bool

func (*Response) SetBody

func (r *Response) SetBody(data interface{})

func (*Response) SetContentType

func (r *Response) SetContentType(contentType string)

func (*Response) SetStatus

func (r *Response) SetStatus(status int, message string, err error)

type Sender

type Sender interface {
	Send(r *ClientRequest, ctx *RequestContext) (*EndpointResponse, error)
}

type ServerResource

type ServerResource interface {
	//the Resource template
	ResourceT() string
	ResourceArgs() interface{}
	// Methods supported
	Verb() string
	// Headers Required
	Headers() []string
	// Request returns a new instance of the request
	RequestBody() interface{}
	// Response return a new instance of the response
	ResponseBody() interface{}
	// RequestContentTypes are a list of accepted content-type for the request portion
	RequestContentTypes() []string
	// ResponseContentTypes are a list content-type that the response will be sent in
	ResponseContentTypes() []string //not sure this should be an array?
}

ServerResource is a Resource that the Server offers and is a readonly view into a ResourceDef

func NewServerResource

func NewServerResource(definition *ResourceDef, reqContentTypes []string, respContentTypes []string) ServerResource

type UrlPath

type UrlPath struct {
	// contains filtered or unexported fields
}

func NewUrlPath

func NewUrlPath(resourceT string) *UrlPath

func (*UrlPath) Path

func (up *UrlPath) Path(args interface{}) string

Jump to

Keyboard shortcuts

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