response.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 responses
  15. import (
  16. "bytes"
  17. "encoding/xml"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "strings"
  22. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
  23. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
  24. )
  25. type AcsResponse interface {
  26. IsSuccess() bool
  27. GetHttpStatus() int
  28. GetHttpHeaders() map[string][]string
  29. GetHttpContentString() string
  30. GetHttpContentBytes() []byte
  31. GetOriginHttpResponse() *http.Response
  32. parseFromHttpResponse(httpResponse *http.Response) error
  33. }
  34. var debug utils.Debug
  35. func init() {
  36. debug = utils.Init("sdk")
  37. }
  38. // Unmarshal object from http response body to target Response
  39. func Unmarshal(response AcsResponse, httpResponse *http.Response, format string) (err error) {
  40. err = response.parseFromHttpResponse(httpResponse)
  41. if err != nil {
  42. return
  43. }
  44. if !response.IsSuccess() {
  45. err = errors.NewServerError(response.GetHttpStatus(), response.GetHttpContentString(), "")
  46. return
  47. }
  48. if _, isCommonResponse := response.(*CommonResponse); isCommonResponse {
  49. // common response need not unmarshal
  50. return
  51. }
  52. if len(response.GetHttpContentBytes()) == 0 {
  53. return
  54. }
  55. if strings.ToUpper(format) == "JSON" {
  56. err = jsonParser.Unmarshal(response.GetHttpContentBytes(), response)
  57. if err != nil {
  58. err = errors.NewClientError(errors.JsonUnmarshalErrorCode, errors.JsonUnmarshalErrorMessage, err)
  59. }
  60. } else if strings.ToUpper(format) == "XML" {
  61. err = xml.Unmarshal(response.GetHttpContentBytes(), response)
  62. }
  63. return
  64. }
  65. type BaseResponse struct {
  66. httpStatus int
  67. httpHeaders map[string][]string
  68. httpContentString string
  69. httpContentBytes []byte
  70. originHttpResponse *http.Response
  71. }
  72. func (baseResponse *BaseResponse) GetHttpStatus() int {
  73. return baseResponse.httpStatus
  74. }
  75. func (baseResponse *BaseResponse) GetHttpHeaders() map[string][]string {
  76. return baseResponse.httpHeaders
  77. }
  78. func (baseResponse *BaseResponse) GetHttpContentString() string {
  79. return baseResponse.httpContentString
  80. }
  81. func (baseResponse *BaseResponse) GetHttpContentBytes() []byte {
  82. return baseResponse.httpContentBytes
  83. }
  84. func (baseResponse *BaseResponse) GetOriginHttpResponse() *http.Response {
  85. return baseResponse.originHttpResponse
  86. }
  87. func (baseResponse *BaseResponse) IsSuccess() bool {
  88. if baseResponse.GetHttpStatus() >= 200 && baseResponse.GetHttpStatus() < 300 {
  89. return true
  90. }
  91. return false
  92. }
  93. func (baseResponse *BaseResponse) parseFromHttpResponse(httpResponse *http.Response) (err error) {
  94. defer httpResponse.Body.Close()
  95. body, err := ioutil.ReadAll(httpResponse.Body)
  96. if err != nil {
  97. return
  98. }
  99. debug("%s", string(body))
  100. baseResponse.httpStatus = httpResponse.StatusCode
  101. baseResponse.httpHeaders = httpResponse.Header
  102. baseResponse.httpContentBytes = body
  103. baseResponse.httpContentString = string(body)
  104. baseResponse.originHttpResponse = httpResponse
  105. return
  106. }
  107. func (baseResponse *BaseResponse) String() string {
  108. resultBuilder := bytes.Buffer{}
  109. // statusCode
  110. // resultBuilder.WriteString("\n")
  111. resultBuilder.WriteString(fmt.Sprintf("%s %s\n", baseResponse.originHttpResponse.Proto, baseResponse.originHttpResponse.Status))
  112. // httpHeaders
  113. //resultBuilder.WriteString("Headers:\n")
  114. for key, value := range baseResponse.httpHeaders {
  115. resultBuilder.WriteString(key + ": " + strings.Join(value, ";") + "\n")
  116. }
  117. resultBuilder.WriteString("\n")
  118. // content
  119. //resultBuilder.WriteString("Content:\n")
  120. resultBuilder.WriteString(baseResponse.httpContentString + "\n")
  121. return resultBuilder.String()
  122. }
  123. type CommonResponse struct {
  124. *BaseResponse
  125. }
  126. func NewCommonResponse() (response *CommonResponse) {
  127. return &CommonResponse{
  128. BaseResponse: &BaseResponse{},
  129. }
  130. }