rest

package
v0.0.0-...-08a8a3a Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2019 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const MockNotFoundError string = "MockUp nil!"

MockNotFoundError ...

Variables

View Source
var DefaultConnectTimeout = 1500 * time.Millisecond

DefaultConnectTimeout ...

View Source
var DefaultMaxIdleConnsPerHost = 2

DefaultMaxIdleConnsPerHost is the default maxium idle connections to have per Host for all clients, that use *any* RequestBuilder that don't set a CustomPool

View Source
var DefaultTimeout = 500 * time.Millisecond

DefaultTimeout is the default timeout for all clients. DefaultConnectTimeout is the time it takes to make a connection Type: time.Duration

View Source
var MaxCacheSize = 1 * GB

MaxCacheSize is the maximum byte size to be hold the ResourceCache Default is 1 Gigabyte Type: rest.ByteSize

Functions

func AddMockups

func AddMockups(mocks ...*Mock) error

AddMockups ...

func AsyncDelete

func AsyncDelete(url string, f func(*Response))

AsyncDelete is the *asynchronous* option for DELETE. The go routine calling AsyncGet(), will not be blocked.

Whenever the Response is ready, the *f* function will be called back.

AsyncDelete uses the DefaultBuilder

func AsyncGet

func AsyncGet(url string, f func(*Response))

AsyncGet is the *asynchronous* option for GET. The go routine calling AsyncGet(), will not be blocked.

Whenever the Response is ready, the *f* function will be called back.

AsyncGet uses the DefaultBuilder

func AsyncHead

func AsyncHead(url string, f func(*Response))

AsyncHead is the *asynchronous* option for HEAD. The go routine calling AsyncGet(), will not be blocked.

Whenever the Response is ready, the *f* function will be called back.

AsyncHead uses the DefaultBuilder

func AsyncOptions

func AsyncOptions(url string, f func(*Response))

AsyncOptions is the *asynchronous* option for OPTIONS. The go routine calling AsyncGet(), will not be blocked.

Whenever the Response is ready, the *f* function will be called back.

AsyncOptions uses the DefaultBuilder

func AsyncPatch

func AsyncPatch(url string, body interface{}, f func(*Response))

AsyncPatch is the *asynchronous* option for PATCH. The go routine calling AsyncGet(), will not be blocked.

Whenever the Response is ready, the *f* function will be called back.

AsyncPatch uses the DefaultBuilder

func AsyncPost

func AsyncPost(url string, body interface{}, f func(*Response))

AsyncPost is the *asynchronous* option for POST. The go routine calling AsyncGet(), will not be blocked.

Whenever the Response is ready, the *f* function will be called back.

AsyncPost uses the DefaultBuilder

func AsyncPut

func AsyncPut(url string, body interface{}, f func(*Response))

AsyncPut is the *asynchronous* option for PUT. The go routine calling AsyncGet(), will not be blocked.

Whenever the Response is ready, the *f* function will be called back.

AsyncPut uses the DefaultBuilder

func FlushMockups

func FlushMockups()

FlushMockups ...

func ForkJoin

func ForkJoin(f func(*Concurrent))

ForkJoin let you *fork* requests, and *wait* until all of them have return.

func StartMockupServer

func StartMockupServer()

StartMockupServer sets the enviroment to send all client requests to the mockup server.

func StopMockupServer

func StopMockupServer()

StopMockupServer stop sending requests to the mockup server

Types

type BasicAuth

type BasicAuth struct {
	UserName string
	Password string
}

BasicAuth allows to set UserName and Password for a given RequestBuilder

type ByteSize

type ByteSize int64

ByteSize is a helper for configuring MaxCacheSize

const (

	// KB = KiloBytes
	KB ByteSize = 1 << (10 * iota)

	// MB = MegaBytes
	MB

	// GB = GygaBytes
	GB
)

type Concurrent

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

Concurrent implements methods for HTTP verbs. The difference with synchronous API is that concurrent methods return a FutureResponse which hold a pointer to a Response, which is nil until the request has finished.

func (*Concurrent) Delete

func (c *Concurrent) Delete(url string) *FutureResponse

Delete perform a DELETE HTTP verb to the specified URL concurrently.

func (*Concurrent) Get

func (c *Concurrent) Get(url string) *FutureResponse

Get perform a GET HTTP verb to the specified URL concurrently.

func (*Concurrent) Head

func (c *Concurrent) Head(url string) *FutureResponse

Head perform a HEAD HTTP verb to the specified URL concurrently.

func (*Concurrent) Options

func (c *Concurrent) Options(url string) *FutureResponse

Options perform a OPTIONS HTTP verb to the specified URL concurrently.

func (*Concurrent) Patch

