-
Notifications
You must be signed in to change notification settings - Fork 1
/
moviesquotes.go
56 lines (45 loc) · 939 Bytes
/
moviesquotes.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
package main
import (
"encoding/json"
"github.com/parnurzeal/gorequest"
)
type MoviesQuotes interface {
Get() (Quote, error)
}
type Quote struct {
Text string
Author string
}
func NewMoviesQuotes(key string) MoviesQuotes {
return &RandomFamousQuotes{
URL: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies",
Key: key,
}
}
type RandomFamousQuotes struct {
URL string
Key string
}
type RandomFamousQuotesResponse struct {
Quote string `json:"quote"`
Author string `json:"author"`
Category string `json:"category"`
}
func (r *RandomFamousQuotes) Get() (Quote, error) {
q := Quote{}
_, body, errs := gorequest.New().
Get(r.URL).
Set("X-Mashape-Key", r.Key).
End()
if errs != nil {
return q, errs[0]
}
resp := RandomFamousQuotesResponse{}
err := json.Unmarshal([]byte(body), &resp)
if err != nil {
return q, err
}
q.Author = resp.Author
q.Text = resp.Quote
return q, nil
}