roa_request.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. return request.queries
  42. }
  43. func (request *RoaRequest) BuildQueries() string {
  44. // replace path params with value
  45. path := request.pathPattern
  46. for key, value := range request.PathParams {
  47. path = strings.Replace(path, "["+key+"]", value, 1)
  48. }
  49. queryParams := request.QueryParams
  50. // check if path contains params
  51. splitArray := strings.Split(path, "?")
  52. path = splitArray[0]
  53. if len(splitArray) > 1 && len(splitArray[1]) > 0 {
  54. queryParams[splitArray[1]] = ""
  55. }
  56. // sort QueryParams by key
  57. var queryKeys []string
  58. for key := range queryParams {
  59. queryKeys = append(queryKeys, key)
  60. }
  61. sort.Strings(queryKeys)
  62. // append urlBuilder
  63. urlBuilder := bytes.Buffer{}
  64. urlBuilder.WriteString(path)
  65. urlBuilder.WriteString("?")
  66. for i := 0; i < len(queryKeys); i++ {
  67. queryKey := queryKeys[i]
  68. urlBuilder.WriteString(queryKey)
  69. if value := queryParams[queryKey]; len(value) > 0 {
  70. urlBuilder.WriteString("=")
  71. urlBuilder.WriteString(value)
  72. }
  73. if i < len(queryKeys)-1 {
  74. urlBuilder.WriteString("&")
  75. }
  76. }
  77. request.queries = urlBuilder.String()
  78. return request.queries
  79. }
  80. func (request *RoaRequest) GetUrl() string {
  81. return strings.ToLower(request.Scheme) + "://" + request.Domain + ":" + request.Port + request.GetQueries()
  82. }
  83. func (request *RoaRequest) BuildUrl() string {
  84. return strings.ToLower(request.Scheme) + "://" + request.Domain + ":" + request.Port + request.BuildQueries()
  85. }
  86. func (request *RoaRequest) addPathParam(key, value string) {
  87. request.PathParams[key] = value
  88. }
  89. func (request *RoaRequest) InitWithApiInfo(product, version, action, uriPattern, serviceCode, endpointType string) {
  90. request.baseRequest = defaultBaseRequest()
  91. request.PathParams = make(map[string]string)
  92. request.Headers["x-acs-version"] = version
  93. request.pathPattern = uriPattern
  94. request.locationServiceCode = serviceCode
  95. request.locationEndpointType = endpointType
  96. //request.product = product
  97. //request.version = version
  98. //request.actionName = action
  99. }
  100. func (request *RoaRequest) initWithCommonRequest(commonRequest *CommonRequest) {
  101. request.baseRequest = commonRequest.baseRequest
  102. request.PathParams = commonRequest.PathParams
  103. //request.product = commonRequest.Product
  104. //request.version = commonRequest.Version
  105. request.Headers["x-acs-version"] = commonRequest.Version
  106. //request.actionName = commonRequest.ApiName
  107. request.pathPattern = commonRequest.PathPattern
  108. request.locationServiceCode = ""
  109. request.locationEndpointType = ""
  110. }