client.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package client
  2. import (
  3. "golang.org/x/net/context"
  4. "github.com/cryptix/exp/todoKitSvc/reqrep"
  5. "github.com/cryptix/exp/todoKitSvc/todosvc"
  6. "github.com/go-kit/kit/endpoint"
  7. )
  8. func NewClient(ep todosvc.Endpoints) todosvc.Todo {
  9. return endpointClient{ep}
  10. }
  11. type endpointClient struct{ ep todosvc.Endpoints }
  12. func (c endpointClient) Add(ctx context.Context, title string) (todosvc.ID, error) {
  13. response, err := c.ep.Add(ctx, reqrep.AddRequest{Title: title})
  14. if err != nil {
  15. return -1, err
  16. }
  17. addResponse, ok := response.(reqrep.AddResponse)
  18. if !ok {
  19. return -1, endpoint.ErrBadCast
  20. }
  21. return addResponse.ID, addResponse.Err
  22. }
  23. func (c endpointClient) List(ctx context.Context) ([]todosvc.Item, error) {
  24. response, err := c.ep.List(ctx, reqrep.ListRequest{})
  25. if err != nil {
  26. return nil, err
  27. }
  28. listResponse, ok := response.(reqrep.ListResponse)
  29. if !ok {
  30. return nil, endpoint.ErrBadCast
  31. }
  32. return listResponse.List, listResponse.Err
  33. }
  34. func (c endpointClient) Toggle(ctx context.Context, id todosvc.ID) error {
  35. panic("not implemented")
  36. }
  37. func (c endpointClient) Delete(ctx context.Context, id todosvc.ID) error {
  38. panic("not implemented")
  39. }