roa_request.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
  18. "io"
  19. "sort"
  20. "strings"
  21. )
  22. type RoaRequest struct {
  23. *baseRequest
  24. pathPattern string
  25. PathParams map[string]string
  26. }
  27. func (*RoaRequest) GetStyle() string {
  28. return ROA
  29. }
  30. func (request *RoaRequest) GetBodyReader() io.Reader {
  31. if request.FormParams != nil && len(request.FormParams) > 0 {
  32. formString := utils.GetUrlFormedMap(request.FormParams)
  33. return strings.NewReader(formString)
  34. } else if len(request.Content) > 0 {
  35. return bytes.NewReader(request.Content)
  36. } else {
  37. return nil
  38. }
  39. }
  40. func (request *RoaRequest) GetQueries() string {
  41. if len(request.queries) == 0 {
  42. request.buildQueries()
  43. }
  44. return request.queries
  45. }
  46. func (request *RoaRequest) buildQueries() {
  47. // replace path params with value
  48. path := request.pathPattern
  49. for key, value := range request.PathParams {
  50. path = strings.Replace(path, "["+key+"]", value, 1)
  51. }
  52. queryParams := request.QueryParams
  53. // check if path contains params
  54. splitArray := strings.Split(path, "?")
  55. path = splitArray[0]
  56. if len(splitArray) > 1 && len(splitArray[1]) > 0 {
  57. queryParams[splitArray[1]] = ""
  58. }
  59. // sort QueryParams by key
  60. var queryKeys []string
  61. for key := range queryParams {
  62. queryKeys = append(queryKeys, key)
  63. }
  64. sort.Strings(queryKeys)
  65. // append urlBuilder
  66. urlBuilder := bytes.Buffer{}
  67. urlBuilder.WriteString(path)
  68. urlBuilder.WriteString("?")
  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. request.queries = urlBuilder.String()
  81. }
  82. func (request *RoaRequest) GetUrl() string {
  83. return strings.ToLower(request.Scheme) + "://" + request.Domain + ":" + request.Port + request.GetQueries()
  84. }
  85. func (request *RoaRequest) addPathParam(key, value string) {
  86. request.PathParams[key] = value
  87. }
  88. func (request *RoaRequest) InitWithApiInfo(product, version, action, uriPattern, serviceCode, endpointType string) {
  89. request.baseRequest = defaultBaseRequest()
  90. request.PathParams = make(map[string]string)
  91. request.Headers["x-acs-version"] = version
  92. request.pathPattern = uriPattern
  93. request.locationServiceCode = serviceCode
  94. request.locationEndpointType = endpointType
  95. //request.product = product
  96. //request.version = version
  97. //request.actionName = action
  98. }
  99. func (request *RoaRequest) initWithCommonRequest(commonRequest *CommonRequest) {
  100. request.baseRequest = commonRequest.baseRequest
  101. request.PathParams = commonRequest.PathParams
  102. //request.product = commonRequest.Product
  103. //request.version = commonRequest.Version
  104. request.Headers["x-acs-version"] = commonRequest.Version
  105. //request.actionName = commonRequest.ApiName
  106. request.pathPattern = commonRequest.PathPattern
  107. request.locationServiceCode = ""
  108. request.locationEndpointType = ""
  109. }