Skip to content
Snippets Groups Projects
Commit a44e0714 authored by Patrick's avatar Patrick
Browse files

header params

parent bc696d56
Branches
Tags
No related merge requests found
...@@ -18,6 +18,7 @@ type RestRequest struct { ...@@ -18,6 +18,7 @@ type RestRequest struct {
PathParams map[string]string PathParams map[string]string
QueryParams map[string]string QueryParams map[string]string
Body map[string]interface{} Body map[string]interface{}
Headers map[string]string
} }
type RestClient interface { type RestClient interface {
......
...@@ -41,4 +41,6 @@ type RestMethodConfig struct { ...@@ -41,4 +41,6 @@ type RestMethodConfig struct {
QueryParamsMap map[int]string QueryParamsMap map[int]string
Body string `yaml:"rest_body" json:"rest_body,omitempty" property:"rest_body"` Body string `yaml:"rest_body" json:"rest_body,omitempty" property:"rest_body"`
BodyMap map[int]string BodyMap map[int]string
Headers string `yaml:"rest_headers" json:"rest_headers,omitempty" property:"rest_headers"`
HeadersMap map[int]string
} }
...@@ -35,18 +35,10 @@ func (ri *RestInvoker) Invoke(ctx context.Context, invocation protocol.Invocatio ...@@ -35,18 +35,10 @@ func (ri *RestInvoker) Invoke(ctx context.Context, invocation protocol.Invocatio
logger.Errorf("[RestInvoker]Rest methodConfig:%s is nil", inv.MethodName()) logger.Errorf("[RestInvoker]Rest methodConfig:%s is nil", inv.MethodName())
return nil return nil
} }
pathParams := make(map[string]string) pathParams := restStringMapTransform(methodConfig.PathParamsMap, inv.Arguments())
queryParams := make(map[string]string) queryParams := restStringMapTransform(methodConfig.QueryParamsMap, inv.Arguments())
bodyParams := make(map[string]interface{}) headers := restStringMapTransform(methodConfig.HeadersMap, inv.Arguments())
for key, value := range methodConfig.PathParamsMap { bodyParams := restInterfaceMapTransform(methodConfig.BodyMap, inv.Arguments())
pathParams[value] = fmt.Sprintf("%v", inv.Arguments()[key])
}
for key, value := range methodConfig.QueryParamsMap {
queryParams[value] = fmt.Sprintf("%v", inv.Arguments()[key])
}
for key, value := range methodConfig.BodyMap {
bodyParams[value] = inv.Arguments()[key]
}
req := &rest_interface.RestRequest{ req := &rest_interface.RestRequest{
Location: ri.GetUrl().Location, Location: ri.GetUrl().Location,
Produces: methodConfig.Produces, Produces: methodConfig.Produces,
...@@ -56,11 +48,27 @@ func (ri *RestInvoker) Invoke(ctx context.Context, invocation protocol.Invocatio ...@@ -56,11 +48,27 @@ func (ri *RestInvoker) Invoke(ctx context.Context, invocation protocol.Invocatio
PathParams: pathParams, PathParams: pathParams,
QueryParams: queryParams, QueryParams: queryParams,
Body: bodyParams, Body: bodyParams,
Headers: headers,
} }
result.Err = ri.client.Do(req, inv.Reply()) result.Err = ri.client.Do(req, inv.Reply())
if result.Err == nil { if result.Err == nil {
result.Rest = inv.Reply() result.Rest = inv.Reply()
} }
return &result return &result
}
func restStringMapTransform(paramsMap map[int]string, args []interface{}) map[string]string {
resMap := make(map[string]string, len(paramsMap))
for key, value := range paramsMap {
resMap[value] = fmt.Sprintf("%v", args[key])
}
return resMap
}
func restInterfaceMapTransform(paramsMap map[int]string, args []interface{}) map[string]interface{} {
resMap := make(map[string]interface{}, len(paramsMap))
for key, value := range paramsMap {
resMap[value] = args[key]
}
return resMap
} }
...@@ -18,7 +18,6 @@ type User struct { ...@@ -18,7 +18,6 @@ type User struct {
func TestRestInvoker_Invoke(t *testing.T) { func TestRestInvoker_Invoke(t *testing.T) {
// Refer // Refer
proto := GetRestProtocol()
url, err := common.NewURL(context.Background(), "rest://127.0.0.1:8888/com.ikurento.user.UserProvider?anyhost=true&"+ url, err := common.NewURL(context.Background(), "rest://127.0.0.1:8888/com.ikurento.user.UserProvider?anyhost=true&"+
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+ "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+
...@@ -57,14 +56,4 @@ func TestRestInvoker_Invoke(t *testing.T) { ...@@ -57,14 +56,4 @@ func TestRestInvoker_Invoke(t *testing.T) {
invocation.WithArguments([]interface{}{"1", "username"}), invocation.WithReply(user)) invocation.WithArguments([]interface{}{"1", "username"}), invocation.WithReply(user))
invoker.Invoke(context.Background(), inv) invoker.Invoke(context.Background(), inv)
// make sure url
eq := invoker.GetUrl().URLEqual(url)
assert.True(t, eq)
// make sure invokers after 'Destroy'
invokersLen := len(proto.(*RestProtocol).Invokers())
assert.Equal(t, 1, invokersLen)
proto.Destroy()
invokersLen = len(proto.(*RestProtocol).Invokers())
assert.Equal(t, 0, invokersLen)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment