76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
|
package responses
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type AcsResponse interface {
|
||
|
IsSuccess() bool
|
||
|
GetHttpStatus() int
|
||
|
GetHttpHeaders() map[string][]string
|
||
|
GetHttpContentString() string
|
||
|
GetHttpContentBytes() []byte
|
||
|
GetOriginHttpResponse() *http.Response
|
||
|
parseFromHttpResponse(httpResponse *http.Response) error
|
||
|
}
|
||
|
|
||
|
type BaseResponse struct {
|
||
|
httpStatus int
|
||
|
httpHeaders map[string][]string
|
||
|
httpContentString string
|
||
|
httpContentBytes []byte
|
||
|
originHttpResponse *http.Response
|
||
|
}
|
||
|
|
||
|
func (baseResponse *BaseResponse) GetHttpStatus() int {
|
||
|
return baseResponse.httpStatus
|
||
|
}
|
||
|
|
||
|
func (baseResponse *BaseResponse) GetHttpHeaders() map[string][]string {
|
||
|
return baseResponse.httpHeaders
|
||
|
}
|
||
|
|
||
|
func (baseResponse *BaseResponse) GetHttpContentString() string {
|
||
|
return baseResponse.httpContentString
|
||
|
}
|
||
|
|
||
|
func (baseResponse *BaseResponse) GetHttpContentBytes() []byte {
|
||
|
return baseResponse.httpContentBytes
|
||
|
}
|
||
|
|
||
|
func (baseResponse *BaseResponse) GetOriginHttpResponse() *http.Response {
|
||
|
return baseResponse.originHttpResponse
|
||
|
}
|
||
|
|
||
|
func (baseResponse *BaseResponse) IsSuccess() bool {
|
||
|
if baseResponse.GetHttpStatus() >= 200 && baseResponse.GetHttpStatus() < 300 {
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (baseResponse *BaseResponse) parseFromHttpResponse(httpResponse *http.Response) (err error) {
|
||
|
defer httpResponse.Body.Close()
|
||
|
body, err := ioutil.ReadAll(httpResponse.Body)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
baseResponse.httpStatus = httpResponse.StatusCode
|
||
|
baseResponse.httpHeaders = httpResponse.Header
|
||
|
baseResponse.httpContentBytes = body
|
||
|
baseResponse.httpContentString = string(body)
|
||
|
baseResponse.originHttpResponse = httpResponse
|
||
|
return
|
||
|
}
|
||
|
|
||
|
type CommonResponse struct {
|
||
|
*BaseResponse
|
||
|
}
|
||
|
|
||
|
func NewCommonResponse() (response *CommonResponse) {
|
||
|
return &CommonResponse{
|
||
|
BaseResponse: &BaseResponse{},
|
||
|
}
|
||
|
}
|