-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
142 lines (117 loc) · 3.68 KB
/
client.go
File metadata and controls
142 lines (117 loc) · 3.68 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
// Package callingcode provides a Go client for the Country Calling Code API.
//
// For more information, visit: https://apiverve.com/marketplace/callingcode?utm_source=go&utm_medium=readme
package callingcode
import (
"errors"
"fmt"
"time"
"github.com/go-resty/resty/v2"
)
const (
baseURL = "https://api.apiverve.com/v1/callingcode"
defaultTimeout = 30 * time.Second
)
// Client is the Country Calling Code API client.
type Client struct {
apiKey string
httpClient *resty.Client
}
// NewClient creates a new Country Calling Code API client.
func NewClient(apiKey string) *Client {
client := resty.New()
client.SetTimeout(defaultTimeout)
client.SetHeader("Content-Type", "application/json")
return &Client{
apiKey: apiKey,
httpClient: client,
}
}
// SetTimeout sets the HTTP client timeout.
func (c *Client) SetTimeout(timeout time.Duration) {
c.httpClient.SetTimeout(timeout)
}
// Execute makes a request to the Country Calling Code API with typed parameters.
//
// Parameters are validated before sending the request. If validation fails,
// an error is returned immediately without making a network request.
//
// Available parameters:
// - country (required): string - The 2 letter ISO code for the country (e.g. FR) [minLength: 2, maxLength: 2]
func (c *Client) Execute(req *Request) (*Response, error) {
if c.apiKey == "" {
return nil, errors.New("API key is required. Get your API key at: https://apiverve.com")
}
// Validate parameters before making request
if req != nil {
if err := req.Validate(); err != nil {
return nil, err
}
}
request := c.httpClient.R().
SetHeader("x-api-key", c.apiKey).
SetResult(&Response{}).
SetError(&ErrorResponse{})
// GET request with query parameters
if req != nil {
request.SetQueryParams(req.ToQueryParams())
}
resp, err := request.Get(baseURL)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
if resp.IsError() {
if errResp, ok := resp.Error().(*ErrorResponse); ok {
return nil, fmt.Errorf("API error: %s", errResp.Error)
}
return nil, fmt.Errorf("API error: status %d", resp.StatusCode())
}
result, ok := resp.Result().(*Response)
if !ok {
return nil, errors.New("failed to parse response")
}
return result, nil
}
// ExecuteRaw makes a request with a raw map of parameters (for dynamic usage).
func (c *Client) ExecuteRaw(params map[string]interface{}) (*Response, error) {
if c.apiKey == "" {
return nil, errors.New("API key is required. Get your API key at: https://apiverve.com")
}
request := c.httpClient.R().
SetHeader("x-api-key", c.apiKey).
SetResult(&Response{}).
SetError(&ErrorResponse{})
if params != nil {
queryParams := make(map[string]string)
for k, v := range params {
queryParams[k] = fmt.Sprintf("%v", v)
}
request.SetQueryParams(queryParams)
}
resp, err := request.Get(baseURL)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
if resp.IsError() {
if errResp, ok := resp.Error().(*ErrorResponse); ok {
return nil, fmt.Errorf("API error: %s", errResp.Error)
}
return nil, fmt.Errorf("API error: status %d", resp.StatusCode())
}
result, ok := resp.Result().(*Response)
if !ok {
return nil, errors.New("failed to parse response")
}
return result, nil
}
// Response represents the API response.
type Response struct {
Status string `json:"status"`
Error interface{} `json:"error"`
Data ResponseData `json:"data"`
}
// ErrorResponse represents an error response from the API.
type ErrorResponse struct {
Status string `json:"status"`
Error string `json:"error"`
}