mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
This PR includes the following changes: - **Upgrade `.golangci.yml`**: Updated the configuration file to align with the latest golangci-lint version, ensuring compatibility and leveraging new features. - **Refactor GitHub Action workflow**: Modified `golangci-lint.yml` in the GitHub Actions workflow to reflect the updated configuration and improve CI performance. - **Codebase optimization**: Refactored code to address issues and warnings raised by the updated golangci-lint rules, including: - Improved function length and complexity. - Enhanced error handling and variable naming conventions. - Fixed minor issues such as unused imports and formatting inconsistencies. These changes aim to maintain code quality, ensure compatibility with the latest tools, and improve overall maintainability.
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gclient
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
|
)
|
|
|
|
// Response is the struct for client request response.
|
|
type Response struct {
|
|
*http.Response // Response is the underlying http.Response object of certain request.
|
|
request *http.Request // Request is the underlying http.Request object of certain request.
|
|
requestBody []byte // The body bytes of certain request, only available in Dump feature.
|
|
cookies map[string]string // Response cookies, which are only parsed once.
|
|
}
|
|
|
|
// initCookie initializes the cookie map attribute of Response.
|
|
func (r *Response) initCookie() {
|
|
if r.cookies == nil {
|
|
r.cookies = make(map[string]string)
|
|
// Response might be nil.
|
|
if r.Response != nil {
|
|
for _, v := range r.Cookies() {
|
|
r.cookies[v.Name] = v.Value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetCookie retrieves and returns the cookie value of specified `key`.
|
|
func (r *Response) GetCookie(key string) string {
|
|
r.initCookie()
|
|
return r.cookies[key]
|
|
}
|
|
|
|
// GetCookieMap retrieves and returns a copy of current cookie values map.
|
|
func (r *Response) GetCookieMap() map[string]string {
|
|
r.initCookie()
|
|
m := make(map[string]string, len(r.cookies))
|
|
for k, v := range r.cookies {
|
|
m[k] = v
|
|
}
|
|
return m
|
|
}
|
|
|
|
// ReadAll retrieves and returns the response content as []byte.
|
|
func (r *Response) ReadAll() []byte {
|
|
// Response might be nil.
|
|
if r == nil || r.Response == nil {
|
|
return []byte{}
|
|
}
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
intlog.Errorf(r.request.Context(), `%+v`, err)
|
|
return nil
|
|
}
|
|
return body
|
|
}
|
|
|
|
// ReadAllString retrieves and returns the response content as string.
|
|
func (r *Response) ReadAllString() string {
|
|
return string(r.ReadAll())
|
|
}
|
|
|
|
// SetBodyContent overwrites response content with custom one.
|
|
func (r *Response) SetBodyContent(content []byte) {
|
|
buffer := bytes.NewBuffer(content)
|
|
r.Body = io.NopCloser(buffer)
|
|
r.ContentLength = int64(buffer.Len())
|
|
}
|
|
|
|
// Close closes the response when it will never be used.
|
|
func (r *Response) Close() error {
|
|
if r == nil || r.Response == nil {
|
|
return nil
|
|
}
|
|
return r.Body.Close()
|
|
}
|