func (c *Concurrent) Patch(url string, body interface{}) *FutureResponse

Patch perform a PATCH HTTP verb to the specified URL concurrently.

Body could be any of the form: string, []byte, struct & map.

func (*Concurrent) Post

func (c *Concurrent) Post(url string, body interface{}) *FutureResponse

Post perform a POST HTTP verb to the specified URL concurrently.

Body could be any of the form: string, []byte, struct & map.

func (*Concurrent) Put

func (c *Concurrent) Put(url string, body interface{}) *FutureResponse

Put perform a PUT HTTP verb to the specified URL concurrently.

Body could be any of the form: string, []byte, struct & map.

type ContentType

type ContentType int

ContentType represents the HTTP Vebs body's type

const (
	// JSON represents a JSON content type
	JSON ContentType = iota
	// XML represents an XML content type
	XML
	// BYTES represents a plain content type
	BYTES
)

type CustomPool

type CustomPool struct {
	MaxIdleConnsPerHost int
	Proxy               string

	// Public for custom fine tuning
	Transport http.RoundTripper
}

CustomPool defines a separated internal *transport* and connection pooling.

type FutureResponse

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

FutureResponse represents a response to be completed after a ForkJoin operation is done

FutureResponse will never be nil and has a Response fucntion for getting the Response that will be nil after the ForkJoin operation is completed.

func (*FutureResponse) Response

func (fr *FutureResponse) Response() *Response

Response gives you the Response of a Request after the ForkJoin operation is completed. Response will be nil if the ForkJoin operation is not completed.

type Mock

type Mock struct {

	// Request URL
	URL string

	// Request HTTP Method (GET, POST, PUT, PATCH, HEAD, DELETE, OPTIONS)
	// As a good practice use the constants in http package (http.MethodGet, etc.)
	HTTPMethod string

	// Request array Headers
	ReqHeaders http.Header

	// Request Body, used with POST, PUT & PATCH
	ReqBody string

	// Response HTTP Code
	RespHTTPCode int

	// Response Array Headers
	RespHeaders http.Header

	// Response Body
	RespBody string
}

Mock serves the purpose of creating Mockups. All requests will be sent to the mockup server if mockup is activated. To activate the mockup *environment* you have to programmatically start the mockup server

Or by programmatically starting the mockup server

StartMockupServer()

type RequestBuilder

type RequestBuilder struct {

	// Headers to be send in the request.
	Headers http.Header

	// Timeout to complete request
	Timeout time.Duration

	// ConnectionTimeout bounds the time spent obtaining a successful connection
	ConnectionTimeout time.Duration

	// Base url is used to build the entire url for make the request (BaseURL + URI)
	BaseURL string

	// ContentType
	ContentType ContentType

	// Disable internal caching of response
	DisableCache bool

	// Disable timeout.
	DisableTimeout bool

	// Set the http client to follow a redirect if it gets a 3xx response from the server
	FollowRedirect bool

	// CustomPool create a pool if you don't want to share the *transport*, with others RequestBuilders
	CustomPool *CustomPool

	// Set basic Auth created request
	BasicAuth *BasicAuth

	// Set a specific user agent for the created request
	UserAgent string

	// Public for custom fine tuning
	Client *http.Client
	// contains filtered or unexported fields
}

RequestBuilder is the baseline to create requests There is a DefaultBuilder that you may use for simple requests RequestBuilder is thread-safe and should be stored for later usage.

func (*RequestBuilder) AsyncDelete

func (rb *RequestBuilder) AsyncDelete(url string, f func(*Response))

AsyncDelete ...

func (*RequestBuilder) AsyncGet

func (rb *RequestBuilder) AsyncGet(url string, f func(*Response))

AsyncGet ...

func (*RequestBuilder) AsyncHead

func (rb *RequestBuilder) AsyncHead(url string, f func(*Response))

AsyncHead ...

func (*RequestBuilder) AsyncOptions

func (rb *RequestBuilder) AsyncOptions(url string, f func(*Response))

AsyncOptions ...

func (*RequestBuilder) AsyncPatch

func (rb *RequestBuilder) AsyncPatch(url string, body interface{}, f func(*Response))

AsyncPatch ...

func (*RequestBuilder) AsyncPost

func (rb *RequestBuilder) AsyncPost(url string, body interface{}, f func(*Response))

AsyncPost ...

func (*RequestBuilder) AsyncPut

func (rb *RequestBuilder) AsyncPut(url string, body interface{}, f func(*Response))

AsyncPut ...

func (*RequestBuilder) Delete

func (rb *RequestBuilder) Delete(url string) *Response

Delete ...

func (*RequestBuilder) ForkJoin

