roa_request.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. return result
  82. }
  83. func (request *RoaRequest) buildQueryString() string {
  84. queryParams := request.QueryParams
  85. // sort QueryParams by key
  86. q := url.Values{}
  87. for key, value := range queryParams {
  88. q.Add(key, value)
  89. }
  90. return q.Encode()
  91. }
  92. func (request *RoaRequest) BuildUrl() string {
  93. // for network trans, need url encoded
  94. scheme := strings.ToLower(request.Scheme)
  95. domain := request.Domain
  96. port := request.Port
  97. path := request.buildPath()
  98. url := fmt.Sprintf("%s://%s:%s%s", scheme, domain, port, path)
  99. querystring := request.buildQueryString()
  100. if len(querystring) > 0 {
  101. url = fmt.Sprintf("%s?%s", url, querystring)
  102. }
  103. return url
  104. }
  105. func (request *RoaRequest) addPathParam(key, value string) {
  106. request.PathParams[key] = value
  107. }
  108. func (request *RoaRequest) InitWithApiInfo(product, version, action, uriPattern, serviceCode, endpointType string) {
  109. request.baseRequest = defaultBaseRequest()
  110. request.PathParams = make(map[string]string)
  111. request.Headers["x-acs-version"] = version
  112. request.Headers["x-acs-action"] = action
  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. if commonRequest.ApiName != "" {
  127. request.Headers["x-acs-action"] = commonRequest.ApiName
  128. }
  129. request.actionName = commonRequest.ApiName
  130. request.pathPattern = commonRequest.PathPattern
  131. request.locationServiceCode = commonRequest.ServiceCode
  132. request.locationEndpointType = ""
  133. }