package thirdparty import ( "bytes" "fmt" "io/ioutil" "net/http" "time" hurl "net/url" "github.com/jaryhe/gopkgs/logger" jsoniter "github.com/json-iterator/go" "go.uber.org/zap" ) var json = jsoniter.ConfigCompatibleWithStandardLibrary func ProvincialPost(api string, accessToken string, data map[string]interface{}) (result []byte, err error) { defer func() { req, _ := json.MarshalToString(data) resultStr := "" if result != nil { resultStr = string(result) } logger.Info("thirdparty", zap.String("api", api), zap.String("request", req), zap.String("response", resultStr)) }() if accessToken != "" { api = fmt.Sprintf("%s?access_token=%s", api, accessToken) } dataBytes, _ := json.Marshal(data) req, err := http.NewRequest("POST", api, bytes.NewReader(dataBytes)) client := &http.Client{} client.Timeout = time.Duration(30) * time.Second resp, err := client.Do(req) if err != nil { return nil, err } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode) } defer resp.Body.Close() result, err = ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return result, nil } func ProvincialGet(api string, accessToken string, data map[string]string) (result []byte, err error) { defer func() { req, _ := json.MarshalToString(data) resultStr := "" if result != nil { resultStr = string(result) } logger.Info("thirdparty", zap.String("api", api), zap.String("request", req), zap.String("response", resultStr)) }() api = fmt.Sprintf("%s?access_token=%s", api, accessToken) if len(data) > 0 { var value hurl.Values for k, v := range data { value.Add(k, v) } api = fmt.Sprintf("%s&%s", api, value.Encode()) } req, err := http.NewRequest("GET", api, nil) client := &http.Client{} client.Timeout = time.Duration(30) * time.Second resp, err := client.Do(req) if err != nil { return nil, err } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode) } defer resp.Body.Close() result, err = ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return result, nil }