Add qiniu deployer
This commit is contained in:
@@ -32,6 +32,24 @@ func Req(url string, method string, body io.Reader, head map[string]string, opts
|
||||
}
|
||||
|
||||
func Req2GetReader(url string, method string, body io.Reader, head map[string]string, opts ...Option) (io.ReadCloser, error) {
|
||||
req := BuildReq(url, method, body, head)
|
||||
|
||||
return ToRequest(req, opts...)
|
||||
|
||||
}
|
||||
|
||||
func BuildReq(url string, method string, body io.Reader, head map[string]string) *http.Request {
|
||||
|
||||
// Create an http.Request instance
|
||||
req, _ := http.NewRequest(method, url, body)
|
||||
for k, v := range head {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
func ToRequest(req *http.Request, opts ...Option) (io.ReadCloser, error) {
|
||||
options := &Options{
|
||||
Timeout: 30000 * time.Millisecond,
|
||||
}
|
||||
@@ -39,13 +57,8 @@ func Req2GetReader(url string, method string, body io.Reader, head map[string]st
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
client := httpclient.NewClient(httpclient.WithHTTPTimeout(options.Timeout))
|
||||
|
||||
// Create an http.Request instance
|
||||
req, _ := http.NewRequest(method, url, body)
|
||||
for k, v := range head {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
client := httpclient.NewClient(httpclient.WithHTTPTimeout(options.Timeout))
|
||||
// Call the `Do` method, which has a similar interface to the `http.Do` method
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
@@ -53,6 +66,9 @@ func Req2GetReader(url string, method string, body io.Reader, head map[string]st
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
defer res.Body.Close()
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
fmt.Println(string(body))
|
||||
return nil, fmt.Errorf("status code is not 200: %d", res.StatusCode)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user