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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// BaseClient provides common HTTP functionality for API clients
type BaseClient struct {
HTTPClient *http.Client
BaseURL string
}
// NewBaseClient creates a new BaseClient with default settings
func NewBaseClient(baseURL string) BaseClient {
return BaseClient{
HTTPClient: &http.Client{Timeout: 15 * time.Second},
BaseURL: baseURL,
}
}
// Get performs a GET request and decodes the JSON response
func (c *BaseClient) Get(ctx context.Context, path string, headers map[string]string, result interface{}) error {
req, err := http.NewRequestWithContext(ctx, "GET", c.BaseURL+path, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
for k, v := range headers {
req.Header.Set(k, v)
}
return c.doJSON(req, result)
}
// Post performs a POST request with JSON body and decodes the response
func (c *BaseClient) Post(ctx context.Context, path string, headers map[string]string, body interface{}, result interface{}) error {
var bodyReader io.Reader
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal request body: %w", err)
}
bodyReader = bytes.NewBuffer(jsonData)
}
req, err := http.NewRequestWithContext(ctx, "POST", c.BaseURL+path, bodyReader)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
for k, v := range headers {
req.Header.Set(k, v)
}
return c.doJSON(req, result)
}
// PostForm performs a POST request with form-encoded body
func (c *BaseClient) PostForm(ctx context.Context, path string, headers map[string]string, formData string, result interface{}) error {
req, err := http.NewRequestWithContext(ctx, "POST", c.BaseURL+path, bytes.NewBufferString(formData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
for k, v := range headers {
req.Header.Set(k, v)
}
return c.doJSON(req, result)
}
// PostEmpty performs a POST request with no body and expects no response body
func (c *BaseClient) PostEmpty(ctx context.Context, path string, headers map[string]string) error {
req, err := http.NewRequestWithContext(ctx, "POST", c.BaseURL+path, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
}
return nil
}
// Put performs a PUT request with form-encoded body
func (c *BaseClient) Put(ctx context.Context, path string, headers map[string]string, formData string, result interface{}) error {
req, err := http.NewRequestWithContext(ctx, "PUT", c.BaseURL+path, bytes.NewBufferString(formData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
for k, v := range headers {
req.Header.Set(k, v)
}
return c.doJSON(req, result)
}
// doJSON executes the request and decodes JSON response
func (c *BaseClient) doJSON(req *http.Request, result interface{}) error {
resp, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
}
if result != nil {
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
}
return nil
}
|