-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
113 lines (89 loc) · 2.57 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/gojek/heimdall/v7/httpclient"
"github.com/gojek/heimdall/v7/plugins"
)
func main() {
Sample("Do", Do)
Sample("Get", Get)
Sample("Post", Post)
Sample("Put", Put)
Sample("Delete", Delete)
Sample("Logger", Logger)
}
func Do() {
timeout := 1000 * time.Millisecond
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))
req, _ := http.NewRequest(http.MethodGet, "https://run.mocky.io/v3/41ac0f88-ff00-4fb5-a7b7-a749781c3260", nil)
res, err := client.Do(req)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
func Get() {
timeout := 1000 * time.Millisecond
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))
res, err := client.Get("https://run.mocky.io/v3/41ac0f88-ff00-4fb5-a7b7-a749781c3260", nil)
if err != nil{
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
func Post() {
timeout := 1000 * time.Millisecond
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))
payload := bytes.NewReader([]byte("{ \"name\": \"gopher\" }"))
res, err := client.Post("https://run.mocky.io/v3/41ac0f88-ff00-4fb5-a7b7-a749781c3260", payload, nil)
if err != nil{
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
func Put() {
timeout := 1000 * time.Millisecond
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))
payload := bytes.NewReader([]byte("{ \"name\": \"gopher\" }"))
res, err := client.Put("https://run.mocky.io/v3/41ac0f88-ff00-4fb5-a7b7-a749781c3260", payload, nil)
if err != nil{
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
func Delete() {
timeout := 1000 * time.Millisecond
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))
res, err := client.Delete("https://run.mocky.io/v3/41ac0f88-ff00-4fb5-a7b7-a749781c3260", nil)
if err != nil{
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
func Logger() {
timeout := 1000 * time.Millisecond
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))
requestLogger := plugins.NewRequestLogger(nil, nil)
client.AddPlugin(requestLogger)
req, _ := http.NewRequest(http.MethodGet, "https://run.mocky.io/v3/41ac0f88-ff00-4fb5-a7b7-a749781c3260", nil)
res, err := client.Do(req)
if err != nil {
fmt.Println("error:", err)
}
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
func Sample(name string, fn func()) {
fmt.Println(">", name)
fn()
fmt.Println()
}