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