1
This commit is contained in:
@@ -3,6 +3,7 @@ package topsdk
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
util2 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/util"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@@ -12,56 +13,53 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
util2 "topsdk/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TopClient struct {
|
type TopClient struct {
|
||||||
AppKey string
|
AppKey string
|
||||||
AppSecret string
|
AppSecret string
|
||||||
ServerUrl string
|
ServerUrl string
|
||||||
Format string
|
Format string
|
||||||
SignMethod string
|
SignMethod string
|
||||||
ConnectTimeout int64
|
ConnectTimeout int64
|
||||||
ReadTimeout int64
|
ReadTimeout int64
|
||||||
Version string
|
Version string
|
||||||
Simplify bool
|
Simplify bool
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
}
|
}
|
||||||
type HttpTransportConfig struct {
|
type HttpTransportConfig struct {
|
||||||
DialTimeout int64
|
DialTimeout int64
|
||||||
KeepAlive int64
|
KeepAlive int64
|
||||||
MaxIdleConns int
|
MaxIdleConns int
|
||||||
MaxIdleConnsPerHost int
|
MaxIdleConnsPerHost int
|
||||||
IdleConnTimeout int64
|
IdleConnTimeout int64
|
||||||
MaxConnsPerHost int
|
MaxConnsPerHost int
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultTopClient(AppKey string,AppSecret string,ServerUrl string, connectTimeount int64, readTimeout int64) TopClient {
|
func NewDefaultTopClient(AppKey string, AppSecret string, ServerUrl string, connectTimeount int64, readTimeout int64) TopClient {
|
||||||
var httpTransportConfig = &HttpTransportConfig{
|
var httpTransportConfig = &HttpTransportConfig{
|
||||||
DialTimeout: 30000,
|
DialTimeout: 30000,
|
||||||
KeepAlive: 30000,
|
KeepAlive: 30000,
|
||||||
MaxIdleConns: 100,
|
MaxIdleConns: 100,
|
||||||
MaxIdleConnsPerHost: 50,
|
MaxIdleConnsPerHost: 50,
|
||||||
IdleConnTimeout: 30000,
|
IdleConnTimeout: 30000,
|
||||||
}
|
}
|
||||||
return NewTopClientWithConfig(AppKey,AppSecret,ServerUrl,connectTimeount,readTimeout,httpTransportConfig)
|
return NewTopClientWithConfig(AppKey, AppSecret, ServerUrl, connectTimeount, readTimeout, httpTransportConfig)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewTopClientWithConfig(AppKey string, AppSecret string, ServerUrl string, connectTimeount int64, readTimeout int64, httpTransportConfig *HttpTransportConfig) TopClient {
|
||||||
func NewTopClientWithConfig(AppKey string,AppSecret string,ServerUrl string, connectTimeount int64, readTimeout int64,httpTransportConfig *HttpTransportConfig) TopClient {
|
|
||||||
httpClient := http.Client{
|
httpClient := http.Client{
|
||||||
Timeout: time.Duration(connectTimeount) * time.Millisecond,
|
Timeout: time.Duration(connectTimeount) * time.Millisecond,
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
DialContext: (&net.Dialer{
|
DialContext: (&net.Dialer{
|
||||||
Timeout: time.Duration(httpTransportConfig.DialTimeout) * time.Millisecond,
|
Timeout: time.Duration(httpTransportConfig.DialTimeout) * time.Millisecond,
|
||||||
KeepAlive: time.Duration(httpTransportConfig.KeepAlive) * time.Millisecond,
|
KeepAlive: time.Duration(httpTransportConfig.KeepAlive) * time.Millisecond,
|
||||||
}).DialContext,
|
}).DialContext,
|
||||||
ForceAttemptHTTP2: true,
|
ForceAttemptHTTP2: true,
|
||||||
MaxIdleConns: httpTransportConfig.MaxIdleConns,
|
MaxIdleConns: httpTransportConfig.MaxIdleConns,
|
||||||
MaxIdleConnsPerHost: httpTransportConfig.MaxIdleConnsPerHost,
|
MaxIdleConnsPerHost: httpTransportConfig.MaxIdleConnsPerHost,
|
||||||
IdleConnTimeout: time.Duration(httpTransportConfig.IdleConnTimeout) * time.Millisecond,
|
IdleConnTimeout: time.Duration(httpTransportConfig.IdleConnTimeout) * time.Millisecond,
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
},
|
},
|
||||||
@@ -80,10 +78,7 @@ func NewTopClientWithConfig(AppKey string,AppSecret string,ServerUrl string, con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *TopClient) ExecuteWithSession(method string, data map[string]interface{}, fileData map[string]interface{}, session string) (string, error) {
|
||||||
|
|
||||||
|
|
||||||
func (client *TopClient) ExecuteWithSession(method string,data map[string]interface{},fileData map[string]interface{},session string) (string,error){
|
|
||||||
var publicParam = make(map[string]interface{})
|
var publicParam = make(map[string]interface{})
|
||||||
publicParam["method"] = method
|
publicParam["method"] = method
|
||||||
publicParam["app_key"] = client.AppKey
|
publicParam["app_key"] = client.AppKey
|
||||||
@@ -100,100 +95,96 @@ func (client *TopClient) ExecuteWithSession(method string,data map[string]interf
|
|||||||
// 构建url
|
// 构建url
|
||||||
serverUrl, _ := url.Parse(client.ServerUrl)
|
serverUrl, _ := url.Parse(client.ServerUrl)
|
||||||
urlValues := url.Values{}
|
urlValues := url.Values{}
|
||||||
urlValues.Add("sign",sign)
|
urlValues.Add("sign", sign)
|
||||||
for k,v := range publicParam{
|
for k, v := range publicParam {
|
||||||
urlValues.Add(k,fmt.Sprint(v))
|
urlValues.Add(k, fmt.Sprint(v))
|
||||||
}
|
}
|
||||||
serverUrl.RawQuery = urlValues.Encode()
|
serverUrl.RawQuery = urlValues.Encode()
|
||||||
urlPath := serverUrl.String()
|
urlPath := serverUrl.String()
|
||||||
// 构建body
|
// 构建body
|
||||||
if fileData != nil && len(fileData) > 0 {
|
if fileData != nil && len(fileData) > 0 {
|
||||||
return doPostWithFile(urlPath,data,fileData,client.httpClient)
|
return doPostWithFile(urlPath, data, fileData, client.httpClient)
|
||||||
}else {
|
} else {
|
||||||
return doPost(urlPath, data, client.httpClient)
|
return doPost(urlPath, data, client.httpClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func doPost(urlPath string,data map[string]interface{},httpClient *http.Client) (string,error){
|
func doPost(urlPath string, data map[string]interface{}, httpClient *http.Client) (string, error) {
|
||||||
bodyParam := url.Values{}
|
bodyParam := url.Values{}
|
||||||
for k,v := range data{
|
for k, v := range data {
|
||||||
bodyParam.Add(k,fmt.Sprint(v))
|
bodyParam.Add(k, fmt.Sprint(v))
|
||||||
}
|
}
|
||||||
resp, err := httpClient.Post(urlPath,"application/x-www-form-urlencoded",strings.NewReader(bodyParam.Encode()))
|
resp, err := httpClient.Post(urlPath, "application/x-www-form-urlencoded", strings.NewReader(bodyParam.Encode()))
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
}
|
}
|
||||||
if(err != nil){
|
if err != nil {
|
||||||
log.Println("http.PostForm error",err)
|
log.Println("http.PostForm error", err)
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if(err != nil){
|
if err != nil {
|
||||||
log.Println("ioutil.ReadAll",err)
|
log.Println("ioutil.ReadAll", err)
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
return string(body),nil
|
return string(body), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func doPostWithFile(urlPath string,data map[string]interface{},fileData map[string]interface{}, httpClient *http.Client) (string,error){
|
func doPostWithFile(urlPath string, data map[string]interface{}, fileData map[string]interface{}, httpClient *http.Client) (string, error) {
|
||||||
bodyBuf := &bytes.Buffer{}
|
bodyBuf := &bytes.Buffer{}
|
||||||
writer := multipart.NewWriter(bodyBuf)
|
writer := multipart.NewWriter(bodyBuf)
|
||||||
for k,v := range data{
|
for k, v := range data {
|
||||||
err := writer.WriteField(k, fmt.Sprint(v))
|
err := writer.WriteField(k, fmt.Sprint(v))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for k,v := range fileData {
|
for k, v := range fileData {
|
||||||
value , ok := v.([]byte)
|
value, ok := v.([]byte)
|
||||||
if ok {
|
if ok {
|
||||||
fileWriter, err := writer.CreateFormFile(k, "file")
|
fileWriter, err := writer.CreateFormFile(k, "file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
_, err = io.Copy(fileWriter, bytes.NewReader(value))
|
_, err = io.Copy(fileWriter, bytes.NewReader(value))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
}else {
|
} else {
|
||||||
value , ok := v.(*util2.FileItem)
|
value, ok := v.(*util2.FileItem)
|
||||||
if ok {
|
if ok {
|
||||||
fileWriter, err := writer.CreateFormFile(k, value.FileName)
|
fileWriter, err := writer.CreateFormFile(k, value.FileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
_, err = io.Copy(fileWriter, bytes.NewReader(value.Content))
|
_, err = io.Copy(fileWriter, bytes.NewReader(value.Content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := writer.Close()
|
err := writer.Close()
|
||||||
if(err != nil){
|
if err != nil {
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := httpClient.Post(urlPath,writer.FormDataContentType(),bodyBuf)
|
resp, err := httpClient.Post(urlPath, writer.FormDataContentType(), bodyBuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("http.PostForm error",err)
|
log.Println("http.PostForm error", err)
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ioutil.ReadAll",err)
|
log.Println("ioutil.ReadAll", err)
|
||||||
return "",err
|
return "", err
|
||||||
}
|
}
|
||||||
return string(body),nil
|
return string(body), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *TopClient) Execute(method string, data map[string]interface{}, fileData map[string]interface{}) (string, error) {
|
||||||
func (client *TopClient) Execute(method string,data map[string]interface{},fileData map[string]interface{}) (string,error){
|
return client.ExecuteWithSession(method, data, fileData, "")
|
||||||
return client.ExecuteWithSession(method,data,fileData,"")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user