Commit 6676796c by zhengqiuyun86

httpClient封装

parent 33c19435
......@@ -3,6 +3,7 @@ module git.168cad.top/zhengqiuyun/rym-util
go 1.18
require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/go-playground/validator/v10 v10.11.0
github.com/go-redis/redis v6.15.9+incompatible
......@@ -15,7 +16,6 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/gin-contrib/cors v1.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
......
......@@ -231,8 +231,6 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fatih/set.v0 v0.2.1 h1:Xvyyp7LXu34P0ROhCyfXkmQCAoOUKb1E2JS9I7SE5CY=
gopkg.in/fatih/set.v0 v0.2.1/go.mod h1:5eLWEndGL4zGGemXWrKuts+wTJR0y+w+auqUJZbmyBg=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
......
......@@ -3,6 +3,7 @@ package util
import (
"fmt"
"net/http"
"net/url"
"testing"
"time"
)
......@@ -22,10 +23,12 @@ func TestDifference(t *testing.T) {
fmt.Println("a1与a2的差集分别是:", d1, d2)
}
func TestPostJSONStr(t *testing.T) {
responseData, err := PostFormStr("http://ex-test-dcxy-base-manage.168cad.top/v3/operate/balance/recharge/success", "{\"orderNo\":\"BRO165119944230021\"}", nil, http.Client{
var client = http.Client{
Timeout: 10 * time.Second,
})
}
func TestPostJSONStr(t *testing.T) {
responseData, err := PostFormStr(client, "http://ex-test-dcxy-base-manage.168cad.top/v3/operate/balance/recharge/success", "{\"orderNo\":\"BRO165119944230021\"}", nil)
if err != nil {
fmt.Printf("错误:%s\n", err.Error())
}
......@@ -33,9 +36,24 @@ func TestPostJSONStr(t *testing.T) {
}
func TestGet(t *testing.T) {
responseData, err := Get("https://test-go-rym-oxygen-manage-service.168cad.top/oxygen/u/price/rule/detail/info?id=1", http.Client{
Timeout: 10 * time.Second,
})
apiUrl := "https://test-go-rym-oxygen-manage-service.168cad.top/oxygen/u/price/rule/detail/info"
p := url.Values{}
p.Add("id", "1")
responseData, err := Get(client, apiUrl, p)
if err != nil {
fmt.Printf("错误:%s\n", err.Error())
}
fmt.Printf("请求结果:%s\n", string(responseData))
}
func TestGetHeader(t *testing.T) {
apiUrl := "https://test-go-rym-oxygen-manage-service.168cad.top/oxygen/u/price/rule/detail/info"
p := url.Values{}
p.Add("id", "1")
header := map[string]string{}
header["token"] = "oxygen:login:token:91c6aff9d32fb16f0404874d60466041"
responseData, err := GetHeader(client, apiUrl, p, header)
if err != nil {
fmt.Printf("错误:%s\n", err.Error())
}
......
......@@ -2,11 +2,14 @@ package util
import (
"bytes"
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/exception"
"github.com/gin-gonic/gin"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
......@@ -65,7 +68,8 @@ const (
XmlForm = "application/x-www-form-urlencoded;charset=utf-8"
)
func Get(url string, client http.Client) ([]byte, error) {
func Get(client http.Client, apiUrl string, params url.Values) ([]byte, error) {
url := fmt.Sprintf("%s?%s", apiUrl, params.Encode())
rep, err := client.Get(url)
if err != nil {
return nil, err
......@@ -78,7 +82,30 @@ func Get(url string, client http.Client) ([]byte, error) {
return data, nil
}
func PostFormStr(url string, params string, header map[string]string, client http.Client) ([]byte, error) {
func GetHeader(client http.Client, apiUrl string, params url.Values, header map[string]string) ([]byte, error) {
url := fmt.Sprintf("%s?%s", apiUrl, params.Encode())
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
if header != nil {
for k, v := range header {
request.Header.Add(k, v)
}
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return data, nil
}
func PostFormStr(client http.Client, url string, params string, header map[string]string) ([]byte, error) {
p := StrToUrlValue(params)
requestStr := p.Encode()
buf := strings.NewReader(requestStr)
......@@ -106,7 +133,7 @@ func PostFormStr(url string, params string, header map[string]string, client htt
return data, err
}
func PostJSONStr(url string, params string, header map[string]string, client http.Client) ([]byte, error) {
func PostJSONStr(client http.Client, url string, params string, header map[string]string) ([]byte, error) {
request, err := http.NewRequest(http.MethodPost, url, bytes.NewBufferString(params))
if err != nil {
return nil, err
......@@ -130,3 +157,38 @@ func PostJSONStr(url string, params string, header map[string]string, client htt
return data, err
}
func PostJson(client http.Client, url string, params interface{}) ([]byte, error) {
return PostJsonHeader(client, url, params, nil)
}
func PostJsonHeader(client http.Client, url string, params interface{}, header map[string]string) ([]byte, error) {
buf := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buf)
if err := encoder.Encode(params); err != nil {
return nil, err
}
request, err := http.NewRequest(http.MethodPost, url, buf)
if err != nil {
return nil, err
}
request.Header.Add("Content-Type", "application/json")
if header != nil {
for k, v := range header {
request.Header.Add(k, v)
}
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return data, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment