/
response.go
44 lines (36 loc) · 1.05 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
package netkit
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/hungdv136/rio/internal/log"
"github.com/hungdv136/rio/internal/util"
)
// Response is generic response structure
// Body is data type of the response body
type Response[Body any] struct {
StatusCode int `json:"status_code"`
Body Body `json:"body"`
}
// ParseResponse parses an http response to struct
func ParseResponse[R any](ctx context.Context, r *http.Response) (*Response[R], error) {
defer util.CloseSilently(ctx, r.Body.Close)
resp := Response[R]{}
decoder := json.NewDecoder(r.Body)
decoder.UseNumber()
if err := decoder.Decode(&resp.Body); err != nil {
log.Error(ctx, err)
return nil, err
}
resp.StatusCode = r.StatusCode
return &resp, nil
}
// InternalBody defines struct for internal response body
// This is for communicate with internal services (rio exposed services)
type InternalBody[R any] struct {
Verdict string `json:"verdict"`
Message string `json:"message"`
Time time.Time `json:"time"`
Data R `json:"data"`
}