-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse.go
58 lines (47 loc) · 1.11 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package tapd
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
// Response represents an API response.
type Response struct {
*http.Response
}
// newResponse creates a new Response.
func newResponse(httpResp *http.Response) *Response {
return &Response{Response: httpResp}
}
// RawBody represents a raw body.
type RawBody struct {
Status int `json:"status"`
Data json.RawMessage `json:"data"`
Info string `json:"info"`
}
// ErrorResponse represents a tapd error response.
type ErrorResponse struct {
response *http.Response
rawBody *RawBody
err error
}
func (e *ErrorResponse) Error() string {
if e.rawBody != nil {
return fmt.Sprintf("code: %d, info: %s", e.rawBody.Status, e.rawBody.Info)
}
if e.response != nil {
return fmt.Sprintf("status code: %d, err: %v", e.response.StatusCode, e.err)
}
return e.err.Error()
}
func (e *ErrorResponse) Unwrap() error {
return e.err
}
func IsErrorResponse(err error) bool {
var e *ErrorResponse
return errors.As(err, &e)
}
// CountResponse represents the response of count.
type CountResponse struct {
Count int `json:"count"`
}