Skip to content

Commit

Permalink
add logo
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Nov 17, 2023
1 parent 581c1b2 commit c549714
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 42 deletions.
27 changes: 8 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
# Hasaki
<div align="center">
<h1>Hasaki</h1>
<h5>HTTP Request Library for Go</h5>
<img src="assets/logo.png" alt="logo" width="300px">
</div>

HTTP Request Library for Go
___

[![Build Status][1]][2] [![codecov][3]][4]

[1]: https://github.com/lxzan/hasaki/workflows/Go%20Test/badge.svg?branch=master
[2]: https://github.com/lxzan/hasaki/actions?query=branch%3Amaster
[3]: https://codecov.io/gh/lxzan/hasaki/graph/badge.svg?token=0VY55RLS3G
[4]: https://codecov.io/gh/lxzan/hasaki

- [Hasaki](#hasaki)
- [Features](#features)
- [Install](#install)
- [Usage](#usage)
- [Get](#get)
- [Post](#post)
- [Stream](#stream)
- [Error Stack](#error-stack)
- [Middleware](#middleware)
[![go-test](https://github.com/lxzan/hasaki/workflows/Go%20Test/badge.svg?branch=master)](https://github.com/lxzan/hasaki/actions?query=branch%3Amaster) [![codecov](https://codecov.io/gh/lxzan/hasaki/graph/badge.svg?token=0VY55RLS3G)](https://codecov.io/gh/lxzan/hasaki)

### Features

- [x] Buffer Pool
- [x] Trace the Error Stack
- [x] Build-In JSON / XML / WWWForm / Protobuf/YAML Codec
- [x] Build-In JSON / XML / WWWForm / Protobuf / YAML Codec
- [x] Request Before and After Middleware
- [x] Export cURL Command in Debug Mode

Expand Down
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestRequest_ReadBody(t *testing.T) {
var cli, _ = NewClient(WithReuseBody())
var resp = cli.Get("http://%s/greet", addr).Send(nil)
assert.NoError(t, resp.Err())
_, ok := resp.Body.(BytesReader)
_, ok := resp.Body.(BytesReadCloser)
assert.True(t, ok)
_, err := resp.ReadBody()
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func WithHTTPClient(client *http.Client) Option {
}
}

// WithReuseBody 开启body复用; Response.Body可以被断言为BytesReader, 重复调用Bytes()方法.
// Enable body reuse; Response.Body can be asserted as a BytesReader, calling the Bytes() method repeatedly.
// WithReuseBody 开启body复用; Response.Body可以被断言为BytesReadCloser, 重复调用Bytes()方法.
// Enable body reuse; Response.Body can be asserted as a BytesReadCloser, calling the Bytes() method repeatedly.
func WithReuseBody() Option {
return func(c *config) {
c.ReuseBodyEnabled = true
Expand Down Expand Up @@ -111,7 +111,7 @@ func withInitialize() Option {
}
}

type BytesReader interface {
io.Reader
type BytesReadCloser interface {
io.ReadCloser
Bytes() []byte
}
2 changes: 1 addition & 1 deletion contrib/pb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Decode(r io.Reader, v any) error {
if !ok {
return errors.WithStack(errDataType)
}
if br, ok := r.(hasaki.BytesReader); ok {
if br, ok := r.(hasaki.BytesReadCloser); ok {
return errors.WithStack(proto.Unmarshal(br.Bytes(), message))
}
var w = bytebufferpool.Get()
Expand Down
32 changes: 16 additions & 16 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,22 @@ func (c *Request) SetQuery(query any) *Request {
// Send 发送请求
// Send http request
func (c *Request) Send(body any) *Response {
response := &Response{ctx: c.ctx}
resp := &Response{ctx: c.ctx}
if c.err != nil {
response.err = c.err
return response
resp.err = c.err
return resp
}

reader, err := c.encoder.Encode(body)
if err != nil {
response.err = err
return response
resp.err = err
return resp
}

req, err := http.NewRequestWithContext(c.ctx, c.method, c.url, reader)
if err != nil {
response.err = errors.WithStack(err)
return response
resp.err = errors.WithStack(err)
return resp
}

if c.method == http.MethodGet && body == nil {
Expand All @@ -169,8 +169,8 @@ func (c *Request) Send(body any) *Response {
req.Header = c.headers

// 执行请求前中间件
if response.ctx, response.err = c.before(c.ctx, req); response.err != nil {
return response
if resp.ctx, resp.err = c.before(c.ctx, req); resp.err != nil {
return resp
}

// 打印CURL命令
Expand All @@ -179,21 +179,21 @@ func (c *Request) Send(body any) *Response {
}

// 发起请求
if response.Response, err = c.client.Do(req); err != nil {
response.err = errors.WithStack(err)
return response
if resp.Response, err = c.client.Do(req); err != nil {
resp.err = errors.WithStack(err)
return resp
}

// 预先读取body, 可复用
if c.reuseBodyEnabled {
if response.err = c.readBody(response); response.err != nil {
return response
if resp.err = c.readBody(resp); resp.err != nil {
return resp
}
}

// 执行请求后中间件
response.ctx, response.err = c.after(response.ctx, response.Response)
return response
resp.ctx, resp.err = c.after(resp.ctx, resp.Response)
return resp
}

func (c *Request) readBody(resp *Response) error {
Expand Down
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *Response) ReadBody() ([]byte, error) {
if c.Response == nil || c.Body == nil {
return nil, errors.WithStack(errEmptyResponse)
}
if v, ok := c.Body.(BytesReader); ok {
if v, ok := c.Body.(BytesReadCloser); ok {
return v.Bytes(), nil
}
b, err := io.ReadAll(c.Body)
Expand Down

0 comments on commit c549714

Please sign in to comment.