-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoke.go
76 lines (63 loc) · 1.42 KB
/
joke.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
package concurrencystuff
import (
"encoding/json"
"fmt"
"net/http"
)
// JokeService contains joke implementations
type JokeService interface {
GetJoke() (interface{}, error)
FuckOffAsshole(name string) (interface{}, error)
}
// Svc contains service url
type svc struct {
yomamaURL string
foaasURL string
}
// New initializes new service
func New(yomama, foaas string) JokeService {
return &svc{
yomamaURL: yomama,
foaasURL: foaas,
}
}
// GetJoke gets a joke from the yo mama api
func (s *svc) GetJoke() (interface{}, error) {
resp, err := http.Get(s.yomamaURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
j := struct {
Joke string `json:"joke,omitempty"`
}{}
err = json.NewDecoder(resp.Body).Decode(&j)
if err != nil {
return nil, err
}
return j, nil
}
// FuckOff gets content from FOAAS (Fuck off as a service)
func (s *svc) FuckOffAsshole(name string) (interface{}, error) {
client := &http.Client{}
url := fmt.Sprintf("%s/asshole/%s", s.foaasURL, name)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
m := struct {
Message string `json:"message,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
}{}
err = json.NewDecoder(resp.Body).Decode(&m)
if err != nil {
return nil, err
}
return m, nil
}