response.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. initJsonParserOnce()
  52. err = jsonParser.Unmarshal(response.GetHttpContentBytes(), response)
  53. if err != nil {
  54. err = errors.NewClientError(errors.JsonUnmarshalErrorCode, errors.JsonUnmarshalErrorMessage, err)
  55. }
  56. } else if strings.ToUpper(format) == "XML" {
  57. err = xml.Unmarshal(response.GetHttpContentBytes(), response)
  58. }
  59. return
  60. }
  61. type BaseResponse struct {
  62. httpStatus int
  63. httpHeaders map[string][]string
  64. httpContentString string
  65. httpContentBytes []byte
  66. originHttpResponse *http.Response
  67. }
  68. func (baseResponse *BaseResponse) GetHttpStatus() int {
  69. return baseResponse.httpStatus
  70. }
  71. func (baseResponse *BaseResponse) GetHttpHeaders() map[string][]string {
  72. return baseResponse.httpHeaders
  73. }
  74. func (baseResponse *BaseResponse) GetHttpContentString() string {
  75. return baseResponse.httpContentString
  76. }
  77. func (baseResponse *BaseResponse) GetHttpContentBytes() []byte {
  78. return baseResponse.httpContentBytes
  79. }
  80. func (baseResponse *BaseResponse) GetOriginHttpResponse() *http.Response {
  81. return baseResponse.originHttpResponse
  82. }
  83. func (baseResponse *BaseResponse) IsSuccess() bool {
  84. if baseResponse.GetHttpStatus() >= 200 && baseResponse.GetHttpStatus() < 300 {
  85. return true
  86. }
  87. return false
  88. }
  89. func (baseResponse *BaseResponse) parseFromHttpResponse(httpResponse *http.Response) (err error) {
  90. defer httpResponse.Body.Close()
  91. body, err := ioutil.ReadAll(httpResponse.Body)
  92. if err != nil {
  93. return
  94. }
  95. baseResponse.httpStatus = httpResponse.StatusCode
  96. baseResponse.httpHeaders = httpResponse.Header
  97. baseResponse.httpContentBytes = body
  98. baseResponse.httpContentString = string(body)
  99. baseResponse.originHttpResponse = httpResponse
  100. return
  101. }
  102. func (baseResponse *BaseResponse) String() string {
  103. resultBuilder := bytes.Buffer{}
  104. // statusCode
  105. // resultBuilder.WriteString("\n")
  106. resultBuilder.WriteString(fmt.Sprintf("%s %s\n", baseResponse.originHttpResponse.Proto, baseResponse.originHttpResponse.Status))
  107. // httpHeaders
  108. //resultBuilder.WriteString("Headers:\n")
  109. for key, value := range baseResponse.httpHeaders {
  110. resultBuilder.WriteString(key + ": " + strings.Join(value, ";") + "\n")
  111. }
  112. resultBuilder.WriteString("\n")
  113. // content
  114. //resultBuilder.WriteString("Content:\n")
  115. resultBuilder.WriteString(baseResponse.httpContentString + "\n")
  116. return resultBuilder.String()
  117. }
  118. type CommonResponse struct {
  119. *BaseResponse
  120. }
  121. func NewCommonResponse() (response *CommonResponse) {
  122. return &CommonResponse{
  123. BaseResponse: &BaseResponse{},
  124. }
  125. }