-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib/sdk/httpclient: add custom response handler support, fixe gog…
- Loading branch information
Showing
5 changed files
with
195 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,68 @@ | ||
// 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 ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/gogf/gf/v2/errors/gcode" | ||
"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/os/glog" | ||
) | ||
|
||
// 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 | ||
} | ||
|
||
// DefaultHandler handle ghttp.DefaultHandlerResponse of json format. | ||
type DefaultHandler struct { | ||
Logger *glog.Logger | ||
RawDump bool | ||
} | ||
|
||
func NewDefaultHandler(logger *glog.Logger, rawRump bool) *DefaultHandler { | ||
if rawRump && logger == nil { | ||
logger = g.Log() | ||
} | ||
return &DefaultHandler{ | ||
Logger: logger, | ||
RawDump: rawRump, | ||
} | ||
} | ||
|
||
func (h DefaultHandler) HandleResponse(ctx context.Context, res *gclient.Response, out interface{}) error { | ||
defer res.Close() | ||
if h.RawDump { | ||
h.Logger.Debugf(ctx, "raw request&response:\n%s", res.Raw()) | ||
} | ||
var ( | ||
responseBytes = res.ReadAll() | ||
result = ghttp.DefaultHandlerResponse{ | ||
Data: out, | ||
} | ||
) | ||
if !json.Valid(responseBytes) { | ||
return gerror.Newf(`invalid response content: %s`, responseBytes) | ||
} | ||
if err := json.Unmarshal(responseBytes, &result); err != nil { | ||
return gerror.Wrapf(err, `json.Unmarshal failed with content:%s`, responseBytes) | ||
} | ||
if result.Code != gcode.CodeOK.Code() { | ||
return gerror.NewCode( | ||
gcode.New(result.Code, result.Message, nil), | ||
result.Message, | ||
) | ||
} | ||
return nil | ||
} |
109 changes: 109 additions & 0 deletions
109
contrib/sdk/httpclient/httpclient_z_unit_feature_handler_test.go
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,109 @@ | ||
// 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_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gogf/gf/contrib/sdk/httpclient/v2" | ||
"github.com/gogf/gf/v2/errors/gcode" | ||
"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/os/gctx" | ||
"github.com/gogf/gf/v2/test/gtest" | ||
"github.com/gogf/gf/v2/util/guid" | ||
) | ||
|
||
func Test_HttpClient_With_Default_Handler(t *testing.T) { | ||
type Req struct { | ||
g.Meta `path:"/get" method:"get"` | ||
} | ||
type Res struct { | ||
Uid int | ||
Name string | ||
} | ||
|
||
s := g.Server(guid.S()) | ||
s.BindHandler("/get", func(r *ghttp.Request) { | ||
res := ghttp.DefaultHandlerResponse{ | ||
Data: Res{ | ||
Uid: 1, | ||
Name: "test", | ||
}, | ||
} | ||
r.Response.WriteJson(res) | ||
}) | ||
s.SetDumpRouterMap(false) | ||
s.Start() | ||
defer s.Shutdown() | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
gtest.C(t, func(t *gtest.T) { | ||
client := httpclient.New(httpclient.Config{ | ||
URL: fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()), | ||
}) | ||
var ( | ||
req = &Req{} | ||
res = &Res{} | ||
) | ||
err := client.Request(gctx.New(), req, res) | ||
t.AssertNil(err) | ||
t.AssertEQ(res.Uid, 1) | ||
t.AssertEQ(res.Name, "test") | ||
}) | ||
} | ||
|
||
type CustomHandler struct{} | ||
|
||
func (c CustomHandler) HandleResponse(ctx context.Context, res *gclient.Response, out interface{}) error { | ||
defer res.Close() | ||
if pointer, ok := out.(*string); ok { | ||
*pointer = res.ReadAllString() | ||
} else { | ||
return gerror.NewCodef(gcode.CodeInvalidParameter, "[CustomHandler] expectedType:'*string', but realType:'%T'", out) | ||
} | ||
return nil | ||
} | ||
|
||
func Test_HttpClient_With_Custom_Handler(t *testing.T) { | ||
type Req struct { | ||
g.Meta `path:"/get" method:"get"` | ||
} | ||
|
||
s := g.Server(guid.S()) | ||
s.BindHandler("/get", func(r *ghttp.Request) { | ||
r.Response.WriteExit("It is a test.") | ||
}) | ||
s.SetDumpRouterMap(false) | ||
s.Start() | ||
defer s.Shutdown() | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
client := httpclient.New(httpclient.Config{ | ||
URL: fmt.Sprintf("127.0.0.1:%d", s.GetListenedPort()), | ||
Handler: CustomHandler{}, | ||
}) | ||
req := &Req{} | ||
gtest.C(t, func(t *gtest.T) { | ||
var res = new(string) | ||
err := client.Request(gctx.New(), req, res) | ||
t.AssertNil(err) | ||
t.AssertEQ(*res, "It is a test.") | ||
}) | ||
gtest.C(t, func(t *gtest.T) { | ||
var res string | ||
err := client.Request(gctx.New(), req, res) | ||
t.AssertEQ(err, gerror.NewCodef(gcode.CodeInvalidParameter, "[CustomHandler] expectedType:'*string', but realType:'%T'", res)) | ||
}) | ||
} |