|
|
@ -1,8 +1,11 @@ |
|
|
|
package ghost |
|
|
|
package ghost |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
"sync" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/mailru/easyjson" |
|
|
|
"github.com/valyala/fasthttp" |
|
|
|
"github.com/valyala/fasthttp" |
|
|
|
) |
|
|
|
) |
|
|
|
// Ghost content data URIs:
|
|
|
|
// Ghost content data URIs:
|
|
|
@ -18,6 +21,8 @@ type HTTPClient struct { |
|
|
|
Secured bool |
|
|
|
Secured bool |
|
|
|
|
|
|
|
|
|
|
|
client *fasthttp.HostClient |
|
|
|
client *fasthttp.HostClient |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setupClientOnce sync.Once |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// setupClient creates the default http client
|
|
|
|
// setupClient creates the default http client
|
|
|
@ -31,3 +36,49 @@ func (g *HTTPClient) setupClient() { |
|
|
|
DisablePathNormalizing: true, |
|
|
|
DisablePathNormalizing: true, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// doQuery does the method and unmarshals the result into the easyjson Unmarshaler
|
|
|
|
|
|
|
|
func (g *HTTPClient) doQuery(method string, v easyjson.Unmarshaler, params ...QueryParam) (err error) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g.setupClientOnce.Do(g.setupClient) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
req := fasthttp.AcquireRequest() |
|
|
|
|
|
|
|
res := fasthttp.AcquireResponse() |
|
|
|
|
|
|
|
defer func() { |
|
|
|
|
|
|
|
fasthttp.ReleaseResponse(res) |
|
|
|
|
|
|
|
fasthttp.ReleaseRequest(req) |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uri := req.URI() |
|
|
|
|
|
|
|
uri.SetHost(g.Addr) |
|
|
|
|
|
|
|
uri.SetPath(method) |
|
|
|
|
|
|
|
uri.QueryArgs().Add("key", g.ContentKey) |
|
|
|
|
|
|
|
if g.client.IsTLS { |
|
|
|
|
|
|
|
uri.SetScheme("https") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, param := range params { |
|
|
|
|
|
|
|
param.Apply(&req.Header, uri.QueryArgs()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = g.client.DoTimeout(req, res, g.QueryTimeout) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if res.StatusCode() != fasthttp.StatusOK { |
|
|
|
|
|
|
|
return fmt.Errorf("non OK status code: %d", res.StatusCode()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resBytes := res.Body() |
|
|
|
|
|
|
|
if resBytes == nil && v == nil { |
|
|
|
|
|
|
|
return fmt.Errorf("nothing to unmarshal") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if resBytes == nil { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = easyjson.Unmarshal(resBytes, v) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |