Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing the interface of RestServer and RestClient #460

Merged
merged 14 commits into from
Apr 7, 2020
16 changes: 9 additions & 7 deletions protocol/rest/client/client_impl/resty_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ func init() {
extension.SetRestClient(constant.DEFAULT_REST_CLIENT, NewRestyClient)
}

// RestyClient a rest client implement by Resty
type RestyClient struct {
client *resty.Client
}

// NewRestyClient a constructor of RestyClient
func NewRestyClient(restOption *client.RestOptions) client.RestClient {
client := resty.New()
client.SetTransport(
Expand All @@ -65,21 +67,21 @@ func NewRestyClient(restOption *client.RestOptions) client.RestClient {
}
}

func (rc *RestyClient) Do(restRequest *client.RestRequest, res interface{}) error {
r, err := rc.client.R().
SetHeader("Content-Type", restRequest.Consumes).
SetHeader("Accept", restRequest.Produces).
// Do send request by RestyClient
func (rc *RestyClient) Do(restRequest *client.RestClientRequest, res interface{}) error {
req := rc.client.R()
req.Header = restRequest.Header
resp, err := req.
SetPathParams(restRequest.PathParams).
SetQueryParams(restRequest.QueryParams).
SetHeaders(restRequest.Headers).
SetBody(restRequest.Body).
SetResult(res).
Execute(restRequest.Method, "http://"+path.Join(restRequest.Location, restRequest.Path))
if err != nil {
return perrors.WithStack(err)
}
if r.IsError() {
return perrors.New(r.String())
if resp.IsError() {
return perrors.New(resp.String())
}
return nil
}
12 changes: 7 additions & 5 deletions protocol/rest/client/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@
package client

import (
"net/http"
"time"
)

// RestOptions
type RestOptions struct {
RequestTimeout time.Duration
ConnectTimeout time.Duration
}

type RestRequest struct {
// RestClientRequest
type RestClientRequest struct {
Header http.Header
Location string
Path string
Produces string
Consumes string
Method string
PathParams map[string]string
QueryParams map[string]string
Body interface{}
Headers map[string]string
}

// RestClient user can implement this client interface to send request
type RestClient interface {
Do(request *RestRequest, res interface{}) error
Do(request *RestClientRequest, res interface{}) error
}
26 changes: 19 additions & 7 deletions protocol/rest/rest_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package rest
import (
"context"
"fmt"
"net/http"
)

import (
Expand Down Expand Up @@ -56,7 +57,7 @@ func (ri *RestInvoker) Invoke(ctx context.Context, invocation protocol.Invocatio
body interface{}
pathParams map[string]string
queryParams map[string]string
headers map[string]string
header http.Header
err error
)
if methodConfig == nil {
Expand All @@ -71,24 +72,21 @@ func (ri *RestInvoker) Invoke(ctx context.Context, invocation protocol.Invocatio
result.Err = err
return &result
}
if headers, err = restStringMapTransform(methodConfig.HeadersMap, inv.Arguments()); err != nil {
if header, err = getRestHttpHeader(methodConfig, inv.Arguments()); err != nil {
result.Err = err
return &result
}
if len(inv.Arguments()) > methodConfig.Body && methodConfig.Body >= 0 {
body = inv.Arguments()[methodConfig.Body]
}

req := &client.RestRequest{
req := &client.RestClientRequest{
Location: ri.GetUrl().Location,
Produces: methodConfig.Produces,
Consumes: methodConfig.Consumes,
Method: methodConfig.MethodType,
Path: methodConfig.Path,
PathParams: pathParams,
QueryParams: queryParams,
Body: body,
Headers: headers,
Header: header,
}
result.Err = ri.client.Do(req, inv.Reply())
if result.Err == nil {
Expand All @@ -107,3 +105,17 @@ func restStringMapTransform(paramsMap map[int]string, args []interface{}) (map[s
}
return resMap, nil
}

func getRestHttpHeader(methodConfig *config.RestMethodConfig, args []interface{}) (http.Header, error) {
header := http.Header{}
headersMap := methodConfig.HeadersMap
header.Set("Content-Type", methodConfig.Consumes)
header.Set("Accept", methodConfig.Produces)
for k, v := range headersMap {
if k >= len(args) || k < 0 {
return nil, perrors.Errorf("[Rest Invoke] Index %v is out of bundle", k)
}
header.Set(v, fmt.Sprint(args[k]))
}
return header, nil
}
4 changes: 3 additions & 1 deletion protocol/rest/rest_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func (rp *RestProtocol) Export(invoker protocol.Invoker) protocol.Exporter {
}
rp.SetExporterMap(serviceKey, exporter)
restServer := rp.getServer(url, restServiceConfig.Server)
restServer.Deploy(invoker, restServiceConfig.RestMethodConfigsMap)
for _, methodConfig := range restServiceConfig.RestMethodConfigsMap {
restServer.Deploy(methodConfig, server.GetRouteFunc(invoker, methodConfig))
}
return exporter
}

Expand Down
Loading