response.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "strconv"
  22. "strings"
  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.GetOriginHttpResponse().Status, response.GetHttpContentString())
  40. return
  41. }
  42. if strings.ToUpper(format) == "JSON" {
  43. initJsonParserOnce()
  44. err = jsonParser.Unmarshal(response.GetHttpContentBytes(), response)
  45. } else if strings.ToUpper(format) == "XML" {
  46. err = xml.Unmarshal(response.GetHttpContentBytes(), response)
  47. }
  48. return
  49. }
  50. type BaseResponse struct {
  51. httpStatus int
  52. httpHeaders map[string][]string
  53. httpContentString string
  54. httpContentBytes []byte
  55. originHttpResponse *http.Response
  56. }
  57. func (baseResponse *BaseResponse) GetHttpStatus() int {
  58. return baseResponse.httpStatus
  59. }
  60. func (baseResponse *BaseResponse) GetHttpHeaders() map[string][]string {
  61. return baseResponse.httpHeaders
  62. }
  63. func (baseResponse *BaseResponse) GetHttpContentString() string {
  64. return baseResponse.httpContentString
  65. }
  66. func (baseResponse *BaseResponse) GetHttpContentBytes() []byte {
  67. return baseResponse.httpContentBytes
  68. }
  69. func (baseResponse *BaseResponse) GetOriginHttpResponse() *http.Response {
  70. return baseResponse.originHttpResponse
  71. }
  72. func (baseResponse *BaseResponse) IsSuccess() bool {
  73. if baseResponse.GetHttpStatus() >= 200 && baseResponse.GetHttpStatus() < 300 {
  74. return true
  75. }
  76. return false
  77. }
  78. func (baseResponse *BaseResponse) parseFromHttpResponse(httpResponse *http.Response) (err error) {
  79. defer httpResponse.Body.Close()
  80. body, err := ioutil.ReadAll(httpResponse.Body)
  81. if err != nil {
  82. return
  83. }
  84. baseResponse.httpStatus = httpResponse.StatusCode
  85. baseResponse.httpHeaders = httpResponse.Header
  86. baseResponse.httpContentBytes = body
  87. baseResponse.httpContentString = string(body)
  88. baseResponse.originHttpResponse = httpResponse
  89. return
  90. }
  91. func (baseResponse *BaseResponse) String() string {
  92. resultBuilder := bytes.Buffer{}
  93. // statusCode
  94. resultBuilder.WriteString("StatusCode : " + strconv.Itoa(baseResponse.httpStatus) + "\n")
  95. // httpHeaders
  96. resultBuilder.WriteString("Headers:\n")
  97. for key, value := range baseResponse.httpHeaders {
  98. resultBuilder.WriteString(" -> " + key + " : " + strings.Join(value, ";") + "\n")
  99. }
  100. // content
  101. resultBuilder.WriteString("Content:\n")
  102. resultBuilder.WriteString(" -> " + baseResponse.httpContentString + "\n")
  103. return resultBuilder.String()
  104. }
  105. type CommonResponse struct {
  106. *BaseResponse
  107. }
  108. func NewCommonResponse() (request *CommonResponse) {
  109. return &CommonResponse{
  110. BaseResponse: &BaseResponse{},
  111. }
  112. }