mirror of
https://github.com/kevin-DL/m3o-go.git
synced 2026-01-23 23:21:27 +00:00
Fix client to call v1api (#1)
This commit is contained in:
@@ -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{
|
||||||
ret.options = *options
|
Address: liveAddress,
|
||||||
} else {
|
}
|
||||||
ret.options = Options{
|
if options.Token != "" {
|
||||||
Address: liveAddress,
|
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 {
|
||||||
@@ -107,7 +107,7 @@ func (client *Client) Call(service, endpoint string, request, response interface
|
|||||||
|
|
||||||
// set the token if it exists
|
// set the token if it exists
|
||||||
if len(client.options.Token) > 0 {
|
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")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +150,7 @@ func (client *Client) Stream(service, endpoint string, request interface{}) (*St
|
|||||||
header := make(http.Header)
|
header := make(http.Header)
|
||||||
// set the token if it exists
|
// set the token if it exists
|
||||||
if len(client.options.Token) > 0 {
|
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")
|
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) {
|
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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user