1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-05-19 21:23:04 +02:00
2020-12-26 15:21:29 +00:00

51 lines
1.2 KiB
Go

package handler
import (
"context"
"encoding/json"
"github.com/micro/go-micro/v2/util/log"
"github.com/micro/go-micro/examples/template/api/client"
example "github.com/micro/go-micro/examples/template/srv/proto/example"
api "github.com/micro/go-micro/v2/api/proto"
"github.com/micro/go-micro/v2/errors"
)
type Example struct{}
func extractValue(pair *api.Pair) string {
if pair == nil {
return ""
}
if len(pair.Values) == 0 {
return ""
}
return pair.Values[0]
}
// Example.Call is called by the API as /template/example/call with post body {"name": "foo"}
func (e *Example) Call(ctx context.Context, req *api.Request, rsp *api.Response) error {
log.Log("Received Example.Call request")
// extract the client from the context
exampleClient, ok := client.ExampleFromContext(ctx)
if !ok {
return errors.InternalServerError("go.micro.api.template.example.call", "example client not found")
}
// make request
response, err := exampleClient.Call(ctx, &example.Request{
Name: extractValue(req.Post["name"]),
})
if err != nil {
return errors.InternalServerError("go.micro.api.template.example.call", err.Error())
}
b, _ := json.Marshal(response)
rsp.StatusCode = 200
rsp.Body = string(b)
return nil
}