Documentation
¶
Overview ¶
go-rip is simple rest json api client. It has fluent chainable api, so you can do request along with unmarshalling and error checking on single line.
First we prepare client that we will use in later examples:
client := rip.New("http://localhost/api/v1").Header("Token", "Token").AppendSlash(true)
Now we can have a look at some examples of rest json api calls
type Product struct {}
product := Product{}
status := 0
if err := client.Get("product", 1).Do(context.Background(), &product).Status(&status); err != nil {
panic(err)
}
In this example we did following:
- make GET http request to http://localhost/api/v1/product/1/
- we have unmarshalled json response to `product` variable
- we have set http status code into `status` variable
- we have checked for error
This is awesome!
Majority of methods return Client so you can chain your calls. Structures are immutable, so chaining creates new struct. Do method returns response, which provides also useful methods.
Let's do POST http call
if err := client.Post("product").Data(product).Do(context.Background()).Status(&status).Error(); err != nil {
panic(err)
}
Index ¶
Constants ¶
const (
DefaultUserAgent = "rip-1.0"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface {
// AppendSlash adds slashes on the end when not presented
AppendSlash(bool) Client
// Base sets base url, all rest endpoints are added to this url.
Base(string) Client
// BeforeSend you can change request
BeforeSend(func(r *http.Request)) Client
// Client override default http client.
Client(func() *http.Client) Client
// Data sets data to be marshalled to json
Data(interface{}) Client
// FromResponse constructs Response from *http.Response
FromResponse(hr *http.Response) Response
// Do performs request and returns Response
Do(ctx context.Context, target ...interface{}) Response
// Header sets header for request
Header(key, value string) Client
// Method sets http method
Method(method string, parts ...interface{}) Client
// Path sets path
Path(parts ...interface{}) Client
// QueryValues
QueryValues(values url.Values) Client
// Request returns prepared request
Request() *http.Request
// URL returns actual rest endpoint url
URL() *url.URL
// UserAgent overrides default user agent
UserAgent(agent string) Client
// Delete HTTP method
Delete(parts ...interface{}) Client
// Get HTTP method
Get(parts ...interface{}) Client
// Head HTTP method
Head(parts ...interface{}) Client
// Options HTTP method
Options(parts ...interface{}) Client
// Patch HTTP method
Patch(parts ...interface{}) Client
// Put HTTP method
Put(parts ...interface{}) Client
// Post HTTP method
Post(parts ...interface{}) Client
}
Client main object in go-rip. most of methods are chainable, so it provides nice fluent api. When Do method is called, Response is returned. This Response is wrapper around http.Response with useful methods.
type Response ¶
type Response interface {
// Body returns response body, if there was an error blank []byte will be returned
Body() []byte
// RawBody returns raw body response
Raw(*[]byte) Response
// Client returns original client who made the request. This is useful in retry scenarios.
Client() Client
// Do calls Do method on client.
Do(ctx context.Context, target ...interface{}) Response
// Error returns error if occurred
Error() error
// Header returns http header
Header() http.Header
// Status fills status into given pointer
Status(*int) Response
// Unmarshal unmarshals json response into target
Unmarshal(target interface{}) Response
}
Response is rest response representation. It wraps http.Response, and has body already read.