Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 185150a903 | |||
| 2b0e131d00 | |||
| e3922d2afb | |||
| 5ef2fcc54f | |||
| 121efa4413 | |||
| aa99f50b3f | |||
| cb3a42bc23 | |||
| 712238a5c4 | |||
| 07ca3b69ad | |||
| e158cc5dc5 | |||
| b46b464236 | |||
| 636d89faa7 | |||
| c4f78703d7 | |||
| 8a7c8512f8 |
@@ -0,0 +1,11 @@
|
||||
# To get started with Dependabot version updates, you'll need to specify which
|
||||
# package ecosystems to update and where the package manifests are located.
|
||||
# Please see the documentation for all configuration options:
|
||||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "gomod" # See documentation for possible values
|
||||
directory: "/" # Location of package manifests
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
@@ -16,7 +16,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v2
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
|
||||
@@ -32,4 +32,4 @@ jobs:
|
||||
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./tea/... ./utils/...
|
||||
|
||||
- name: CodeCov
|
||||
run: bash <(curl -s https://codecov.io/bash)
|
||||
run: bash <(curl -s https://codecov.io/bash)
|
||||
|
||||
+3
-3
@@ -2,11 +2,11 @@
|
||||
|
||||
<a href="https://badge.fury.io/gh/alibabacloud-go%2Ftea"><img src="https://badge.fury.io/gh/alibabacloud-go%2Ftea.svg" alt="Latest Stable Version"></a>
|
||||
<a href="https://codecov.io/gh/alibabacloud-go/tea"><img src="https://codecov.io/gh/alibabacloud-go/tea/branch/master/graph/badge.svg" alt="codecov"></a>
|
||||
<a href="https://travis-ci.org/alibabacloud-go/tea"><img src="https://travis-ci.org/alibabacloud-go/tea.svg?branch=master" alt="Travis Build Status"></a>
|
||||
[](https://github.com/alibabacloud-go/tea/actions/workflows/go.yml)
|
||||
|
||||
该项目用于支持TEA OpenAPI DSL。它是http请求的底层库.
|
||||
该项目用于支持 Darabonba OpenAPI DSL。它是http请求的底层库.
|
||||
|
||||
## 许可证
|
||||
[Apache-2.0](/LICENSE)
|
||||
|
||||
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
|
||||
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
<a href="https://badge.fury.io/gh/alibabacloud-go%2Ftea"><img src="https://badge.fury.io/gh/alibabacloud-go%2Ftea.svg" alt="Latest Stable Version"></a>
|
||||
<a href="https://codecov.io/gh/alibabacloud-go/tea"><img src="https://codecov.io/gh/alibabacloud-go/tea/branch/master/graph/badge.svg" alt="codecov"></a>
|
||||
<a href="https://travis-ci.org/alibabacloud-go/tea"><img src="https://travis-ci.org/alibabacloud-go/tea.svg?branch=master" alt="Travis Build Status"></a>
|
||||
[](https://github.com/alibabacloud-go/tea/actions/workflows/go.yml)
|
||||
|
||||
This project is used for support TEA OpenAPI DSL. It's a low-level library for http request.
|
||||
This project is used for support Darabonba OpenAPI DSL. It's a low-level library for http request.
|
||||
|
||||
## License
|
||||
[Apache-2.0](/LICENSE)
|
||||
|
||||
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
|
||||
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
|
||||
|
||||
@@ -4,7 +4,8 @@ go 1.14
|
||||
|
||||
require (
|
||||
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68
|
||||
github.com/json-iterator/go v1.1.10
|
||||
github.com/modern-go/reflect2 v1.0.1
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/modern-go/reflect2 v1.0.2
|
||||
golang.org/x/net v0.11.0
|
||||
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b
|
||||
)
|
||||
|
||||
+62
-23
@@ -26,6 +26,8 @@ import (
|
||||
"github.com/alibabacloud-go/debug/debug"
|
||||
"github.com/alibabacloud-go/tea/utils"
|
||||
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
@@ -97,6 +99,8 @@ type RuntimeObject struct {
|
||||
Listener utils.ProgressListener `json:"listener" xml:"listener"`
|
||||
Tracker *utils.ReaderTracker `json:"tracker" xml:"tracker"`
|
||||
Logger *utils.Logger `json:"logger" xml:"logger"`
|
||||
Span *opentracing.Span `json:"span" xml:"span"`
|
||||
IsCloseTrace *bool `json:"isCloseTrace" xml:"isCloseTrace"`
|
||||
}
|
||||
|
||||
type teaClient struct {
|
||||
@@ -133,6 +137,7 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
|
||||
Key: TransInterfaceToString(runtime["key"]),
|
||||
Cert: TransInterfaceToString(runtime["cert"]),
|
||||
CA: TransInterfaceToString(runtime["ca"]),
|
||||
IsCloseTrace: TransInterfaceToBool(runtime["isCloseTrace"]),
|
||||
}
|
||||
if runtime["listener"] != nil {
|
||||
runtimeObject.Listener = runtime["listener"].(utils.ProgressListener)
|
||||
@@ -143,6 +148,9 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
|
||||
if runtime["logger"] != nil {
|
||||
runtimeObject.Logger = runtime["logger"].(*utils.Logger)
|
||||
}
|
||||
if runtime["span"] != nil {
|
||||
runtimeObject.Span = runtime["span"].(*opentracing.Span)
|
||||
}
|
||||
return runtimeObject
|
||||
}
|
||||
|
||||
@@ -262,7 +270,7 @@ func Convert(in interface{}, out interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Convert is use convert map[string]interface object to struct
|
||||
// Recover is used to format error
|
||||
func Recover(in interface{}) error {
|
||||
if in == nil {
|
||||
return nil
|
||||
@@ -378,6 +386,31 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
event := utils.NewProgressEvent(utils.TransferStartedEvent, 0, int64(contentlength), 0)
|
||||
utils.PublishProgress(runtimeObject.Listener, event)
|
||||
|
||||
// Set tracer
|
||||
var span opentracing.Span
|
||||
if ok := opentracing.IsGlobalTracerRegistered(); ok && BoolValue(runtimeObject.IsCloseTrace) != true {
|
||||
tracer := opentracing.GlobalTracer()
|
||||
var rootCtx opentracing.SpanContext
|
||||
var rootSpan opentracing.Span
|
||||
|
||||
if runtimeObject.Span != nil {
|
||||
rootSpan = *runtimeObject.Span
|
||||
rootCtx = rootSpan.Context()
|
||||
}
|
||||
|
||||
span = tracer.StartSpan(
|
||||
httpRequest.URL.RequestURI(),
|
||||
opentracing.ChildOf(rootCtx),
|
||||
opentracing.Tag{Key: string(ext.Component), Value: "aliyunApi"},
|
||||
opentracing.Tag{Key: "request", Value: requestURL})
|
||||
|
||||
defer span.Finish()
|
||||
tracer.Inject(
|
||||
span.Context(),
|
||||
opentracing.HTTPHeaders,
|
||||
opentracing.HTTPHeadersCarrier(httpRequest.Header))
|
||||
}
|
||||
|
||||
putMsgToMap(fieldMap, httpRequest)
|
||||
startTime := time.Now()
|
||||
fieldMap["{start_time}"] = startTime.Format("2006-01-02 15:04:05")
|
||||
@@ -415,28 +448,30 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.ToLower(*req.Protocol) == "https" &&
|
||||
runtime.Key != nil && runtime.Cert != nil {
|
||||
cert, err := tls.X509KeyPair([]byte(StringValue(runtime.Cert)), []byte(StringValue(runtime.Key)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
trans.TLSClientConfig = &tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
InsecureSkipVerify: BoolValue(runtime.IgnoreSSL),
|
||||
}
|
||||
if runtime.CA != nil {
|
||||
clientCertPool := x509.NewCertPool()
|
||||
ok := clientCertPool.AppendCertsFromPEM([]byte(StringValue(runtime.CA)))
|
||||
if !ok {
|
||||
return nil, errors.New("Failed to parse root certificate")
|
||||
if strings.ToLower(*req.Protocol) == "https" {
|
||||
if BoolValue(runtime.IgnoreSSL) != true {
|
||||
trans.TLSClientConfig = &tls.Config{
|
||||
InsecureSkipVerify: false,
|
||||
}
|
||||
if runtime.Key != nil && runtime.Cert != nil && StringValue(runtime.Key) != "" && StringValue(runtime.Cert) != "" {
|
||||
cert, err := tls.X509KeyPair([]byte(StringValue(runtime.Cert)), []byte(StringValue(runtime.Key)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
trans.TLSClientConfig.Certificates = []tls.Certificate{cert}
|
||||
}
|
||||
if runtime.CA != nil && StringValue(runtime.CA) != "" {
|
||||
clientCertPool := x509.NewCertPool()
|
||||
ok := clientCertPool.AppendCertsFromPEM([]byte(StringValue(runtime.CA)))
|
||||
if !ok {
|
||||
return nil, errors.New("Failed to parse root certificate")
|
||||
}
|
||||
trans.TLSClientConfig.RootCAs = clientCertPool
|
||||
}
|
||||
} else {
|
||||
trans.TLSClientConfig = &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
trans.TLSClientConfig.RootCAs = clientCertPool
|
||||
}
|
||||
} else {
|
||||
trans.TLSClientConfig = &tls.Config{
|
||||
InsecureSkipVerify: BoolValue(runtime.IgnoreSSL),
|
||||
}
|
||||
}
|
||||
if httpProxy != nil {
|
||||
@@ -476,6 +511,10 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
} else {
|
||||
trans.DialContext = setDialContext(runtime)
|
||||
}
|
||||
if runtime.MaxIdleConns != nil && *runtime.MaxIdleConns > 0 {
|
||||
trans.MaxIdleConns = IntValue(runtime.MaxIdleConns)
|
||||
trans.MaxIdleConnsPerHost = IntValue(runtime.MaxIdleConns)
|
||||
}
|
||||
return trans, nil
|
||||
}
|
||||
|
||||
@@ -978,7 +1017,7 @@ func validatePtr(field reflect.StructField, elementValue reflect.Value, contains
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} else if elementValue.Elem().Type().Kind() == reflect.Struct {
|
||||
err := validate(elementValue)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+40
-14
@@ -16,6 +16,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/alibabacloud-go/tea/utils"
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
)
|
||||
|
||||
type test struct {
|
||||
@@ -47,18 +49,19 @@ var runtimeObj = map[string]interface{}{
|
||||
}
|
||||
|
||||
type validateTest struct {
|
||||
Num1 *int `json:"num1,omitempty" require:"true" minimum:"2"`
|
||||
Num2 *int `json:"num2,omitempty" maximum:"6"`
|
||||
Name1 *string `json:"name1,omitempty" maxLength:"4"`
|
||||
Name2 *string `json:"name2,omitempty" minLength:"2"`
|
||||
Str *string `json:"str,omitempty" pattern:"[a-d]*" maxLength:"4"`
|
||||
MaxLength *errMaxLength `json:"MaxLength,omitempty"`
|
||||
MinLength *errMinLength `json:"MinLength,omitempty"`
|
||||
Maximum *errMaximum `json:"Maximum,omitempty"`
|
||||
Minimum *errMinimum `json:"Minimum,omitempty"`
|
||||
MaxItems *errMaxItems `json:"MaxItems,omitempty"`
|
||||
MinItems *errMinItems `json:"MinItems,omitempty"`
|
||||
List []*string `json:"list,omitempty" pattern:"[a-d]*" minItems:"2" maxItems:"3" maxLength:"4"`
|
||||
Num1 *int `json:"num1,omitempty" require:"true" minimum:"2"`
|
||||
Num2 *int `json:"num2,omitempty" maximum:"6"`
|
||||
Name1 *string `json:"name1,omitempty" maxLength:"4"`
|
||||
Name2 *string `json:"name2,omitempty" minLength:"2"`
|
||||
Str *string `json:"str,omitempty" pattern:"[a-d]*" maxLength:"4"`
|
||||
MaxLength *errMaxLength `json:"MaxLength,omitempty"`
|
||||
MinLength *errMinLength `json:"MinLength,omitempty"`
|
||||
Maximum *errMaximum `json:"Maximum,omitempty"`
|
||||
Minimum *errMinimum `json:"Minimum,omitempty"`
|
||||
MaxItems *errMaxItems `json:"MaxItems,omitempty"`
|
||||
MinItems *errMinItems `json:"MinItems,omitempty"`
|
||||
List []*string `json:"list,omitempty" pattern:"[a-d]*" minItems:"2" maxItems:"3" maxLength:"4"`
|
||||
PtrNotStruct *opentracing.Span `json:"span" xml:"span"`
|
||||
}
|
||||
|
||||
type errMaxLength struct {
|
||||
@@ -542,20 +545,32 @@ func Test_DoRequest(t *testing.T) {
|
||||
|
||||
runtimeObj["key"] = "private rsa key"
|
||||
runtimeObj["cert"] = "private certification"
|
||||
runtimeObj["ca"] = "private ca"
|
||||
runtimeObj["ignoreSSL"] = true
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertNotNil(t, resp)
|
||||
|
||||
// update the host is to restart a client
|
||||
request.Headers["host"] = String("a.com")
|
||||
runtimeObj["ignoreSSL"] = false
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNotNil(t, err)
|
||||
utils.AssertEqual(t, "tls: failed to find any PEM data in certificate input", err.Error())
|
||||
utils.AssertNil(t, resp)
|
||||
|
||||
// update the host is to restart a client
|
||||
request.Headers["host"] = String("b.com")
|
||||
runtimeObj["key"] = key
|
||||
runtimeObj["cert"] = cert
|
||||
runtimeObj["ca"] = "private ca"
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@cs.aliyun.com"
|
||||
_, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNotNil(t, err)
|
||||
utils.AssertEqual(t, "Failed to parse root certificate", err.Error())
|
||||
|
||||
// update the host is to restart a client
|
||||
request.Headers["host"] = String("c.com")
|
||||
runtimeObj["ca"] = ca
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@cs.aliyuncs.com"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
@@ -736,6 +751,17 @@ func Test_Validate(t *testing.T) {
|
||||
err := Validate(config)
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
tracer := opentracing.GlobalTracer()
|
||||
span := tracer.StartSpan(
|
||||
"aliyuncs.com/test",
|
||||
opentracing.Tag{Key: string(ext.Component), Value: "aliyunApi"},
|
||||
opentracing.Tag{Key: "request", Value: "test"})
|
||||
config = &validateTest{
|
||||
PtrNotStruct: &span,
|
||||
}
|
||||
err = Validate(config)
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
err = Validate(new(validateTest))
|
||||
utils.AssertEqual(t, err.Error(), "num1 should be setted")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user