-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
197 lines (172 loc) · 5.11 KB
/
Copy pathclient.go
File metadata and controls
197 lines (172 loc) · 5.11 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package cufinder
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
// Client represents the CUFinder API client
type Client struct {
apiKey string
baseURL string
httpClient *http.Client
}
// ClientConfig holds configuration for the client
type ClientConfig struct {
APIKey string
BaseURL string
Timeout time.Duration
MaxRetries int
}
// NewClient creates a new CUFinder client
func NewClient(config ClientConfig) *Client {
if config.BaseURL == "" {
config.BaseURL = "https://api.cufinder.io/v2"
}
if config.Timeout == 0 {
config.Timeout = 30 * time.Second
}
if config.MaxRetries == 0 {
config.MaxRetries = 3
}
return &Client{
apiKey: config.APIKey,
baseURL: config.BaseURL,
httpClient: &http.Client{
Timeout: config.Timeout,
},
}
}
// Post sends a POST request to the API
func (c *Client) Post(endpoint string, data interface{}) (map[string]interface{}, error) {
url := c.baseURL + endpoint
// Convert data to form-encoded format
formData, err := c.StructToFormData(data)
if err != nil {
return nil, fmt.Errorf("failed to convert data to form format: %w", err)
}
req, err := http.NewRequest("POST", url, strings.NewReader(formData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("x-api-key", c.apiKey)
req.Header.Set("User-Agent", "cufinder-go/1.3.0")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("API error: status %d, body: %s", resp.StatusCode, string(body))
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return result, nil
}
// StructToFormData converts a struct to form-encoded data
func (c *Client) StructToFormData(data interface{}) (string, error) {
v := reflect.ValueOf(data)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return "", fmt.Errorf("data must be a struct")
}
t := v.Type()
values := url.Values{}
for i := 0; i < v.NumField(); i++ {
field := t.Field(i)
fieldValue := v.Field(i)
// Skip unexported fields
if !fieldValue.CanInterface() {
continue
}
// Get JSON tag name
jsonTag := field.Tag.Get("json")
if jsonTag == "" || jsonTag == "-" {
continue
}
// Remove omitempty and other options
jsonTag = strings.Split(jsonTag, ",")[0]
// Handle different field types
if fieldValue.Kind() == reflect.Slice {
// Handle slices (like []string)
if fieldValue.Len() > 0 {
var sliceValues []string
for i := 0; i < fieldValue.Len(); i++ {
item := fieldValue.Index(i)
if item.Kind() == reflect.String {
sliceValues = append(sliceValues, item.String())
} else {
sliceValues = append(sliceValues, fmt.Sprintf("%v", item.Interface()))
}
}
// Join slice values with commas
values.Set(jsonTag, strings.Join(sliceValues, ","))
}
continue
}
// Convert field value to string
var strValue string
switch fieldValue.Kind() {
case reflect.String:
strValue = fieldValue.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
strValue = strconv.FormatInt(fieldValue.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
strValue = strconv.FormatUint(fieldValue.Uint(), 10)
case reflect.Float32, reflect.Float64:
strValue = strconv.FormatFloat(fieldValue.Float(), 'f', -1, 64)
case reflect.Bool:
strValue = strconv.FormatBool(fieldValue.Bool())
default:
// For other types, try to convert to string
strValue = fmt.Sprintf("%v", fieldValue.Interface())
}
// Add non-empty values, but handle different types appropriately
if fieldValue.Kind() == reflect.String {
// For strings, only add if not empty
if strValue != "" {
values.Set(jsonTag, strValue)
}
} else if fieldValue.Kind() == reflect.Bool {
// For booleans, always add the value (true/false)
if strValue != "false" {
values.Set(jsonTag, strconv.FormatBool(fieldValue.Bool()))
}
} else if fieldValue.Kind() >= reflect.Int && fieldValue.Kind() <= reflect.Int64 {
// For integers, only add if not zero
if fieldValue.Int() != 0 {
values.Set(jsonTag, strValue)
}
} else if fieldValue.Kind() >= reflect.Uint && fieldValue.Kind() <= reflect.Uint64 {
// For unsigned integers, only add if not zero
if fieldValue.Uint() != 0 {
values.Set(jsonTag, strValue)
}
} else if fieldValue.Kind() >= reflect.Float32 && fieldValue.Kind() <= reflect.Float64 {
// For floats, only add if not zero
if fieldValue.Float() != 0 {
values.Set(jsonTag, strValue)
}
} else {
// For other types, only add if not empty string representation
if strValue != "" {
values.Set(jsonTag, strValue)
}
}
}
return values.Encode(), nil
}