136 lines
4.0 KiB
Go
136 lines
4.0 KiB
Go
package httputil
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func ClientPostJson(connTimeout int, url string, headers map[string]string, data any) (*http.Response, error) {
|
|
body, err := dataToJsonReader(data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req, err := http.NewRequest(http.MethodPost, url, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
for k, v := range headers {
|
|
req.Header.Set(k, v)
|
|
}
|
|
cl := getClientByTimeoutSet(connTimeout)
|
|
return cl.Do(req)
|
|
}
|
|
|
|
func DefaultClientPostJson(url string, headers map[string]string, data any) (*http.Response, error) {
|
|
return ClientPostJson(defaultTimeOut, url, headers, data)
|
|
}
|
|
|
|
func ClientPostJsonWithProxy(connTimeout int, p *ProxyCfg, url string, headers map[string]string, params any) (*http.Response, error) {
|
|
if p == nil {
|
|
return nil, errProxyNil
|
|
}
|
|
pUrl, pHeaders, err := p.Build(url, headers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ClientPostJson(connTimeout, pUrl, pHeaders, params)
|
|
}
|
|
|
|
func DefaultClientPostJsonWithProxy(p *ProxyCfg, url string, headers map[string]string, params any) (*http.Response, error) {
|
|
return ClientPostJsonWithProxy(defaultTimeOut, p, url, headers, params)
|
|
}
|
|
|
|
func ClientPostJsonWithRespWithProxy(connTimeout int, p *ProxyCfg, bind any, url string, headers map[string]string, data any) (int, error) {
|
|
resp, err := ClientPostJsonWithProxy(connTimeout, p, url, headers, data)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
ct, err := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
if err != nil {
|
|
return resp.StatusCode, err
|
|
}
|
|
return resp.StatusCode, json.Unmarshal(ct, &bind)
|
|
}
|
|
|
|
func DefaultClientPostJsonWithRespWithProxy(p *ProxyCfg, bind any, url string, headers map[string]string, data any) (int, error) {
|
|
return ClientPostJsonWithRespWithProxy(defaultTimeOut, p, bind, url, headers, data)
|
|
}
|
|
|
|
func ClientPostJsonWithResp(connTimeout int, bind any, url string, headers map[string]string, data any) (int, error) {
|
|
resp, err := ClientPostJson(connTimeout, url, headers, data)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
ct, err := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
if err != nil {
|
|
return resp.StatusCode, err
|
|
}
|
|
return resp.StatusCode, json.Unmarshal(ct, &bind)
|
|
}
|
|
|
|
func DefaultClientPostJsonWithResp(bind any, url string, headers map[string]string, data any) (int, error) {
|
|
return ClientPostJsonWithResp(defaultTimeOut, bind, url, headers, data)
|
|
}
|
|
|
|
func ClientPostJsonWithCtx(ctx context.Context, connTimeout int, url string, headers map[string]string, data any) (*http.Response, error) {
|
|
cl := getClientByTimeoutSet(connTimeout)
|
|
body, err := dataToJsonReader(data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req, err := http.NewRequest(http.MethodPost, url, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
for k, v := range headers {
|
|
req.Header.Set(k, v)
|
|
}
|
|
req = req.WithContext(ctx)
|
|
return cl.Do(req)
|
|
}
|
|
|
|
func DefaultClientPostJsonWithCtx(ctx context.Context, url string, headers map[string]string, data any) (*http.Response, error) {
|
|
return ClientPostJsonWithCtx(ctx, defaultTimeOut, url, headers, data)
|
|
}
|
|
|
|
func ClientPostJsonWithRespWithCtx(ctx context.Context, bind any, connTimeout int, url string, headers map[string]string, data any) (int, error) {
|
|
resp, err := ClientPostJsonWithCtx(ctx, connTimeout, url, headers, data)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
ct, err := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
if err != nil {
|
|
return resp.StatusCode, err
|
|
}
|
|
return resp.StatusCode, json.Unmarshal(ct, &bind)
|
|
}
|
|
|
|
func DefaultClientPostJsonWithRespWithCtx(ctx context.Context, bind any, url string, headers map[string]string, data any) (int, error) {
|
|
return ClientPostJsonWithRespWithCtx(ctx, bind, defaultTimeOut, url, headers, data)
|
|
}
|
|
|
|
func dataToJsonReader(data any) (*bytes.Reader, error) {
|
|
var body []byte
|
|
switch t := data.(type) {
|
|
case []byte:
|
|
body = t
|
|
case string:
|
|
body = []byte(t)
|
|
default:
|
|
var err error
|
|
body, err = json.Marshal(data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return bytes.NewReader(body), nil
|
|
}
|