You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 line
2.5 KiB

  1. package responses
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. )
  9. type AcsResponse interface {
  10. IsSuccess() bool
  11. GetHttpStatus() int
  12. GetHttpHeaders() map[string][]string
  13. GetHttpContentString() string
  14. GetHttpContentBytes() []byte
  15. GetOriginHttpResponse() *http.Response
  16. parseFromHttpResponse(httpResponse *http.Response) error
  17. }
  18. type BaseResponse struct {
  19. httpStatus int
  20. httpHeaders map[string][]string
  21. httpContentString string
  22. httpContentBytes []byte
  23. originHttpResponse *http.Response
  24. Code int `json:"code"`
  25. Status bool `json:"status"`
  26. Msg string `json:"msg"`
  27. }
  28. func (baseResponse *BaseResponse) GetHttpStatus() int {
  29. return baseResponse.httpStatus
  30. }
  31. func (baseResponse *BaseResponse) GetHttpHeaders() map[string][]string {
  32. return baseResponse.httpHeaders
  33. }
  34. func (baseResponse *BaseResponse) GetHttpContentString() string {
  35. return baseResponse.httpContentString
  36. }
  37. func (baseResponse *BaseResponse) GetHttpContentBytes() []byte {
  38. return baseResponse.httpContentBytes
  39. }
  40. func (baseResponse *BaseResponse) GetOriginHttpResponse() *http.Response {
  41. return baseResponse.originHttpResponse
  42. }
  43. func (baseResponse *BaseResponse) IsSuccess() bool {
  44. if baseResponse.GetHttpStatus() >= 200 && baseResponse.GetHttpStatus() < 300 {
  45. return true
  46. }
  47. return false
  48. }
  49. func (baseResponse *BaseResponse) parseFromHttpResponse(httpResponse *http.Response) (err error) {
  50. defer httpResponse.Body.Close()
  51. body, err := ioutil.ReadAll(httpResponse.Body)
  52. if err != nil {
  53. return
  54. }
  55. baseResponse.httpStatus = httpResponse.StatusCode
  56. baseResponse.httpHeaders = httpResponse.Header
  57. baseResponse.httpContentBytes = body
  58. baseResponse.httpContentString = string(body)
  59. baseResponse.originHttpResponse = httpResponse
  60. return
  61. }
  62. type CommonResponse struct {
  63. *BaseResponse
  64. }
  65. func NewCommonResponse() (response *CommonResponse) {
  66. return &CommonResponse{
  67. BaseResponse: &BaseResponse{},
  68. }
  69. }
  70. func Unmarshal(response AcsResponse, httpResponse *http.Response, format string) (err error) {
  71. err = response.parseFromHttpResponse(httpResponse)
  72. if err != nil {
  73. return
  74. }
  75. if !response.IsSuccess() {
  76. err = errors.New(fmt.Sprintf("%d %s", response.GetHttpStatus(), response.GetHttpContentString()))
  77. return
  78. }
  79. if _, isCommonResponse := response.(CommonResponse); isCommonResponse {
  80. return
  81. }
  82. if format != "xml" {
  83. err = json.Unmarshal(response.GetHttpContentBytes(), response)
  84. if err != nil {
  85. return errors.New("json Unmarshal:" + err.Error())
  86. }
  87. }
  88. return
  89. }