init project
This commit is contained in:
parent
e7cb80d0b2
commit
1a05deb89d
141
sdk/client.go
141
sdk/client.go
@ -10,6 +10,9 @@ import (
|
|||||||
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -28,6 +31,33 @@ type Client struct {
|
|||||||
readTimeout time.Duration
|
readTimeout time.Duration
|
||||||
connectTimeout time.Duration
|
connectTimeout time.Duration
|
||||||
config *Config
|
config *Config
|
||||||
|
httpProxy string
|
||||||
|
httpsProxy string
|
||||||
|
noProxy string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) GetNoProxy() string {
|
||||||
|
return client.noProxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) SetNoProxy(noProxy string) {
|
||||||
|
client.noProxy = noProxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) GetHttpsProxy() string {
|
||||||
|
return client.httpsProxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) SetHttpsProxy(httpsProxy string) {
|
||||||
|
client.httpsProxy = httpsProxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) SetHttpProxy(httpProxy string) {
|
||||||
|
client.httpProxy = httpProxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) GetHttpProxy() string {
|
||||||
|
return client.httpProxy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) GetHTTPSInsecure() bool {
|
func (client *Client) GetHTTPSInsecure() bool {
|
||||||
@ -146,7 +176,32 @@ func (client *Client) DoAction(request requests.AcsRequest, response responses.A
|
|||||||
|
|
||||||
func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) {
|
func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) {
|
||||||
|
|
||||||
|
httpRequest, err := client.buildRequestWithSigner(request, signer)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
client.setTimeOut(request)
|
client.setTimeOut(request)
|
||||||
|
proxy, err := client.getHttpProxy(httpRequest.URL.Scheme)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
noProxy := client.getNoProxy(httpRequest.URL.Scheme)
|
||||||
|
var flag bool
|
||||||
|
for _, value := range noProxy {
|
||||||
|
if strings.HasPrefix(value, "*") {
|
||||||
|
value = fmt.Sprint(".%s", value)
|
||||||
|
}
|
||||||
|
noProxyReg, err := regexp.Compile(value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if noProxyReg.MatchString(httpRequest.Host) {
|
||||||
|
flag = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
|
if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
|
||||||
if trans.TLSClientConfig != nil {
|
if trans.TLSClientConfig != nil {
|
||||||
@ -157,13 +212,95 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if proxy != nil && !flag {
|
||||||
|
trans.Proxy = http.ProxyURL(proxy)
|
||||||
|
}
|
||||||
|
|
||||||
client.httpClient.Transport = trans
|
client.httpClient.Transport = trans
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpResponse *http.Response
|
var httpResponse *http.Response
|
||||||
|
|
||||||
|
httpResponse, err = hookDo(client.httpClient.Do)(httpRequest)
|
||||||
|
if err == nil {
|
||||||
|
for key, val := range httpResponse.Header {
|
||||||
|
fmt.Println(key, val)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *requests.HttpRequest, err error) {
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *http.Request, err error) {
|
||||||
|
// init param
|
||||||
|
request.SetDomain("")
|
||||||
|
if request.GetScheme() == "" {
|
||||||
|
request.SetScheme(client.config.Scheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = requests.InitParam(request)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// build request
|
||||||
|
err = auth.Sign(request, signer)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
requestMethod := request.GetMethod()
|
||||||
|
requestUrl := request.BuildUrl()
|
||||||
|
body := request.GetBodyReader()
|
||||||
|
httpRequest, err = http.NewRequest(requestMethod, requestUrl, body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, val := range request.GetHeaders() {
|
||||||
|
httpRequest.Header[key] = []string{val}
|
||||||
|
}
|
||||||
|
|
||||||
|
if host, isContainsHost := request.GetHeaders()["host"]; isContainsHost {
|
||||||
|
httpRequest.Host = host
|
||||||
|
}
|
||||||
|
userAgent := DefaultUserAgent
|
||||||
|
httpRequest.Header.Set("User-Agent", userAgent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) getHttpProxy(scheme string) (proxy *url.URL, err error) {
|
||||||
|
switch scheme {
|
||||||
|
case "https":
|
||||||
|
if client.GetHttpsProxy() != "" {
|
||||||
|
proxy, err = url.Parse(client.httpsProxy)
|
||||||
|
} else if rawurl := os.Getenv("HTTPS_PROXY"); rawurl != "" {
|
||||||
|
proxy, err = url.Parse(rawurl)
|
||||||
|
} else if rawurl := os.Getenv("https_proxy"); rawurl != "" {
|
||||||
|
proxy, err = url.Parse(rawurl)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if client.GetHttpProxy() != "" {
|
||||||
|
proxy, err = url.Parse(client.httpProxy)
|
||||||
|
} else if rawurl := os.Getenv("HTTP_PROXY"); rawurl != "" {
|
||||||
|
proxy, err = url.Parse(rawurl)
|
||||||
|
} else if rawurl := os.Getenv("http_proxy"); rawurl != "" {
|
||||||
|
proxy, err = url.Parse(rawurl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) getNoProxy(scheme string) []string {
|
||||||
|
var urls []string
|
||||||
|
if client.GetNoProxy() != "" {
|
||||||
|
urls = strings.Split(client.noProxy, ",")
|
||||||
|
} else if rawurl := os.Getenv("NO_PROXY"); rawurl != "" {
|
||||||
|
urls = strings.Split(rawurl, ",")
|
||||||
|
} else if rawurl := os.Getenv("no_proxy"); rawurl != "" {
|
||||||
|
urls = strings.Split(rawurl, ",")
|
||||||
|
}
|
||||||
|
return urls
|
||||||
|
}
|
||||||
|
|
||||||
|
func hookDo(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
|
||||||
|
return fn
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package requests
|
package requests
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
type CommonRequest struct {
|
type CommonRequest struct {
|
||||||
*baseRequest
|
*baseRequest
|
||||||
Product string
|
Product string
|
||||||
@ -20,3 +22,7 @@ func (request *CommonRequest) BuildUrl() string {
|
|||||||
func (request *CommonRequest) BuildQueries() string {
|
func (request *CommonRequest) BuildQueries() string {
|
||||||
return request.Ontology.BuildQueries()
|
return request.Ontology.BuildQueries()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (request *CommonRequest) GetBodyReader() io.Reader {
|
||||||
|
return request.Ontology.GetBodyReader()
|
||||||
|
}
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
package requests
|
package requests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type HttpRequest struct {
|
type HttpRequest struct {
|
||||||
*baseRequest
|
*baseRequest
|
||||||
}
|
}
|
||||||
@ -27,3 +33,12 @@ func (request *HttpRequest) InitWithApiInfo(product, version, action string) {
|
|||||||
request.version = version
|
request.version = version
|
||||||
request.actionName = action
|
request.actionName = action
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (request *HttpRequest) GetBodyReader() io.Reader {
|
||||||
|
if request.FormParams != nil && len(request.FormParams) > 0 {
|
||||||
|
formString := utils.GetUrlFormedMap(request.FormParams)
|
||||||
|
return strings.NewReader(formString)
|
||||||
|
} else {
|
||||||
|
return strings.NewReader("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package requests
|
package requests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -47,6 +48,7 @@ type AcsRequest interface {
|
|||||||
GetScheme() string
|
GetScheme() string
|
||||||
GetDomain() string
|
GetDomain() string
|
||||||
GetActionName() string
|
GetActionName() string
|
||||||
|
GetHeaders() map[string]string
|
||||||
|
|
||||||
BuildUrl() string
|
BuildUrl() string
|
||||||
BuildQueries() string
|
BuildQueries() string
|
||||||
@ -54,6 +56,11 @@ type AcsRequest interface {
|
|||||||
SetScheme(scheme string)
|
SetScheme(scheme string)
|
||||||
SetDomain(host string)
|
SetDomain(host string)
|
||||||
SetStringToSign(stringToSign string)
|
SetStringToSign(stringToSign string)
|
||||||
|
GetBodyReader() io.Reader
|
||||||
|
|
||||||
|
addHeaderParam(key, value string)
|
||||||
|
addQueryParam(key, value string)
|
||||||
|
addFormParam(key, value string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type baseRequest struct {
|
type baseRequest struct {
|
||||||
@ -83,6 +90,10 @@ type baseRequest struct {
|
|||||||
stringToSign string
|
stringToSign string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (request *baseRequest) GetHeaders() map[string]string {
|
||||||
|
return request.Headers
|
||||||
|
}
|
||||||
|
|
||||||
func (request *baseRequest) GetActionName() string {
|
func (request *baseRequest) GetActionName() string {
|
||||||
return request.actionName
|
return request.actionName
|
||||||
}
|
}
|
||||||
@ -143,6 +154,18 @@ func (request *baseRequest) SetStringToSign(stringToSign string) {
|
|||||||
request.stringToSign = stringToSign
|
request.stringToSign = stringToSign
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (request *baseRequest) addHeaderParam(key, val string) {
|
||||||
|
request.Headers[key] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (request *baseRequest) addQueryParam(key, val string) {
|
||||||
|
request.QueryParams[key] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (request *baseRequest) addFormParam(key, val string) {
|
||||||
|
request.FormParams[key] = val
|
||||||
|
}
|
||||||
|
|
||||||
func defaultBaseRequest() (request *baseRequest) {
|
func defaultBaseRequest() (request *baseRequest) {
|
||||||
request = &baseRequest{
|
request = &baseRequest{
|
||||||
Scheme: "",
|
Scheme: "",
|
||||||
|
@ -73,3 +73,7 @@ func NewCommonResponse() (response *CommonResponse) {
|
|||||||
BaseResponse: &BaseResponse{},
|
BaseResponse: &BaseResponse{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Unmarshal(response AcsResponse, httpResponse *http.Response, format string) {
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user