-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
up: net - update and add some http client util functions
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package httpreq | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/gookit/goutil/netutil/httpctype" | ||
) | ||
|
||
// Resp struct | ||
type Resp struct { | ||
*http.Response | ||
// CostTime for a request-response | ||
CostTime int64 | ||
} | ||
|
||
// NewResp instance | ||
func NewResp(hr *http.Response) *Resp { | ||
return &Resp{Response: hr} | ||
} | ||
|
||
// IsFail check | ||
func (r *Resp) IsFail() bool { | ||
return r.StatusCode != http.StatusOK | ||
} | ||
|
||
// IsOk check | ||
func (r *Resp) IsOk() bool { | ||
return r.StatusCode == http.StatusOK | ||
} | ||
|
||
// IsSuccessful check | ||
func (r *Resp) IsSuccessful() bool { | ||
return IsSuccessful(r.StatusCode) | ||
} | ||
|
||
// IsEmptyBody check response body is empty | ||
func (r *Resp) IsEmptyBody() bool { | ||
return r.ContentLength <= 0 | ||
} | ||
|
||
// ContentType get response content type | ||
func (r *Resp) ContentType() string { | ||
return r.Header.Get(httpctype.Key) | ||
} | ||
|
||
// BodyString get body as string. | ||
func (r *Resp) String() string { | ||
return ResponseToString(r.Response) | ||
} | ||
|
||
// BodyString get body as string. | ||
func (r *Resp) BodyString() string { | ||
return r.BodyBuffer().String() | ||
} | ||
|
||
// BodyBuffer read body to buffer. | ||
// | ||
// NOTICE: must close resp body. | ||
func (r *Resp) BodyBuffer() *bytes.Buffer { | ||
buf := &bytes.Buffer{} | ||
// prof: assign memory before read | ||
if r.ContentLength > bytes.MinRead { | ||
buf.Grow(int(r.ContentLength) + 2) | ||
} | ||
|
||
// NOTICE: must close resp body. | ||
defer r.QuiteCloseBody() | ||
_, err := buf.ReadFrom(r.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return buf | ||
} | ||
|
||
// BindJsonOnOk body data on status is 200 | ||
// | ||
// NOTICE: must close resp body. | ||
func (r *Resp) BindJsonOnOk(ptr any) error { | ||
// NOTICE: must close resp body. | ||
defer r.QuiteCloseBody() | ||
|
||
if r.IsFail() { | ||
_, _ = io.Copy(io.Discard, r.Body) // <-- add this line | ||
return errors.New("response status is not equals to 200") | ||
} | ||
|
||
if ptr == nil { | ||
_, _ = io.Copy(io.Discard, r.Body) // <-- add this line | ||
return nil | ||
} | ||
return json.NewDecoder(r.Body).Decode(ptr) | ||
} | ||
|
||
// BindJson body data to a ptr | ||
// | ||
// NOTICE: must close resp body. | ||
func (r *Resp) BindJson(ptr any) error { | ||
// NOTICE: must close resp body. | ||
defer r.QuiteCloseBody() | ||
|
||
if ptr == nil { | ||
_, _ = io.Copy(io.Discard, r.Body) // <-- add this line | ||
return nil | ||
} | ||
|
||
return json.NewDecoder(r.Body).Decode(ptr) | ||
} | ||
|
||
// CloseBody close resp body | ||
func (r *Resp) CloseBody() error { | ||
return r.Body.Close() | ||
} | ||
|
||
// QuiteCloseBody close resp body, ignore error | ||
func (r *Resp) QuiteCloseBody() { | ||
_ = r.Body.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters