roa_request.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "fmt"
  18. "io"
  19. "net/url"
  20. "sort"
  21. "strings"
  22. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
  23. )
  24. type RoaRequest struct {
  25. *baseRequest
  26. pathPattern string
  27. PathParams map[string]string
  28. }
  29. func (*RoaRequest) GetStyle() string {
  30. return ROA
  31. }
  32. func (request *RoaRequest) GetBodyReader() io.Reader {
  33. if request.FormParams != nil && len(request.FormParams) > 0 {
  34. formString := utils.GetUrlFormedMap(request.FormParams)
  35. return strings.NewReader(formString)
  36. } else if len(request.Content) > 0 {
  37. return bytes.NewReader(request.Content)
  38. } else {
  39. return nil
  40. }
  41. }
  42. // for sign method, need not url encoded
  43. func (request *RoaRequest) BuildQueries() string {
  44. return request.buildQueries()
  45. }
  46. func (request *RoaRequest) buildPath() string {
  47. path := request.pathPattern
  48. for key, value := range request.PathParams {
  49. path = strings.Replace(path, "["+key+"]", value, 1)
  50. }
  51. return path
  52. }
  53. func (request *RoaRequest) buildQueries() string {
  54. // replace path params with value
  55. path := request.buildPath()
  56. queryParams := request.QueryParams
  57. // sort QueryParams by key
  58. var queryKeys []string
  59. for key := range queryParams {
  60. queryKeys = append(queryKeys, key)
  61. }
  62. sort.Strings(queryKeys)
  63. // append urlBuilder
  64. urlBuilder := bytes.Buffer{}
  65. urlBuilder.WriteString(path)
  66. if len(queryKeys) > 0 {
  67. urlBuilder.WriteString("?")
  68. }
  69. for i := 0; i < len(queryKeys); i++ {
  70. queryKey := queryKeys[i]
  71. urlBuilder.WriteString(queryKey)
  72. if value := queryParams[queryKey]; len(value) > 0 {
  73. urlBuilder.WriteString("=")
  74. urlBuilder.WriteString(value)
  75. }
  76. if i < len(queryKeys)-1 {
  77. urlBuilder.WriteString("&")
  78. }
  79. }
  80. result := urlBuilder.String()
  81. result = popStandardUrlencode(result)
  82. return result
  83. }
  84. func (request *RoaRequest) buildQueryString() string {
  85. queryParams := request.QueryParams
  86. // sort QueryParams by key
  87. q := url.Values{}
  88. for key, value := range queryParams {
  89. q.Add(key, value)
  90. }
  91. return q.Encode()
  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) BuildUrl() string {
  100. // for network trans, need url encoded
  101. scheme := strings.ToLower(request.Scheme)
  102. domain := request.Domain
  103. port := request.Port
  104. path := request.buildPath()
  105. url := fmt.Sprintf("%s://%s:%s%s", scheme, domain, port, path)
  106. querystring := request.buildQueryString()
  107. if len(querystring) > 0 {
  108. url = fmt.Sprintf("%s?%s", url, querystring)
  109. }
  110. return url
  111. }
  112. func (request *RoaRequest) addPathParam(key, value string) {
  113. request.PathParams[key] = value
  114. }
  115. func (request *RoaRequest) InitWithApiInfo(product, version, action, uriPattern, serviceCode, endpointType string) {
  116. request.baseRequest = defaultBaseRequest()
  117. request.PathParams = make(map[string]string)
  118. request.Headers["x-acs-version"] = version
  119. request.pathPattern = uriPattern
  120. request.locationServiceCode = serviceCode
  121. request.locationEndpointType = endpointType
  122. request.product = product
  123. //request.version = version
  124. request.actionName = action
  125. }
  126. func (request *RoaRequest) initWithCommonRequest(commonRequest *CommonRequest) {
  127. request.baseRequest = commonRequest.baseRequest
  128. request.PathParams = commonRequest.PathParams
  129. request.product = commonRequest.Product
  130. //request.version = commonRequest.Version
  131. request.Headers["x-acs-version"] = commonRequest.Version
  132. request.actionName = commonRequest.ApiName
  133. request.pathPattern = commonRequest.PathPattern
  134. request.locationServiceCode = commonRequest.ServiceCode
  135. request.locationEndpointType = ""
  136. }