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.

118 lines
2.8 KiB

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