func (rb *RequestBuilder) ForkJoin(f func(c *Concurrent))

ForkJoin let you *fork* requests, and *wait* until all of them have return.

var futureA, futureB *rest.FutureResponse

rest.ForkJoin(func(c *rest.Concurrent){
	futureA = c.Get("/url/1")
	futureB = c.Get("/url/2")
})

fmt.Println(futureA.Response())
fmt.Println(futureB.Response())

func (*RequestBuilder) Get

func (rb *RequestBuilder) Get(url string) *Response

Get ...

func (*RequestBuilder) Head

func (rb *RequestBuilder) Head(url string) *Response

Head ...

func (*RequestBuilder) Options

func (rb *RequestBuilder) Options(url string) *Response

Options ...

func (*RequestBuilder) Patch

func (rb *RequestBuilder) Patch(url string, body interface{}) *Response

Patch ...

func (*RequestBuilder) Post

func (rb *RequestBuilder) Post(url string, body interface{}) *Response

Post ...

func (*RequestBuilder) Put

func (rb *RequestBuilder) Put(url string, body interface{}) *Response

Put ...

type Response

type Response struct {
	*http.Response
	Err error
	// contains filtered or unexported fields
}

Response structure

func Delete

func Delete(url string) *Response

Delete handles a DELETE HTTP verb to an specified URL.

In RESTful, DELETE is used to "delete" an specified resource from the server. Client should expect a response status code of 200(OK), 400(Bad Request), 401(Unauthorized), 403(Forbiden), 404(Not Found), 405(Method Not Allowed).

Delete uses the DefaultBuilder.

func Get

func Get(url string) *Response

Get handles a GET HTTP verb to an specified URL.

In RESTful, GET is used for "reading" or retrieving a resource. Client should expect a response status code of 200(OK) if resource exists, 404(Not Found) if it doesn't, or 400(Bad Request).

Get uses the DefaultBuilder.

func Head(url string) *Response

Head issues a HEAD HTTP verb to the specified URL

In Restful, HEAD is used to "read" a resource headers only. Client should expect a response status code of 200(OK) if resource exists, 404(Not Found) if it doesn't, or 400(Bad Request).

Head uses the DefaultBuilder.

func Options

func Options(url string) *Response

Options issues a OPTIONS HTTP verb to the specified URL

In Restful, OPTIONS is used to get information about the resource and supported HTTP verbs. Client should expect a response status code of 200(OK) if resource exists, 404(Not Found) if it doesn't, or 400(Bad Request).

func Patch

func Patch(url string, body interface{}) *Response

Patch issues a PATCH HTTP verb to the specified URL

In Restful, PATCH is used for "partially updating" a resource. Client should expect a response status code of of 200(OK), 404(Not Found), or 400(Bad Request). 200(OK) could be also 204(No Content)

Body could be any of the form: string, []byte, struct & map.

Patch uses the DefaultBuilder.

func Post

func Post(url string, body interface{}) *Response

Post handles a POST HTTP verb to an specified URL.

In RESTful, POST is used to send user-generated data to the server. This data is stored in a subordinate of the resourse identified in the Request-URI. In other words, POST append a resourse at the end of a given url. Client should expect a response status code of 201(Created), 400(Bad Request), 404(Not Found), 405(Method Not Allowed) or 409(Conflict) if resource already exist.

Body could be any of the form: string, []byte, struct & map.

func Put

func Put(url string, body interface{}) *Response

Put handles a PUT HTTP verb to an specified URL

In RESTful, PUT is used to send user-generated data to the server. Knowing the exact URI of the resourse, it creates it or, if the resourse already exists it ovewrites it with the Request-body data. PUT verb is idempotent, it means that if you apply it n times it will always return the same result. Client should expect a response status code of 200(OK), 201(Created), 204(No Content), 400(Bad Request), 404(Not Found), 405(Method Not Allowed).

Body could be any of the form: string, []byte, struct & map.

Put uses the DefaultBuilder.

func (*Response) Bytes

func (r *Response) Bytes() []byte

Bytes return the Response body as a slice of bytes.

func (*Response) CacheHit

func (r *Response) CacheHit() bool

CacheHit shows if a response was get from the cache.

func (*Response) Debug

func (r *Response) Debug() string

Debug let any req/res to be dumped, showing how the req/res went through the wire, only if debug mode is *on* on RequestBuilder

func (*Response) FillUp

func (r *Response) FillUp(fill interface{}) error

FillUp set the *fill* parameter with the corresponding JSON or XML response. fill could be `struct`or `map[string]interface{}`

func (*Response) String

func (r *Response) String() string

String return the Response body as a string

Jump to

Keyboard shortcuts

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