-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
112 lines (93 loc) · 2.84 KB
/
common.go
File metadata and controls
112 lines (93 loc) · 2.84 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package flashduty
import (
"context"
"fmt"
"net/http"
)
type dataEnvelope[T any] struct {
Error *DutyError `json:"error,omitempty"`
Data T `json:"data,omitempty"`
}
type optionalDataEnvelope[T any] struct {
Error *DutyError `json:"error,omitempty"`
Data *T `json:"data,omitempty"`
}
type emptyEnvelope struct {
Error *DutyError `json:"error,omitempty"`
}
func getData[T any](c *Client, ctx context.Context, path string, errPrefix string) (*T, error) {
resp, err := c.makeRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, fmt.Errorf("%s: %w", errPrefix, err)
}
defer func() { _ = resp.Body.Close() }()
return parseData[T](c, resp)
}
func postData[T any](c *Client, ctx context.Context, path string, body any, errPrefix string) (*T, error) {
resp, err := c.makeRequest(ctx, http.MethodPost, path, body)
if err != nil {
return nil, fmt.Errorf("%s: %w", errPrefix, err)
}
defer func() { _ = resp.Body.Close() }()
return parseData[T](c, resp)
}
func getOptionalData[T any](c *Client, ctx context.Context, path string, errPrefix string) (*T, error) {
resp, err := c.makeRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, fmt.Errorf("%s: %w", errPrefix, err)
}
defer func() { _ = resp.Body.Close() }()
return parseOptionalData[T](c, resp)
}
func postOptionalData[T any](c *Client, ctx context.Context, path string, body any, errPrefix string) (*T, error) {
resp, err := c.makeRequest(ctx, http.MethodPost, path, body)
if err != nil {
return nil, fmt.Errorf("%s: %w", errPrefix, err)
}
defer func() { _ = resp.Body.Close() }()
return parseOptionalData[T](c, resp)
}
func postEmpty(c *Client, ctx context.Context, path string, body any, errPrefix string) error {
resp, err := c.makeRequest(ctx, http.MethodPost, path, body)
if err != nil {
return fmt.Errorf("%s: %w", errPrefix, err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return handleAPIError(c.logger, resp)
}
var result emptyEnvelope
if err := parseResponse(c.logger, resp, &result); err != nil {
return err
}
if result.Error != nil {
return result.Error
}
return nil
}
func parseData[T any](c *Client, resp *http.Response) (*T, error) {
if resp.StatusCode != http.StatusOK {
return nil, handleAPIError(c.logger, resp)
}
var result dataEnvelope[T]
if err := parseResponse(c.logger, resp, &result); err != nil {
return nil, err
}
if result.Error != nil {
return nil, result.Error
}
return &result.Data, nil
}
func parseOptionalData[T any](c *Client, resp *http.Response) (*T, error) {
if resp.StatusCode != http.StatusOK {
return nil, handleAPIError(c.logger, resp)
}
var result optionalDataEnvelope[T]
if err := parseResponse(c.logger, resp, &result); err != nil {
return nil, err
}
if result.Error != nil {
return nil, result.Error
}
return result.Data, nil
}