-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenderize.go
183 lines (144 loc) · 3.83 KB
/
genderize.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
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
package genderize
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
)
type (
// Client is the client to call genderize.io
Client struct {
apiKey string
baseUrl string
http *http.Client
}
// clientDefaults is a struct used to hold the default values for the client
clientDefaults struct {
apiKey string
baseUrl string
http *http.Client
}
// ClientOption is a function that can be used to configure the client
ClientOption func(*clientDefaults)
// Prediction is the response from the API
Prediction struct {
Name string `json:"name"`
Gender string `json:"gender"`
Probability float64 `json:"probability"`
Count int `json:"count"`
}
// RateLimit is the rate limiting information from the API
RateLimit struct {
Limit string
Remaining string
Reset string
}
// errorResponse is the error response from the genderize API
errorResponse struct {
Error string `json:"error"`
}
)
// WithApiKey overrides the default API key
func WithUrl(baseUrl string) ClientOption {
return func(client *clientDefaults) {
client.baseUrl = baseUrl
}
}
// WithApiKey overrides the default API key
func WithApiKey(apiKey string) ClientOption {
return func(client *clientDefaults) {
client.apiKey = apiKey
}
}
// WithClient overrides the default http client
func WithClient(httpClient *http.Client) ClientOption {
return func(client *clientDefaults) {
client.http = httpClient
}
}
// NewClient creates a client to call genderize.io
// By default, the client will use the public API URL without an API key.
// The default configuration can be overridden by passing in options.
func NewClient(opts ...ClientOption) *Client {
// We use the default option to prevent Client options from having access to private data in the client
defaults := &clientDefaults{
apiKey: "",
baseUrl: "https://api.genderize.io",
http: &http.Client{},
}
for _, opt := range opts {
opt(defaults)
}
return &Client{
apiKey: defaults.apiKey,
baseUrl: defaults.baseUrl,
http: defaults.http,
}
}
func (client *Client) Predict(name string) (*Prediction, *RateLimit, error) {
url, _ := url.Parse(client.baseUrl)
values := url.Query()
values.Add("name", name)
if client.apiKey != "" {
values.Add("apikey", client.apiKey)
}
url.RawQuery = values.Encode()
body, rateLimit, err := client.get(url.String())
if err != nil {
return nil, rateLimit, err
}
var prediction Prediction
err = json.Unmarshal(body, &prediction)
if err != nil {
return nil, rateLimit, err
}
return &prediction, rateLimit, nil
}
func (client *Client) BatchPredict(names []string) ([]Prediction, *RateLimit, error) {
url, _ := url.Parse(client.baseUrl)
values := url.Query()
for _, name := range names {
values.Add("name[]", name)
}
if client.apiKey != "" {
values.Add("apikey", client.apiKey)
}
url.RawQuery = values.Encode()
body, rateLimit, err := client.get(url.String())
if err != nil {
return nil, rateLimit, err
}
var predictions []Prediction
err = json.Unmarshal(body, &predictions)
if err != nil {
return nil, rateLimit, err
}
return predictions, rateLimit, nil
}
// get makes the API request and returns the response body
func (client *Client) get(url string) ([]byte, *RateLimit, error) {
resp, err := http.Get(url)
if err != nil {
return nil, nil, err
}
rateLimit := &RateLimit{
Limit: resp.Header.Get("X-Rate-Limit-Limit"),
Remaining: resp.Header.Get("X-Rate-Limit-Remaining"),
Reset: resp.Header.Get("X-Rate-Reset"),
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
var resp errorResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, rateLimit, err
}
return nil, rateLimit, errors.New(resp.Error)
}
if err != nil {
return nil, rateLimit, err
}
return body, rateLimit, nil
}