response.go 3.8 KB

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