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.

80 line
1.9 KiB

  1. package responses
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. )
  6. type AcsResponse interface {
  7. IsSuccess() bool
  8. GetHttpStatus() int
  9. GetHttpHeaders() map[string][]string
  10. GetHttpContentString() string
  11. GetHttpContentBytes() []byte
  12. GetOriginHttpResponse() *http.Response
  13. parseFromHttpResponse(httpResponse *http.Response) error
  14. }
  15. type BaseResponse struct {
  16. httpStatus int
  17. httpHeaders map[string][]string
  18. httpContentString string
  19. httpContentBytes []byte
  20. originHttpResponse *http.Response
  21. }
  22. func (baseResponse *BaseResponse) GetHttpStatus() int {
  23. return baseResponse.httpStatus
  24. }
  25. func (baseResponse *BaseResponse) GetHttpHeaders() map[string][]string {
  26. return baseResponse.httpHeaders
  27. }
  28. func (baseResponse *BaseResponse) GetHttpContentString() string {
  29. return baseResponse.httpContentString
  30. }
  31. func (baseResponse *BaseResponse) GetHttpContentBytes() []byte {
  32. return baseResponse.httpContentBytes
  33. }
  34. func (baseResponse *BaseResponse) GetOriginHttpResponse() *http.Response {
  35. return baseResponse.originHttpResponse
  36. }
  37. func (baseResponse *BaseResponse) IsSuccess() bool {
  38. if baseResponse.GetHttpStatus() >= 200 && baseResponse.GetHttpStatus() < 300 {
  39. return true
  40. }
  41. return false
  42. }
  43. func (baseResponse *BaseResponse) parseFromHttpResponse(httpResponse *http.Response) (err error) {
  44. defer httpResponse.Body.Close()
  45. body, err := ioutil.ReadAll(httpResponse.Body)
  46. if err != nil {
  47. return
  48. }
  49. baseResponse.httpStatus = httpResponse.StatusCode
  50. baseResponse.httpHeaders = httpResponse.Header
  51. baseResponse.httpContentBytes = body
  52. baseResponse.httpContentString = string(body)
  53. baseResponse.originHttpResponse = httpResponse
  54. return
  55. }
  56. type CommonResponse struct {
  57. *BaseResponse
  58. }
  59. func NewCommonResponse() (response *CommonResponse) {
  60. return &CommonResponse{
  61. BaseResponse: &BaseResponse{},
  62. }
  63. }
  64. func Unmarshal(response AcsResponse, httpResponse *http.Response, format string) {
  65. }