diff --git a/client/client.go b/client/client.go index cbfde46..0be46c9 100644 --- a/client/client.go +++ b/client/client.go @@ -2,8 +2,8 @@ package client import ( "bytes" - "encoding/base64" "encoding/json" + "errors" "io/ioutil" "net/http" "net/url" @@ -66,15 +66,15 @@ type Stream struct { // NewClient returns a generic micro client that connects to live by default func NewClient(options *Options) *Client { ret := new(Client) - if options != nil { - ret.options = *options - } else { - ret.options = Options{ - Address: liveAddress, - } + ret.options = Options{ + Address: liveAddress, + } + if options.Token != "" { + ret.options.Token = options.Token } if options != nil && options.Local { ret.options.Address = localAddress + ret.options.Local = true } return ret } @@ -93,7 +93,7 @@ func (client *Client) Call(service, endpoint string, request, response interface return err } // TODO: make optional - uri.Path = "/client" + uri.Path = "/v1/" + service + "/" + endpoint b, err := marshalRequest(service, endpoint, request) if err != nil { @@ -107,7 +107,7 @@ func (client *Client) Call(service, endpoint string, request, response interface // set the token if it exists if len(client.options.Token) > 0 { - req.Header.Set("Authorization", "Bearer " + client.options.Token) + req.Header.Set("Authorization", "Bearer "+client.options.Token) } req.Header.Set("Content-Type", "application/json") @@ -123,6 +123,9 @@ func (client *Client) Call(service, endpoint string, request, response interface if err != nil { return err } + if !(resp.StatusCode >= 200 && resp.StatusCode < 300) { + return errors.New(string(body)) + } return unmarshalResponse(body, response) } @@ -147,7 +150,7 @@ func (client *Client) Stream(service, endpoint string, request interface{}) (*St header := make(http.Header) // set the token if it exists if len(client.options.Token) > 0 { - header.Set("Authorization", "Bearer " + client.options.Token) + header.Set("Authorization", "Bearer "+client.options.Token) } header.Set("Content-Type", "application/json") @@ -183,25 +186,9 @@ func (s *Stream) Send(v interface{}) error { } func marshalRequest(service, endpoint string, v interface{}) ([]byte, error) { - b, err := json.Marshal(v) - if err != nil { - return nil, err - } - return json.Marshal(&Request{ - Service: service, - Endpoint: endpoint, - Body: base64.StdEncoding.EncodeToString(b), - }) + return json.Marshal(v) } func unmarshalResponse(body []byte, v interface{}) error { - rsp := new(Response) - if err := json.Unmarshal(body, rsp); err != nil { - return err - } - b, err := base64.StdEncoding.DecodeString(rsp.Body) - if err != nil { - return err - } - return json.Unmarshal(b, v) + return json.Unmarshal(body, &v) } diff --git a/client/client_test.go b/client/client_test.go index 47ba59c..cf5422a 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,22 +1,20 @@ package client -import "testing" - -type req struct { - Name string `json:"name"` -} - -type rsp struct { - Msg string `json:"msg"` -} +import ( + "os" + "testing" +) func TestBasicCall(t *testing.T) { - response := rsp{} - if err := NewClient(nil).Call("go.micro.srv.greeter", "Say.Hello", req{Name: "John"}, &response); err != nil { - t.Fail() + response := map[string]interface{}{} + if err := NewClient(&Options{ + Token: os.Getenv("TOKEN"), + }).Call("groups", "list", map[string]interface{}{ + "memberId": "random", + }, &response); err != nil { + t.Fatal(err) } - if response.Msg != "Hello John" { - t.Logf("Message is not as expected: %v", response.Msg) - t.Fail() + if len(response) > 0 { + t.Fatal(len(response)) } }