roa_request.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "net/url"
  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. urlBuilder.WriteString("?")
  71. for i := 0; i < len(queryKeys); i++ {
  72. queryKey := queryKeys[i]
  73. urlBuilder.WriteString(queryKey)
  74. if value := queryParams[queryKey]; len(value) > 0 {
  75. urlBuilder.WriteString("=")
  76. if needParamEncode{
  77. urlBuilder.WriteString(url.QueryEscape(value))
  78. }else{
  79. urlBuilder.WriteString(value)
  80. }
  81. }
  82. if i < len(queryKeys)-1 {
  83. urlBuilder.WriteString("&")
  84. }
  85. }
  86. result := urlBuilder.String()
  87. result = popStandardUrlencode(result)
  88. request.queries = result
  89. return request.queries
  90. }
  91. func popStandardUrlencode(stringToSign string)(result string){
  92. result = strings.Replace(stringToSign, "+", "%20", -1)
  93. result = strings.Replace(result, "*", "%2A", -1)
  94. result = strings.Replace(result, "%7E", "~", -1)
  95. return
  96. }
  97. func (request *RoaRequest) GetUrl() string {
  98. return strings.ToLower(request.Scheme) + "://" + request.Domain + ":" + request.Port + request.GetQueries()
  99. }
  100. func (request *RoaRequest) BuildUrl() string {
  101. // for network trans, need url encoded
  102. return strings.ToLower(request.Scheme) + "://" + request.Domain + ":" + request.Port + request.buildQueries(true)
  103. }
  104. func (request *RoaRequest) addPathParam(key, value string) {
  105. request.PathParams[key] = value
  106. }
  107. func (request *RoaRequest) InitWithApiInfo(product, version, action, uriPattern, serviceCode, endpointType string) {
  108. request.baseRequest = defaultBaseRequest()
  109. request.PathParams = make(map[string]string)
  110. request.Headers["x-acs-version"] = version
  111. request.pathPattern = uriPattern
  112. request.locationServiceCode = serviceCode
  113. request.locationEndpointType = endpointType
  114. //request.product = product
  115. //request.version = version
  116. //request.actionName = action
  117. }
  118. func (request *RoaRequest) initWithCommonRequest(commonRequest *CommonRequest) {
  119. request.baseRequest = commonRequest.baseRequest
  120. request.PathParams = commonRequest.PathParams
  121. //request.product = commonRequest.Product
  122. //request.version = commonRequest.Version
  123. request.Headers["x-acs-version"] = commonRequest.Version
  124. //request.actionName = commonRequest.ApiName
  125. request.pathPattern = commonRequest.PathPattern
  126. request.locationServiceCode = ""
  127. request.locationEndpointType = ""
  128. }