diff --git a/cmd/gf/internal/consts/consts_gen_ctrl_template_sdk.go b/cmd/gf/internal/consts/consts_gen_ctrl_template_sdk.go index 92177fbf2fc..09f3795e393 100644 --- a/cmd/gf/internal/consts/consts_gen_ctrl_template_sdk.go +++ b/cmd/gf/internal/consts/consts_gen_ctrl_template_sdk.go @@ -26,12 +26,6 @@ type implementer struct { } func New(config httpclient.Config) IClient { - if !gstr.HasPrefix(config.URL, "http") { - config.URL = fmt.Sprintf("http://%s", config.URL) - } - if config.Logger == nil { - config.Logger = g.Log() - } return &implementer{ config: config, } diff --git a/contrib/sdk/httpclient/httpclient.go b/contrib/sdk/httpclient/httpclient.go index d8117335b4c..99655679a8c 100644 --- a/contrib/sdk/httpclient/httpclient.go +++ b/contrib/sdk/httpclient/httpclient.go @@ -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" @@ -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. @@ -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, } } diff --git a/contrib/sdk/httpclient/httpclient_config.go b/contrib/sdk/httpclient/httpclient_config.go index 8b3f8f67fcf..eccb45323fb 100644 --- a/contrib/sdk/httpclient/httpclient_config.go +++ b/contrib/sdk/httpclient/httpclient_config.go @@ -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. } diff --git a/contrib/sdk/httpclient/handler.go b/contrib/sdk/httpclient/httpclient_handler.go similarity index 71% rename from contrib/sdk/httpclient/handler.go rename to contrib/sdk/httpclient/httpclient_handler.go index 1f598dcee00..848233a9d30 100644 --- a/contrib/sdk/httpclient/handler.go +++ b/contrib/sdk/httpclient/httpclient_handler.go @@ -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 ( @@ -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 }