@@ -3,6 +3,9 @@ package calculatorservice
33import (
44"context"
55"github.com/go-kit/kit/endpoint"
6+ httptransport "github.com/go-kit/kit/transport/http"
7+ "net/url"
8+ "strings"
69)
710
811type Endpoints struct {
@@ -18,10 +21,41 @@ func MakeAddEndpoint(s Service) endpoint.Endpoint {
1821return func (ctx context.Context , request interface {}) (interface {}, error ) {
1922req := request .(addRequest )
2023//res := (addResponse)
21- r , err := s .Add (req .X , req .Y )
24+ r , err := s .Add (ctx , req .X , req .Y )
2225if err != nil {
2326return addResponse {0 , err }, err
2427}
2528return addResponse {r , nil }, nil
2629}
2730}
31+
32+ func MakeClientEndpoints (target string ) (Endpoints , error ) {
33+ if ! strings .HasPrefix (target , "http" ) {
34+ target = "http://" + target
35+ }
36+
37+ tgt , err := url .Parse (target )
38+
39+ if err != nil {
40+ return Endpoints {}, err
41+ }
42+ tgt .Path = ""
43+
44+ options := []httptransport.ClientOption {}
45+
46+ return Endpoints {
47+ AddEndpoint : httptransport .NewClient ("GET" , tgt , encodeAddRequest , decodeAddResponse , options ... ).Endpoint (),
48+ }, nil
49+ }
50+
51+ // implement client endpoint
52+
53+ func (e Endpoints ) Add (ctx context.Context , x int , y int ) (int , error ) {
54+ req := addRequest {X : x , Y : y }
55+ res , err := e .AddEndpoint (ctx , req )
56+ if err != nil {
57+ return 0 , nil
58+ }
59+ response := res .(addResponse )
60+ return response .Result , nil
61+ }
0 commit comments