response.go 3.4 KB

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