roa_request.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. func (request *RoaRequest) GetQueries() string {
  42. return request.queries
  43. }
  44. // for sign method, need not url encoded
  45. func (request *RoaRequest) BuildQueries() string {
  46. return request.buildQueries(false)
  47. }
  48. func (request *RoaRequest) buildQueries(needParamEncode bool) string {
  49. // replace path params with value
  50. path := request.pathPattern
  51. for key, value := range request.PathParams {
  52. path = strings.Replace(path, "["+key+"]", value, 1)
  53. }
  54. queryParams := request.QueryParams
  55. // check if path contains params
  56. splitArray := strings.Split(path, "?")
  57. path = splitArray[0]
  58. if len(splitArray) > 1 && len(splitArray[1]) > 0 {
  59. queryParams[splitArray[1]] = ""
  60. }
  61. // sort QueryParams by key
  62. var queryKeys []string
  63. for key := range queryParams {
  64. queryKeys = append(queryKeys, key)
  65. }
  66. sort.Strings(queryKeys)
  67. // append urlBuilder
  68. urlBuilder := bytes.Buffer{}
  69. urlBuilder.WriteString(path)
  70. if len(queryKeys) > 0 {
  71. urlBuilder.WriteString("?")
  72. }
  73. for i := 0; i < len(queryKeys); i++ {
  74. queryKey := queryKeys[i]
  75. urlBuilder.WriteString(queryKey)
  76. if value := queryParams[queryKey]; len(value) > 0 {
  77. urlBuilder.WriteString("=")
  78. if needParamEncode {
  79. urlBuilder.WriteString(url.QueryEscape(value))
  80. } else {
  81. urlBuilder.WriteString(value)
  82. }
  83. }
  84. if i < len(queryKeys)-1 {
  85. urlBuilder.WriteString("&")
  86. }
  87. }
  88. result := urlBuilder.String()
  89. result = popStandardUrlencode(result)
  90. request.queries = result
  91. return request.queries
  92. }
  93. func popStandardUrlencode(stringToSign string) (result string) {
  94. result = strings.Replace(stringToSign, "+", "%20", -1)
  95. result = strings.Replace(result, "*", "%2A", -1)
  96. result = strings.Replace(result, "%7E", "~", -1)
  97. return
  98. }
  99. func (request *RoaRequest) GetUrl() string {
  100. return strings.ToLower(request.Scheme) + "://" + request.Domain + ":" + request.Port + request.GetQueries()
  101. }
  102. func (request *RoaRequest) BuildUrl() string {
  103. // for network trans, need url encoded
  104. return strings.ToLower(request.Scheme) + "://" + request.Domain + ":" + request.Port + request.buildQueries(true)
  105. }
  106. func (request *RoaRequest) addPathParam(key, value string) {
  107. request.PathParams[key] = value
  108. }
  109. func (request *RoaRequest) InitWithApiInfo(product, version, action, uriPattern, serviceCode, endpointType string) {
  110. request.baseRequest = defaultBaseRequest()
  111. request.PathParams = make(map[string]string)
  112. request.Headers["x-acs-version"] = version
  113. request.pathPattern = uriPattern
  114. request.locationServiceCode = serviceCode
  115. request.locationEndpointType = endpointType
  116. //request.product = product
  117. //request.version = version
  118. //request.actionName = action
  119. }
  120. func (request *RoaRequest) initWithCommonRequest(commonRequest *CommonRequest) {
  121. request.baseRequest = commonRequest.baseRequest
  122. request.PathParams = commonRequest.PathParams
  123. //request.product = commonRequest.Product
  124. //request.version = commonRequest.Version
  125. request.Headers["x-acs-version"] = commonRequest.Version
  126. //request.actionName = commonRequest.ApiName
  127. request.pathPattern = commonRequest.PathPattern
  128. request.locationServiceCode = ""
  129. request.locationEndpointType = ""
  130. }