Documentation ¶
Index ¶
- Constants
- func Bytes(resp *EndpointResponse, err error) ([]byte, error)
- func Encode(v url.Values) string
- func FullApiWithHandlers(apiSpec interface{}) map[ServerResource]Handler
- func GetHeaderValue(key string, headers map[string][]string) string
- func LocalRequest(cr *ClientRequest) (*Request, *Response)
- type Client
- func (c *Client) Fetch(r *ClientRequest, ctx *RequestContext, v interface{}) (*EndpointResponse, error)
- func (c *Client) NewHttpRequest(cr *ClientRequest) (*http.Request, error)
- func (c *Client) Send(r *ClientRequest, ctx *RequestContext) (*EndpointResponse, error)
- func (t *Client) SetHeader(h interface{}) error
- type ClientRequest
- type ClientResource
- type DeleteHandler
- type EndpointResponse
- type FrozenCommunication
- type FrozenRequest
- type FrozenResponse
- type FrozenSender
- type FullResource
- type GetHandler
- type Handler
- type HandlerFunc
- type HeadHandler
- type IdIntArg
- type PatchHandler
- type PostHandler
- type PutHandler
- type Request
- type RequestContext
- type ResourceDef
- type ResourceEndpoint
- type ResourceSpec
- func (rs *ResourceSpec) Delete(args interface{}) *ClientRequest
- func (rs *ResourceSpec) Get(args interface{}) *ClientRequest
- func (rs *ResourceSpec) Head(args interface{}) *ClientRequest
- func (rs *ResourceSpec) Patch(args interface{}, body interface{}) *ClientRequest
- func (rs *ResourceSpec) Post(args interface{}, body interface{}) *ClientRequest
- func (rs *ResourceSpec) Put(args interface{}, body interface{}) *ClientRequest
- func (rs *ResourceSpec) ServeAll() ([]ServerResource, Handler)
- func (r *ResourceSpec) Use(def *ResourceDef) *ResourceSpec
- func (r *ResourceSpec) WithHandler(handler Handler) *ResourceSpec
- type Responder
- type Response
- type Sender
- type ServerResource
- type UrlPath
Constants ¶
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 ¶
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 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 (*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 (c *Client) Send(r *ClientRequest, ctx *RequestContext) (*EndpointResponse, error)
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 EndpointResponse ¶
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 FrozenSender ¶
type FullResource ¶
type FullResource interface { ClientResource ServerResource }
type GetHandler ¶
type HandlerFunc ¶
type HeadHandler ¶
type PatchHandler ¶
type PostHandler ¶
type PutHandler ¶
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) ContentType ¶
type RequestContext ¶
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 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 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) SetContentType ¶
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