-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.go
48 lines (41 loc) · 1.13 KB
/
api.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
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
const (
urbanDictionaryAPIURI = "https://api.urbandictionary.com/v0"
)
// Response describes the data structure that comes back from the
// Urban Dictionary API.
type Response struct {
Results []Result `json:"list"`
Tags []string `json:"tags"`
Type string `json:"result_type"`
}
// Result holds the information for a given definition.
type Result struct {
ID int64 `json:"defid"`
Author string `json:"author"`
Definition string `json:"definition"`
Link string `json:"permalink"`
ThumbsDown int64 `json:"thumbs_down"`
ThumbsUp int64 `json:"thumbs_up"`
Word string `json:"word"`
}
// Define returns the definitions from Urban Dictionary for a given word.
func Define(word string) (response *Response, err error) {
endpoint := fmt.Sprintf("%s/define?page=%d&term=%s", urbanDictionaryAPIURI, 1, url.QueryEscape(word))
resp, err := http.Get(endpoint)
if err != nil {
return
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
if err = dec.Decode(&response); err != nil {
return nil, err
}
return response, nil
}