roa_request.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package requests
  15. import (
  16. "bytes"
  17. "io"
  18. "net/url"
  19. "sort"
  20. "strings"
  21. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
  22. )
  23. type RoaRequest struct {
  24. *baseRequest
  25. pathPattern string
  26. PathParams map[string]string
  27. }
  28. func (*RoaRequest) GetStyle() string {
  29. return ROA
  30. }
  31. func (request *RoaRequest) GetBodyReader() io.Reader {
  32. if request.FormParams != nil && len(request.FormParams) > 0 {
  33. formString := utils.GetUrlFormedMap(request.FormParams)
  34. return strings.NewReader(formString)
  35. } else if len(request.Content) > 0 {
  36. return bytes.NewReader(request.Content)
  37. } else {
  38. return nil
  39. }
  40. }
  41. // for sign method, need not url encoded
  42. func (request *RoaRequest) BuildQueries() string {
  43. return request.buildQueries(false)
  44. }
  45. func (request *RoaRequest) buildQueries(needParamEncode bool) string {
  46. // replace path params with value
  47. path := request.pathPattern
  48. for key, value := range request.PathParams {
  49. path = strings.Replace(path, "["+key+"]", value, 1)
  50. }
  51. queryParams := request.QueryParams
  52. // check if path contains params
  53. // splitArray := strings.Split(path, "?")
  54. // path = splitArray[0]
  55. // if len(splitArray) > 1 && len(splitArray[1]) > 0 {
  56. // queryParams[splitArray[1]] = ""
  57. // }
  58. // sort QueryParams by key
  59. var queryKeys []string
  60. for key := range queryParams {
  61. queryKeys = append(queryKeys, key)
  62. }
  63. sort.Strings(queryKeys)
  64. // append urlBuilder
  65. urlBuilder := bytes.Buffer{}
  66. urlBuilder.WriteString(path)
  67. if len(queryKeys) > 0 {
  68. urlBuilder.WriteString("?")
  69. }
  70. for i := 0; i < len(queryKeys); i++ {
  71. queryKey := queryKeys[i]
  72. urlBuilder.WriteString(queryKey)
  73. if value := queryParams[queryKey]; len(value) > 0 {
  74. urlBuilder.WriteString("=")
  75. if needParamEncode {
  76. urlBuilder.WriteString(url.QueryEscape(value))
  77. } else {
  78. urlBuilder.WriteString(value)
  79. }
  80. }
  81. if i < len(queryKeys)-1 {
  82. urlBuilder.WriteString("&")
  83. }
  84. }
  85. result := urlBuilder.String()
  86. result = popStandardUrlencode(result)
  87. request.queries = result
  88. return request.queries
  89. }
  90. func popStandardUrlencode(stringToSign string) (result string) {
  91. result = strings.Replace(stringToSign, "+", "%20", -1)
  92. result = strings.Replace(result, "*", "%2A", -1)
  93. result = strings.Replace(result, "%7E", "~", -1)
  94. return
  95. }
  96. func (request *RoaRequest) BuildUrl() string {
  97. // for network trans, need url encoded
  98. return strings.ToLower(request.Scheme) + "://" + request.Domain + ":" + request.Port + request.buildQueries(true)
  99. }
  100. func (request *RoaRequest) addPathParam(key, value string) {
  101. request.PathParams[key] = value
  102. }
  103. func (request *RoaRequest) InitWithApiInfo(product, version, action, uriPattern, serviceCode, endpointType string) {
  104. request.baseRequest = defaultBaseRequest()
  105. request.PathParams = make(map[string]string)
  106. request.Headers["x-acs-version"] = version
  107. request.pathPattern = uriPattern
  108. request.locationServiceCode = serviceCode
  109. request.locationEndpointType = endpointType
  110. //request.product = product
  111. //request.version = version
  112. //request.actionName = action
  113. }
  114. func (request *RoaRequest) initWithCommonRequest(commonRequest *CommonRequest) {
  115. request.baseRequest = commonRequest.baseRequest
  116. request.PathParams = commonRequest.PathParams
  117. //request.product = commonRequest.Product
  118. //request.version = commonRequest.Version
  119. request.Headers["x-acs-version"] = commonRequest.Version
  120. //request.actionName = commonRequest.ApiName
  121. request.pathPattern = commonRequest.PathPattern
  122. request.locationServiceCode = ""
  123. request.locationEndpointType = ""
  124. }