@@ -16,9 +16,11 @@ http request library for golang
16
16
- [ Features] ( #features )
17
17
- [ Install] ( #install )
18
18
- [ Usage] ( #usage )
19
- - [ GET] ( #get )
20
- - [ POST] ( #post )
21
- - [ Middleware] ( #middleware )
19
+ - [ Get] ( #get )
20
+ - [ Post] ( #post )
21
+ - [ Stream] ( #stream )
22
+ - [ Error Stack] ( #error-stack )
23
+ - [ Middleware] ( #middleware )
22
24
23
25
### Features
24
26
@@ -35,98 +37,126 @@ go get -v github.com/lxzan/hasaki
35
37
36
38
### Usage
37
39
38
- ##### GET
40
+ #### Get
39
41
40
- ``` go
41
- package main
42
-
43
- import (
44
- " log"
45
-
46
- " github.com/lxzan/hasaki"
47
- )
48
-
49
- func main () {
50
- type Query struct {
51
- Q string ` form:"q"`
52
- Page int ` form:"page"`
53
- Order string ` form:"-"`
54
- }
55
- var out = make (map [string ]any)
56
- var err = hasaki.
57
- Get (" https://api.github.com/search/repositories" ).
58
- SetQuery (Query{
59
- Q: " go-ws" ,
60
- Page: 1 ,
61
- }).
62
- Send (nil ).
63
- BindJSON (out)
64
- if err != nil {
65
- log.Printf (" %+v " , err)
66
- }
42
+ ```
43
+ // GET https://api.example.com/search
44
+ // Send get request with path parameters
45
+
46
+ resp := hasaki.
47
+ Get("https://api.example.com/%s", "search").
48
+ Send(nil)
49
+ ```
50
+
51
+ ```
52
+ // GET https://api.example.com/search?q=hasaki&page=1
53
+ // Send get request, with Query parameter, encoded with url.Values
54
+
55
+ resp := hasaki.
56
+ Get("https://api.example.com/search").
57
+ SetQuery(url.Values{
58
+ "q": []string{"hasaki"},
59
+ "page": []string{"1"},
60
+ }).
61
+ Send(nil)
62
+ ```
63
+
64
+ ```
65
+ // GET https://api.example.com/search?q=hasaki&page=1
66
+ // Send get request, with Query parameter, encoded with struct
67
+
68
+ type Req struct {
69
+ Q string `form:"q"`
70
+ Page int `form:"page"`
67
71
}
72
+ resp := hasaki.
73
+ Get("https://api.example.com/search").
74
+ SetQuery(Req{
75
+ Q: "hasaki",
76
+ Page: 1,
77
+ }).
78
+ Send(nil)
79
+ ```
80
+
81
+ #### Post
68
82
69
83
```
84
+ // POST https://api.example.com/search
85
+ // Send post request, encoded with json
70
86
71
- ##### POST
87
+ type Req struct {
88
+ Q string `json:"q"`
89
+ Page int `json:"page"`
90
+ }
91
+ resp := hasaki.
92
+ Post("https://api.example.com/search").
93
+ Send(Req{
94
+ Q: "hasaki",
95
+ Page: 1,
96
+ })
97
+ ```
72
98
73
- ``` go
74
- package main
75
-
76
- import (
77
- " log"
78
-
79
- " github.com/lxzan/hasaki"
80
- )
81
-
82
- func main () {
83
- type Query struct {
84
- Q string ` form:"q"`
85
- Page int ` form:"page"`
86
- Order string ` form:"-"`
87
- }
88
- var out = make (map [string ]any)
89
- var err = hasaki.
90
- Post (" https://api.github.com/search/repositories" ).
91
- SetEncoder (hasaki.FormEncoder ).
92
- Send (Query{
93
- Q: " go-ws" ,
94
- Page: 1 ,
95
- }).
96
- BindJSON (&out)
97
- if err != nil {
98
- log.Printf (" %+v " , err)
99
- }
99
+ ```
100
+ // POST https://api.example.com/search
101
+ // Send post request, encoded with www-form
102
+
103
+ type Req struct {
104
+ Q string `form:"q"`
105
+ Page int `form:"page"`
100
106
}
107
+ resp := hasaki.
108
+ Post("https://api.example.com/search").
109
+ SetEncoder(hasaki.FormEncoder).
110
+ Send(Req{
111
+ Q: "hasaki",
112
+ Page: 1,
113
+ })
101
114
```
102
115
103
- ### Middleware
116
+ #### Stream
104
117
105
- ``` go
106
- package main
107
-
108
- import (
109
- " context"
110
- " log"
111
- " net/http"
112
- " time"
113
-
114
- " github.com/lxzan/hasaki"
115
- )
116
-
117
- func main () {
118
- before := hasaki.WithBefore (func (ctx context.Context , request *http.Request ) (context.Context , error ) {
119
- return context.WithValue (ctx, " t0" , time.Now ()), nil
120
- })
121
-
122
- after := hasaki.WithAfter (func (ctx context.Context , response *http.Response ) (context.Context , error ) {
123
- t0 := ctx.Value (" t0" ).(time.Time )
124
- log.Printf (" latency=%s " , time.Since (t0).String ())
125
- return ctx, nil
126
- })
127
-
128
- var url = " https://api.github.com/search/repositories"
129
- cli , _ := hasaki.NewClient (before, after)
130
- cli.Get (url).Send (nil )
118
+ ```
119
+ // POST https://api.example.com/upload
120
+ // Send a put request, using a byte stream
121
+
122
+ var reader io.Reader
123
+ encoder := hasaki.NewStreamEncoder(hasaki.MimeSTREAM)
124
+ resp := hasaki.
125
+ Put("https://api.example.com/upload").
126
+ SetEncoder(encoder).
127
+ Send(reader)
128
+ ```
129
+
130
+ #### Error Stack
131
+
132
+ ```
133
+ // Print the error stack
134
+ data := make(map[string]any)
135
+ err := hasaki.
136
+ Post("https://api.example.com/upload").
137
+ Send(nil).
138
+ BindJSON(&data)
139
+ if err != nil {
140
+ log.Printf("%+v", err)
131
141
}
142
+ ```
143
+
144
+ #### Middleware
145
+
146
+ ``` go
147
+ // Statistics on time spent on requests
148
+
149
+ before := hasaki.WithBefore (func (ctx context .Context , request *http .Request ) (context .Context , error ) {
150
+ return context.WithValue (ctx, " t0" , time.Now ()), nil
151
+ })
152
+
153
+ after := hasaki.WithAfter (func (ctx context .Context , response *http .Response ) (context .Context , error ) {
154
+ t0 := ctx.Value (" t0" ).(time.Time )
155
+ log.Printf (" latency=%s " , time.Since (t0).String ())
156
+ return ctx, nil
157
+ })
158
+
159
+ var url = " https://api.github.com/search/repositories"
160
+ cli , _ := hasaki.NewClient (before, after)
161
+ cli.Get (url).Send (nil )
132
162
```
0 commit comments