|
1 | 1 | # Hasaki |
| 2 | + |
2 | 3 | http request library for golang |
3 | 4 |
|
4 | | -[](https://github.com/lxzan/hasaki/actions?query=branch%3Amaster) |
5 | | -[](https://www.oscs1024.com/project/lxzan/hasaki?ref=badge_small) |
| 5 | +[![Build Status][1]][2] [![codecov][3]][4] |
6 | 6 |
|
7 | | -- Package |
8 | | -```bash |
9 | | -github.com/lxzan/hasaki |
10 | | -``` |
| 7 | +[1]: https://github.com/lxzan/hasaki/workflows/Go%20Test/badge.svg?branch=master |
11 | 8 |
|
12 | | -### 基本使用 |
13 | | -#### 发送GET请求 |
14 | | -```go |
15 | | -package main |
| 9 | +[2]: https://github.com/lxzan/hasaki/actions?query=branch%3Amaster |
16 | 10 |
|
17 | | -import ( |
18 | | - "github.com/lxzan/hasaki" |
19 | | -) |
| 11 | +[3]: https://codecov.io/gh/lxzan/hasaki/graph/badge.svg?token=0VY55RLS3G |
20 | 12 |
|
21 | | -func main() { |
22 | | - result := make([]map[string]interface{}, 0) |
23 | | - err := hasaki. |
24 | | - Get("https://api.github.com/users/%s/repos", "lxzan"). |
25 | | - SetQuery(hasaki.Any{ |
26 | | - "type": "go", |
27 | | - "per_page": 1, |
28 | | - }). |
29 | | - Send(nil). |
30 | | - BindJSON(&result) |
31 | | -} |
32 | | -``` |
| 13 | +[4]: https://codecov.io/gh/lxzan/hasaki |
33 | 14 |
|
34 | | -#### 发送POST JSON请求 |
35 | | -```go |
36 | | -func main() { |
37 | | - var body = struct { |
38 | | - Name string `json:"name"` |
39 | | - }{Name: "caster"} |
40 | | - |
41 | | - err := hasaki. |
42 | | - Post("http://localhost:8080" + "/sendJson"). |
43 | | - SetEncoder(hasaki.JsonEncoder). |
44 | | - Send(body). |
45 | | - Err() |
46 | | -} |
47 | | -``` |
| 15 | +- [Hasaki](#hasaki) |
| 16 | + - [Features](#features) |
| 17 | + - [Install](#install) |
| 18 | + - [Usage](#usage) |
| 19 | + - [GET](#get) |
| 20 | + - [POST](#post) |
| 21 | + - [Middleware](#middleware) |
48 | 22 |
|
49 | | -#### 发送POST Form请求 |
50 | | -```go |
51 | | -func main() { |
52 | | - var body = struct { |
53 | | - Name string `form:"name"` |
54 | | - }{Name: "caster"} |
| 23 | +### Features |
55 | 24 |
|
56 | | - // Content-Type: application/x-www-form-urlencoded |
57 | | - err := hasaki. |
58 | | - Post("http://localhost:8080" + "/sendForm"). |
59 | | - SetEncoder(hasaki.FormEncoder). |
60 | | - Send(body). |
61 | | - Err() |
62 | | -} |
| 25 | +- [x] Buffer Pool |
| 26 | +- [x] Trace the Error Stack |
| 27 | +- [x] JSON / WWWForm Encoder |
| 28 | +- [x] Request Before and After Middleware |
| 29 | + |
| 30 | +### Install |
| 31 | + |
| 32 | +```bash |
| 33 | +go get -v github.com/lxzan/hasaki |
63 | 34 | ``` |
64 | 35 |
|
65 | | -#### 发送字节流 |
| 36 | +### Usage |
| 37 | + |
| 38 | +##### GET |
| 39 | + |
66 | 40 | ```go |
67 | 41 | package main |
68 | 42 |
|
69 | 43 | import ( |
| 44 | + "log" |
| 45 | + |
70 | 46 | "github.com/lxzan/hasaki" |
71 | | - "os" |
72 | 47 | ) |
73 | 48 |
|
74 | 49 | func main() { |
75 | | - file, _ := os.Open("") |
76 | | - err := hasaki. |
77 | | - Put("http://localhost:8080" + "/upload"). |
78 | | - SetHeader(hasaki.H{"Content-Type": hasaki.ContentTypeSTREAM}). |
79 | | - Send(file). |
80 | | - Err() |
| 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 | + } |
81 | 67 | } |
82 | | -``` |
83 | | - |
84 | | -### 高级 |
85 | 68 |
|
86 | | -#### 设置代理 |
87 | | -```go |
88 | | -cli, err := hasaki.NewClient(hasaki.WithProxy("socks5://127.0.0.1:1080")) |
89 | 69 | ``` |
90 | 70 |
|
91 | | -#### 统一的错误处理 |
| 71 | +##### POST |
| 72 | + |
92 | 73 | ```go |
93 | 74 | package main |
94 | 75 |
|
95 | 76 | import ( |
96 | | - "context" |
97 | | - "fmt" |
98 | | - jsoniter "github.com/json-iterator/go" |
| 77 | + "log" |
| 78 | + |
99 | 79 | "github.com/lxzan/hasaki" |
100 | | - "github.com/pkg/errors" |
101 | | - "mime" |
102 | | - "net/http" |
103 | 80 | ) |
104 | 81 |
|
105 | | -type BaseResult struct { |
106 | | - Code *int `json:"code"` |
107 | | - Message string `json:"message"` |
108 | | -} |
109 | | - |
110 | | -var afterFunc = func(ctx context.Context, resp *http.Response) (context.Context, error) { |
111 | | - if resp.StatusCode != http.StatusOK { |
112 | | - return ctx, hasaki.ErrUnexpectedStatusCode |
113 | | - } |
114 | | - typ, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type")) |
115 | | - if typ != "application/json" { |
116 | | - return ctx, nil |
| 82 | +func main() { |
| 83 | + type Query struct { |
| 84 | + Q string `form:"q"` |
| 85 | + Page int `form:"page"` |
| 86 | + Order string `form:"-"` |
117 | 87 | } |
118 | | - |
119 | | - rc, err := hasaki.NewReadCloser(resp.Body) |
| 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) |
120 | 97 | if err != nil { |
121 | | - return ctx, err |
122 | | - } |
123 | | - var result = BaseResult{} |
124 | | - if err := jsoniter.Unmarshal(rc.Bytes(), &result); err != nil { |
125 | | - return ctx, err |
126 | | - } |
127 | | - if result.Code == nil || *result.Code != 0 { |
128 | | - return ctx, errors.New(result.Message) |
| 98 | + log.Printf("%+v", err) |
129 | 99 | } |
130 | | - resp.Body = rc |
131 | | - return ctx, nil |
132 | | -} |
133 | | - |
134 | | -func main() { |
135 | | - cli, _ := hasaki.NewClient(hasaki.WithAfter(afterFunc)) |
136 | 100 | } |
137 | 101 | ``` |
138 | 102 |
|
139 | | -#### 统计请求耗时 |
| 103 | +### Middleware |
| 104 | + |
140 | 105 | ```go |
141 | 106 | package main |
142 | 107 |
|
143 | 108 | import ( |
144 | 109 | "context" |
145 | | - "fmt" |
146 | | - "github.com/lxzan/hasaki" |
| 110 | + "log" |
147 | 111 | "net/http" |
148 | 112 | "time" |
| 113 | + |
| 114 | + "github.com/lxzan/hasaki" |
149 | 115 | ) |
150 | 116 |
|
151 | 117 | func main() { |
152 | | - cli, _ := hasaki.NewClient( |
153 | | - hasaki.WithBefore(func(ctx context.Context, request *http.Request) (context.Context, error) { |
154 | | - ctx = context.WithValue(ctx, "t0", time.Now()) |
155 | | - return ctx, nil |
156 | | - }), |
157 | | - hasaki.WithAfter(func(ctx context.Context, response *http.Response) (context.Context, error) { |
158 | | - t0 := ctx.Value("t0").(time.Time) |
159 | | - fmt.Printf("cost = %dms\n", time.Since(t0).Milliseconds()) |
160 | | - return ctx, nil |
161 | | - }), |
162 | | - ) |
163 | | - cli.Get("https://api.github.com/users/%s", "lxzan").Send(nil) |
| 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) |
164 | 131 | } |
165 | 132 | ``` |
0 commit comments