Skip to content

Commit

Permalink
fix: Resolve pr comments. Fixes gogf#3539
Browse files Browse the repository at this point in the history
Signed-off-by: oninowang <[email protected]>
  • Loading branch information
oninowang committed Apr 25, 2024
1 parent f5b610a commit 6df5f7b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 0 additions & 6 deletions cmd/gf/internal/consts/consts_gen_ctrl_template_sdk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions contrib/sdk/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ package httpclient

import (
"context"
"fmt"
"net/http"

"github.com/gogf/gf/v2/encoding/gurl"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/text/gregex"
Expand All @@ -25,7 +27,7 @@ import (
// Client is an http client for SDK.
type Client struct {
*gclient.Client
IHandler
Handler
}

// New creates and returns an http client for SDK.
Expand All @@ -34,13 +36,19 @@ func New(config Config) *Client {
if client == nil {
client = gclient.New()
}
if config.Logger == nil {
config.Logger = g.Log()
}
handler := config.Handler
if handler == nil {
handler = NewDefaultHandler(config)
}
if !gstr.HasPrefix(config.URL, "http") {
config.URL = fmt.Sprintf("http://%s", config.URL)
}
return &Client{
Client: client.Prefix(config.URL),
IHandler: handler,
Client: client.Prefix(config.URL),
Handler: handler,
}
}

Expand Down
2 changes: 1 addition & 1 deletion contrib/sdk/httpclient/httpclient_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Config struct {
URL string `v:"required"` // Service address. Eg: user.svc.local, http://user.svc.local
Client *gclient.Client // Custom underlying client.
Handler IHandler // Custom response handler.
Handler Handler // Custom response handler.
Logger *glog.Logger // Custom logger.
RawDump bool // Whether auto dump request&response in stdout.
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 httpclient

import (
Expand All @@ -11,7 +17,10 @@ import (
"github.com/gogf/gf/v2/os/glog"
)

type IHandler interface {
// Handler is the interface for http response handling.
type Handler interface {
// HandleResponse handles the http response and transforms its body to the specified object.
// The parameter `out` specifies the object that the response body is transformed to.
HandleResponse(ctx context.Context, res *gclient.Response, out interface{}) error
}

Expand Down

0 comments on commit 6df5f7b

Please sign in to comment.