2 Commits
v0.8.0 ... fix

Author SHA1 Message Date
Janos Dobronszki
c12e0e2c49 F 2021-04-21 11:31:39 +01:00
Janos Dobronszki
0235f16741 Fix client to call v1api 2021-04-21 11:29:04 +01:00
2 changed files with 28 additions and 43 deletions

View File

@@ -2,8 +2,8 @@ package client
import ( import (
"bytes" "bytes"
"encoding/base64"
"encoding/json" "encoding/json"
"errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@@ -66,15 +66,15 @@ type Stream struct {
// NewClient returns a generic micro client that connects to live by default // NewClient returns a generic micro client that connects to live by default
func NewClient(options *Options) *Client { func NewClient(options *Options) *Client {
ret := new(Client) ret := new(Client)
if options != nil {
ret.options = *options
} else {
ret.options = Options{ ret.options = Options{
Address: liveAddress, Address: liveAddress,
} }
if options.Token != "" {
ret.options.Token = options.Token
} }
if options != nil && options.Local { if options != nil && options.Local {
ret.options.Address = localAddress ret.options.Address = localAddress
ret.options.Local = true
} }
return ret return ret
} }
@@ -93,7 +93,7 @@ func (client *Client) Call(service, endpoint string, request, response interface
return err return err
} }
// TODO: make optional // TODO: make optional
uri.Path = "/client" uri.Path = "/v1/" + service + "/" + endpoint
b, err := marshalRequest(service, endpoint, request) b, err := marshalRequest(service, endpoint, request)
if err != nil { if err != nil {
@@ -123,6 +123,9 @@ func (client *Client) Call(service, endpoint string, request, response interface
if err != nil { if err != nil {
return err return err
} }
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return errors.New(string(body))
}
return unmarshalResponse(body, response) return unmarshalResponse(body, response)
} }
@@ -183,25 +186,9 @@ func (s *Stream) Send(v interface{}) error {
} }
func marshalRequest(service, endpoint string, v interface{}) ([]byte, error) { func marshalRequest(service, endpoint string, v interface{}) ([]byte, error) {
b, err := json.Marshal(v) return json.Marshal(v)
if err != nil {
return nil, err
}
return json.Marshal(&Request{
Service: service,
Endpoint: endpoint,
Body: base64.StdEncoding.EncodeToString(b),
})
} }
func unmarshalResponse(body []byte, v interface{}) error { func unmarshalResponse(body []byte, v interface{}) error {
rsp := new(Response) return json.Unmarshal(body, &v)
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)
} }

View File

@@ -1,22 +1,20 @@
package client package client
import "testing" import (
"os"
type req struct { "testing"
Name string `json:"name"` )
}
type rsp struct {
Msg string `json:"msg"`
}
func TestBasicCall(t *testing.T) { func TestBasicCall(t *testing.T) {
response := rsp{} response := map[string]interface{}{}
if err := NewClient(nil).Call("go.micro.srv.greeter", "Say.Hello", req{Name: "John"}, &response); err != nil { if err := NewClient(&Options{
t.Fail() Token: os.Getenv("TOKEN"),
}).Call("groups", "list", map[string]interface{}{
"memberId": "random",
}, &response); err != nil {
t.Fatal(err)
} }
if response.Msg != "Hello John" { if len(response) > 0 {
t.Logf("Message is not as expected: %v", response.Msg) t.Fatal(len(response))
t.Fail()
} }
} }