response.go 3.3 KB

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