update tests for http client

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-01-19 04:15:57 +03:00
parent 16301438fd
commit 48558b66ff
10 changed files with 784 additions and 16 deletions

View File

@@ -0,0 +1,94 @@
// Code generated by protoc-gen-micro
// source: github.proto
package pb
import (
"context"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/gorilla/mux"
micro_client_http "github.com/unistack-org/micro-client-http/v3"
micro_api "github.com/unistack-org/micro/v3/api"
micro_client "github.com/unistack-org/micro/v3/client"
micro_server "github.com/unistack-org/micro/v3/server"
)
var (
_ micro_server.Option
_ micro_client.Option
)
type githubService struct {
c micro_client.Client
name string
}
// Micro client stuff
// NewGithubService create new service client
func NewGithubService(name string, c micro_client.Client) GithubService {
return &githubService{c: c, name: name}
}
func (c *githubService) LookupUser(ctx context.Context, req *LookupUserReq, opts ...micro_client.CallOption) (*LookupUserRsp, error) {
errmap := make(map[string]interface{}, 1)
errmap["default"] = &Error{}
nopts := append(opts,
micro_client_http.Method("GET"),
micro_client_http.Path("/users/{username}"),
micro_client_http.Body(""),
micro_client_http.ErrorMap(errmap),
)
rsp := &LookupUserRsp{}
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Github.LookupUser", req), rsp, nopts...)
if err != nil {
return nil, err
}
return rsp, nil
}
// Error method to satisfy error interface
func (e *Error) Error() string {
return fmt.Sprintf("%#v", e)
}
// Micro server stuff
type githubHandler struct {
GithubHandler
}
func (h *githubHandler) LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error {
return h.GithubHandler.LookupUser(ctx, req, rsp)
}
func Register(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {
v := reflect.ValueOf(h)
methods := v.NumMethod()
if methods < 1 {
return fmt.Errorf("invalid handler specified: %#+v", h)
}
for _, ep := range eps {
idx := strings.Index(ep.Name, ".")
if idx < 1 || len(ep.Name) <= idx {
return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name)
}
name := ep.Name[idx+1:]
m := v.MethodByName(name)
if !m.IsValid() || m.IsZero() {
return fmt.Errorf("invalid handler, method %s not found", name)
}
rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))
if !ok {
return fmt.Errorf("invalid handler: %#+v", m.Interface())
}
r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)
}
return nil
}