-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
468 lines (425 loc) · 11.6 KB
/
client.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package webfonts
import (
"context"
"crypto/md5"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/chromedp/verhist"
"github.com/kenshaw/diskcache"
"github.com/kenshaw/httplog"
"golang.org/x/oauth2"
gtransport "google.golang.org/api/googleapi/transport"
"google.golang.org/api/option"
gfonts "google.golang.org/api/webfonts/v1"
)
// DefaultTransport is the default http transport.
var DefaultTransport = http.DefaultTransport
// Client is a webfonts client.
type Client struct {
userAgent string
transport http.RoundTripper
appCacheDir string
key string
source oauth2.TokenSource
opts []option.ClientOption
cl *http.Client
svc *gfonts.Service
once sync.Once
}
// NewClient creates a new webfonts client.
func NewClient(opts ...ClientOption) *Client {
cl := &Client{
transport: DefaultTransport,
}
for _, o := range opts {
o(cl)
}
return cl
}
// init initializes the client.
func (cl *Client) init(ctx context.Context) error {
var err error
cl.once.Do(func() {
if err = cl.buildTransport(ctx); err != nil {
return
}
if err = cl.buildUserAgent(ctx); err != nil {
return
}
if err = cl.buildService(ctx); err != nil {
return
}
})
return err
}
// buildTransport builds the http client used for retrievals.
func (cl *Client) buildTransport(ctx context.Context) error {
if cl.appCacheDir != "" {
var err error
cl.transport, err = diskcache.New(
diskcache.WithTransport(cl.transport),
diskcache.WithAppCacheDir(cl.appCacheDir),
diskcache.WithTTL(24*time.Hour),
diskcache.WithHeaderWhitelist("Date", "Set-Cookie", "Content-Type", "Location"),
diskcache.WithErrorTruncator(),
diskcache.WithGzipCompression(),
)
if err != nil {
return err
}
}
cl.cl = &http.Client{
Transport: cl.transport,
}
return nil
}
// buildUserAgent builds the user agent.
func (cl *Client) buildUserAgent(ctx context.Context) error {
if cl.userAgent != "" {
return nil
}
var err error
cl.userAgent, err = verhist.UserAgent(ctx, "linux", "stable", verhist.WithTransport(cl.transport))
return err
}
// buildService builds the google webfonts service.
func (cl *Client) buildService(ctx context.Context) error {
if cl.svc != nil {
return nil
}
// build transport
transport := cl.transport
switch {
case cl.source != nil:
transport = &oauth2.Transport{
Source: cl.source,
Base: transport,
}
case cl.key != "":
transport = >ransport.APIKey{
Key: cl.key,
Transport: transport,
}
}
// build service
opts := append(cl.opts, option.WithHTTPClient(&http.Client{
Transport: transport,
}))
var err error
cl.svc, err = gfonts.NewService(ctx, opts...)
return err
}
// Available retrieves all available webfonts from the google webfonts service.
func (cl *Client) Available(ctx context.Context) ([]*gfonts.Webfont, error) {
// init
if err := cl.init(ctx); err != nil {
return nil, err
}
if cl.svc == nil {
return nil, ErrServiceUninitialized
}
// retrieve
res, err := cl.svc.Webfonts.List().Context(ctx).Do()
if err != nil {
return nil, err
}
return res.Items, nil
}
// get retrieves a stylesheet from the url using the specified user agent,
// return any parsed font faces contained in the stylesheet.
//
// Adds &_=<md5hash(userAgent)[:5]> to the query request to ensure request
// traverses transport caching.
func (cl *Client) get(ctx context.Context, urlstr, userAgent string) ([]Font, error) {
// build request
urlstr += "&_=" + fmt.Sprintf("%x", md5.Sum([]byte(userAgent)))[:5]
req, err := http.NewRequest("GET", urlstr, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
// execute
res, err := cl.cl.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer res.Body.Close()
// check status
if res.StatusCode != http.StatusOK {
return nil, ErrStatusNotOK
}
// parse
return FontsFromStylesheetReader(res.Body)
}
// Faces retrieves the font faces for the specified family, building a query
// using the client's user agent and passed options.
func (cl *Client) Faces(ctx context.Context, family string, opts ...QueryOption) ([]Font, error) {
// initialize
if err := cl.init(ctx); err != nil {
return nil, err
}
if cl.cl == nil {
return nil, ErrClientUninitialized
}
// build query
q := NewQuery(family, opts...)
userAgent := cl.userAgent
if q.UserAgent != "" {
userAgent = q.UserAgent
}
// retrieve
return cl.get(ctx, q.String(), userAgent)
}
// All retrieves all common font faces for the specified family by using
// multiple user agents (EOT, SVG, TTF, WOFF2, WOFF).
func (cl *Client) All(ctx context.Context, family string, opts ...QueryOption) ([]Font, error) {
// initialize
if err := cl.init(ctx); err != nil {
return nil, err
}
if cl.cl == nil {
return nil, ErrClientUninitialized
}
// build query
q := NewQuery(family, opts...)
var faces []Font
for _, userAgent := range []string{
UserAgentEOT,
UserAgentSVG,
UserAgentTTF,
UserAgentWOFF2,
UserAgentWOFF,
} {
fonts, err := cl.get(ctx, q.String(), userAgent)
if err != nil {
return nil, err
}
faces = append(faces, fonts...)
}
return faces, nil
}
// Format retrieves a font face with the specified format and family.
func (cl *Client) Format(ctx context.Context, family, format string, opts ...QueryOption) (Font, error) {
// initialize
if err := cl.init(ctx); err != nil {
return Font{}, err
}
if cl.cl == nil {
return Font{}, ErrClientUninitialized
}
var userAgent string
switch format {
case "eot":
userAgent = UserAgentEOT
case "svg":
userAgent = UserAgentSVG
case "ttf":
userAgent = UserAgentTTF
case "woff2":
userAgent = UserAgentWOFF2
case "woff":
userAgent = UserAgentWOFF
default:
return Font{}, ErrFormatNotAvailable
}
// build query
fonts, err := cl.get(ctx, NewQuery(family, opts...).String(), userAgent)
if err != nil {
return Font{}, nil
}
for _, font := range fonts {
if font.Format == format {
return font, nil
}
}
return Font{}, ErrFormatNotAvailable
}
// EOT retrieves the eot font face for the specified family.
func (cl *Client) EOT(ctx context.Context, family string, opts ...QueryOption) (Font, error) {
return cl.Format(ctx, family, "eot", opts...)
}
// SVG retrieves the svg font face for the specified family.
func (cl *Client) SVG(ctx context.Context, family string, opts ...QueryOption) (Font, error) {
return cl.Format(ctx, family, "svg", opts...)
}
// TTF retrieves the ttf font face for the specified family.
func (cl *Client) TTF(ctx context.Context, family string, opts ...QueryOption) (Font, error) {
return cl.Format(ctx, family, "ttf", opts...)
}
// WOFF2 retrieves the woff2 font face for the specified family.
func (cl *Client) WOFF2(ctx context.Context, family string, opts ...QueryOption) (Font, error) {
return cl.Format(ctx, family, "woff2", opts...)
}
// WOFF retrieves the woff font face for the specified family.
func (cl *Client) WOFF(ctx context.Context, family string, opts ...QueryOption) (Font, error) {
return cl.Format(ctx, family, "woff", opts...)
}
// Query wraps a font request.
type Query struct {
Family string
UserAgent string
Variants []string
Subsets []string
Styles []string
Effects []string
Directory string
Display string
Text string
}
// NewQuery builds a new webfont query.
func NewQuery(family string, opts ...QueryOption) *Query {
q := &Query{
Family: family,
}
for _, o := range opts {
o(q)
}
return q
}
// Values returns the url values for the request.
func (q *Query) Values() url.Values {
family := q.Family
if q.Variants != nil {
family += ":" + strings.Join(q.Variants, ",")
}
v := url.Values{
"family": []string{family},
}
if q.Subsets != nil {
v["subset"] = []string{strings.Join(q.Subsets, ",")}
}
if q.Effects != nil {
v["effect"] = []string{strings.Join(q.Effects, "|")}
}
if q.Directory != "" {
v["directory"] = []string{q.Directory}
}
if q.Display != "" {
v["display"] = []string{q.Display}
}
if q.Text != "" {
v["text"] = []string{q.Text}
}
return v
}
// String satisfies the fmt.Stringer interface.
//
// Returns the URL for the request.
func (q *Query) String() string {
return "https://fonts.googleapis.com/css?" + q.Values().Encode()
}
// ClientOption is a webfonts client option.
type ClientOption func(*Client)
// WithTransport is a webfonts client option to set the http transport.
func WithTransport(transport http.RoundTripper) ClientOption {
return func(cl *Client) {
cl.transport = transport
}
}
// WithLogf is a webfonts client option to set a log handler for http requests and
// responses.
func WithLogf(logf interface{}, opts ...httplog.Option) ClientOption {
return func(cl *Client) {
cl.transport = httplog.NewPrefixedRoundTripLogger(cl.transport, logf, opts...)
}
}
// WithAppCacheDir is a webfonts client option to set the app cache dir.
func WithAppCacheDir(appCacheDir string) ClientOption {
return func(cl *Client) {
cl.appCacheDir = appCacheDir
}
}
// WithClientOption is a webfonts client option to set underlying client
// options.
func WithClientOption(opt option.ClientOption) ClientOption {
return func(cl *Client) {
cl.opts = append(cl.opts, opt)
}
}
// WithKey is a webfonts client option to set the google webfonts api key.
func WithKey(key string) ClientOption {
return func(cl *Client) {
cl.key = key
}
}
// WithTokenSource is a webfonts client option to set the token source.
func WithTokenSource(source oauth2.TokenSource) ClientOption {
return func(cl *Client) {
cl.source = source
}
}
// QueryOption is a webfonts query option.
type QueryOption func(*Query)
// WithUserAgent is a query option to set the user agent.
func WithUserAgent(userAgent string) QueryOption {
return func(q *Query) {
q.UserAgent = userAgent
}
}
// WithVariants is a query option to set variants.
func WithVariants(variants ...string) QueryOption {
return func(q *Query) {
q.Variants = variants
}
}
// WithSubsets is a query option to set subsets.
func WithSubsets(subsets ...string) QueryOption {
return func(q *Query) {
q.Subsets = subsets
}
}
// WithStyles is a query option to set styles.
func WithStyles(styles ...string) QueryOption {
return func(q *Query) {
q.Styles = styles
}
}
// WithEffects is a query option to set effects.
func WithEffects(effects ...string) QueryOption {
return func(q *Query) {
q.Effects = effects
}
}
// WithDirectory is a query option to set directory.
func WithDirectory(directory string) QueryOption {
return func(q *Query) {
q.Directory = directory
}
}
// WithDisplay is a query option to set display.
func WithDisplay(display string) QueryOption {
return func(q *Query) {
q.Display = display
}
}
// WithText is a query option to set text.
func WithText(text string) QueryOption {
return func(q *Query) {
q.Text = text
}
}
// User agents.
const (
UserAgentEOT = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)"
UserAgentSVG = "Mozilla/4.0 (iPad; CPU OS 4_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/4.1 Mobile/9A405 Safari/7534.48.3"
UserAgentTTF = "Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) Safari/538.1 Daum/4.1"
UserAgentWOFF2 = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"
UserAgentWOFF = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"
)
// Error is a client error.
type Error string
// Error satisfies the error interface.
func (err Error) Error() string {
return string(err)
}
// Errors.
const (
ErrServiceUninitialized Error = "service uninitialized"
ErrClientUninitialized Error = "client uninitialized"
ErrStatusNotOK Error = "status not ok"
ErrFormatNotAvailable Error = "format not available"
)