Skip to content

Commit

Permalink
feat: slotdata
Browse files Browse the repository at this point in the history
  • Loading branch information
xfslove committed Jun 16, 2023
1 parent 9799deb commit b19c5fb
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 120 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
## Installation
If you use `go mod` to manage your dependence, you can use the following command:
```
go get github.com/aliyun/alibabacloud-yjopenapi-go-client 1.0.20230602
go get github.com/aliyun/alibabacloud-yjopenapi-go-client 1.0.20230616
```

## Usage
Expand Down
239 changes: 120 additions & 119 deletions client/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,35 @@
package api

import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"

)

const (
Trace_Id string = "Traceid"
Result_Status string = "Result-Status"
Trace_Id string = "Traceid"
Result_Status string = "Result-Status"
)

var (
jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")

DefaultConfiguration = NewConfiguration()
DefaultConfiguration = NewConfiguration()
)

// APIClient manages communication with the API
Expand Down Expand Up @@ -141,34 +142,34 @@ func typeCheckParameter(obj interface{}, expected string, name string) error {

// parameterToString convert interface{} parameters to string, using a delimiter if format is provided.
func parameterToString(obj interface{}, collectionFormat string) string {
// var delimiter string

// switch collectionFormat {
// case "pipes":
// delimiter = "|"
// case "ssv":
// delimiter = " "
// case "tsv":
// delimiter = "\t"
// case "csv":
// delimiter = ","
// }
// var delimiter string

// switch collectionFormat {
// case "pipes":
// delimiter = "|"
// case "ssv":
// delimiter = " "
// case "tsv":
// delimiter = "\t"
// case "csv":
// delimiter = ","
// }

if reflect.TypeOf(obj).Kind() == reflect.Slice {
// return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
if marshal, err := json.Marshal(obj); err != nil {
return ""
} else {
return string(marshal)
}
}
if reflect.TypeOf(obj).Kind() == reflect.Struct {
if marshal, err := json.Marshal(obj); err != nil {
return ""
} else {
return string(marshal)
}
// return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
if marshal, err := json.Marshal(obj); err != nil {
return ""
} else {
return string(marshal)
}
}
if reflect.TypeOf(obj).Kind() == reflect.Struct {
if marshal, err := json.Marshal(obj); err != nil {
return ""
} else {
return string(marshal)
}
}
return fmt.Sprintf("%v", obj)
}

Expand All @@ -180,7 +181,7 @@ func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
// prepareRequest build the request
func (c *APIClient) prepareRequest(
path string,
method string,
method string,
headerParams map[string]string,
queryParams url.Values,
formParams url.Values,
Expand Down Expand Up @@ -240,8 +241,8 @@ func (c *APIClient) prepareRequest(
// Add the user agent to the request.
localVarRequest.Header.Add("User-Agent", "cgw-client/1.0.0/go")

// prepare sign headers
prepareSignHeader(queryParams, formParams, localVarRequest, c.cfg)
// prepare sign headers
prepareSignHeader(queryParams, formParams, localVarRequest, c.cfg)

for header, value := range c.cfg.DefaultHeader {
localVarRequest.Header.Add(header, value)
Expand All @@ -252,11 +253,11 @@ func (c *APIClient) prepareRequest(

func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
if strings.Contains(contentType, "application/json") {
if err = json.Unmarshal(b, v); err != nil {
return err
}
return nil
}
if err = json.Unmarshal(b, v); err != nil {
return err
}
return nil
}
return errors.New("undefined response type")
}

Expand Down Expand Up @@ -319,79 +320,79 @@ func strlen(s string) int {
}

func prepareSignHeader(queryParams url.Values, formParams url.Values, localVarRequest *http.Request, cfg *Configuration) {
accessKey := cfg.AccessKey
nonce := randNonce(16)
version := cfg.SignatureVersion
timestamp := time.Now().UTC().Format(time.RFC3339)
method := cfg.SignatureMethod

values := url.Values{}
values.Add("AccessKey", accessKey)
values.Add("SignatureNonce", nonce)
values.Add("SignatureVersion", version)
values.Add("SignatureMethod", method)
values.Add("Timestamp", timestamp)
if len(queryParams) > 0 {
for s, i := range queryParams {
for _, s2 := range i {
values.Add(s, s2)
}
}
}
if len(formParams) > 0 {
for s, i := range formParams {
for _, s2 := range i {
values.Add(s, s2)
}
}
}

sign := md5Hex([]byte(getSignRaw(values, localVarRequest.Method, cfg.SecretKey)))
localVarRequest.Header.Add("Signature", sign)
localVarRequest.Header.Add("Accesskey", accessKey)
localVarRequest.Header.Add("Signaturenonce", nonce)
localVarRequest.Header.Add("Signatureversion", version)
localVarRequest.Header.Add("Timestamp", timestamp)
localVarRequest.Header.Add("Signaturemethod", method)
accessKey := cfg.AccessKey
nonce := randNonce(16)
version := cfg.SignatureVersion
timestamp := time.Now().UTC().Format(time.RFC3339)
method := cfg.SignatureMethod

values := url.Values{}
values.Add("AccessKey", accessKey)
values.Add("SignatureNonce", nonce)
values.Add("SignatureVersion", version)
values.Add("SignatureMethod", method)
values.Add("Timestamp", timestamp)
if len(queryParams) > 0 {
for s, i := range queryParams {
for _, s2 := range i {
values.Add(s, s2)
}
}
}
if len(formParams) > 0 {
for s, i := range formParams {
for _, s2 := range i {
values.Add(s, s2)
}
}
}

sign := md5Hex([]byte(getSignRaw(values, localVarRequest.Method, cfg.SecretKey)))
localVarRequest.Header.Add("Signature", sign)
localVarRequest.Header.Add("Accesskey", accessKey)
localVarRequest.Header.Add("Signaturenonce", nonce)
localVarRequest.Header.Add("Signatureversion", version)
localVarRequest.Header.Add("Timestamp", timestamp)
localVarRequest.Header.Add("Signaturemethod", method)
}

func md5Hex(data []byte) string {
md5Sum := md5.Sum(data)
return hex.EncodeToString(md5Sum[:])
md5Sum := md5.Sum(data)
return hex.EncodeToString(md5Sum[:])
}

func getSignRaw(values url.Values, method string, secret string) string {
return secret + "&" + keySignInput(values, method)
return secret + "&" + keySignInput(values, method)
}

func keySignInput(values url.Values, method string) string {
keys := make([]string, 0)
for s, _ := range values {
keys = append(keys, s)
}
sort.Strings(keys)

stringToSign := method + "&" + encodeURLComponent("/")

strs := make([]string, 0)
for _, k := range keys {
strs = append(strs, k+"="+encodeURLComponent(values.Get(k)))
}
return stringToSign + "&" + encodeURLComponent(strings.Join(strs, "&"))
keys := make([]string, 0)
for s, _ := range values {
keys = append(keys, s)
}
sort.Strings(keys)

stringToSign := method + "&" + encodeURLComponent("/")

strs := make([]string, 0)
for _, k := range keys {
strs = append(strs, k+"="+encodeURLComponent(values.Get(k)))
}
return stringToSign + "&" + encodeURLComponent(strings.Join(strs, "&"))
}

func encodeURLComponent(str string) string {
escape := url.QueryEscape(str)
escape = strings.ReplaceAll(escape, "+", "%20")
escape = strings.ReplaceAll(escape, "*", "%2A")
escape = strings.ReplaceAll(escape, "%7E", "~")
return escape
escape := url.QueryEscape(str)
escape = strings.ReplaceAll(escape, "+", "%20")
escape = strings.ReplaceAll(escape, "*", "%2A")
escape = strings.ReplaceAll(escape, "%7E", "~")
return escape
}

func randNonce(n int) string {
b := make([]byte, n)
rand.Read(b)
return hex.EncodeToString(b)
b := make([]byte, n)
rand.Read(b)
return hex.EncodeToString(b)
}

// GenericError Provides access to the body, error and model on returned errors.
Expand All @@ -417,14 +418,14 @@ func (e GenericError) Model() interface{} {
}

type Configuration struct {
Host string `json:"host"`
Scheme string `json:"scheme"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
SignatureVersion string `json:"signatureVersion"`
SignatureMethod string `json:"signatureMethod"`
HTTPClient *http.Client
Host string `json:"host"`
Scheme string `json:"scheme"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
SignatureVersion string `json:"signatureVersion"`
SignatureMethod string `json:"signatureMethod"`
HTTPClient *http.Client
}

func NewConfiguration() *Configuration {
Expand All @@ -434,4 +435,4 @@ func NewConfiguration() *Configuration {
SignatureMethod: "MD5",
HTTPClient: http.DefaultClient,
}
}
}
1 change: 1 addition & 0 deletions client/model/trytogetslotresultmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ type TryToGetSlotResultModel struct {
Message string `json:"message,omitempty"`
// 调度执行结果
Success bool `json:"success,omitempty"`
SlotData string `json:"slotData,omitempty"`
}

0 comments on commit b19c5fb

Please sign in to comment.