-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
53 lines (44 loc) · 1.21 KB
/
api.go
File metadata and controls
53 lines (44 loc) · 1.21 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
package mwgoapi
import (
"fmt"
"io/ioutil"
"net/http"
)
// Handler is the handler for the API.
type Handler struct {
Client *http.Client
BaseURL string
Key string
}
const (
// BaseURL is the base URL for the API.
BaseURL = "https://www.dictionaryapi.com/api/v3/references/collegiate/json"
AudioFormat = "mp3"
AudioBaseURL = "https://media.merriam-webster.com/audio/prons/en/us"
)
// NewClient returns a new Handler.
func NewClient(client *http.Client, url, key string) *Handler {
if url == "" {
url = BaseURL
}
return &Handler{Client: client, BaseURL: url, Key: key}
}
// Get returns the response for the given word.
func (a *Handler) Get(word string) ([]byte, error) {
res, err := a.Client.Get(fmt.Sprintf("%s/%s?key=%s", a.BaseURL, word, a.Key))
if err != nil {
return nil, fmt.Errorf("could not send request, %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status code %d", res.StatusCode)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("could not read body, err: %w", err)
}
if string(b) == "Invalid API key. Not subscribed for this reference." {
return nil, fmt.Errorf("invalid API key")
}
return b, nil
}