Name: golang-restclient
Owner: MercadoLibre
Description: An extremely simple to use, lightweight, yet powerful REST Client
Created: 2017-05-22 20:41:05.0
Updated: 2018-04-18 00:29:13.0
Pushed: 2017-07-01 02:22:18.0
Size: 21
Language: Go
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
========
The Go http standard library is a great library, but it might sometimes be a bit too low level to use, and it doesn't offer features like fork-join requests for better performance, response caching based on headers, and the possibility to mockup responses.
GET
, POST
, PUT
, PATCH
, DELETE
, HEAD
& OPTIONS
HTTP verbsstring
, []byte
, struct
& map
.JSON
and XML
Content-Type. Default JSON.Caching is done by two strategies working together: Time To Live (TTL) and Least Recently Used (LRU). Objects are inserted in the cache based on Response Headers. You can establish a maximum Memory Size for the cache and objects are flushed based on time expiration (TTL) or by hitting the maximum memory limit. In the last case, least accessed objects will be removed first.
clone en tu gopath o goroot
clone https://github.com/mercadolibre/golang-restclient/
rt "github.com/mercadolibre/golang-restclient/rest"
:= rest.Get("https://api.restfulsite.com/resource")
sing a `string` as body
:= rest.Post("https://api.restfulsite.com/resource", "Body")
User struct {
Id int `json:"id"`
Name string `json:"name"`
:= new(User)
.Id = 1
.Name = "Hernan"
ody will be marshall as JSON
:= rest.Post("https://api.restfulsite.com/resource/1", body)
Println(resp)
ForkJoin let you fork requests, and wait until all of them have return.
Concurrent has methods for Get, Post, Put, Patch, Delete, Head & Options, with the almost the same API as the synchronous methods. The difference is that these methods return a FutureResponse, which holds a pointer to Response. Response inside FutureResponse is nil until request has finished.
f [3]*rest.FutureResponse
orkJoin will send all requests concurrently
nd will wait until all requests have their correspondent responses
.ForkJoin(func(c *rest.Concurrent) {
f[0] = c.Get("https://api.restfulsite.com/resource/1")
f[1] = c.Get("https://api.restfulsite.com/resource/2")
f[2] = c.Get("https://api.restfulsite.com/resource/3")
i := range f {
f[i].Response().StatusCode == http.StatusOK {
fmt.Println(f[i].Response())
Async let you make Restful requests in an asynchronous way, without blocking the go routine calling the Async function.
Whenever the Response is ready, the f function will be called back.
his won't be blocked.
.AsyncGet("https://api.restfulsite.com/user", func(r *rest.Response) {
if r.StatusCode == http.StatusOK {
fmt.Println(r)
}
his will be printed first.
Println("print first")
RequestBuilder gives you the power to go beyond defaults. It is possible to set up headers, timeout, baseURL, proxy, contentType, not to use cache, directly disabling timeout (in an explicit way), and setting max idle connections.
ou can reuse in every RequestBuilder
omPool := &rest.CustomPool{
MaxIdleConnsPerHost: 100,
ers := make(http.Header)
ers.Add("myHeader", "myValue")
rb = rest.RequestBuilder{
Headers: headers,
Timeout: 200 * time.Millisecond,
BaseURL: "https://baseURL",
Proxy: "http://myproxy",
ContentType: rest.JSON,
DisableCache: false,
DisableTimeout: false,
MaxIdleConnsPerHost: 10,
CustomPool: customPool,
:= rb.Get("/mypath")
When using mockups all requests will be sent to the mockup server. To activate the mockup environment you have two ways: using the flag -mock
est -mock
Or by programmatically starting the mockup server
tMockupServer()
L := "http://mytest.com/foo"
aders := make(http.Header)
aders.Add("Hello", "world")
:= rest.Mock{
URL: myURL,
HTTPMethod: http.MethodGet,
ReqHeaders: myHeaders,
RespHTTPCode: http.StatusOK,
RespBody: "foo",
.AddMockups(&mock)
rest.Get(myURL)