forked from go-kit/kit
/
endpoint.go
139 lines (109 loc) · 3.72 KB
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package booking
import (
"context"
"time"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
)
type bookCargoRequest struct {
Origin location.UNLocode
Destination location.UNLocode
ArrivalDeadline time.Time
}
type bookCargoResponse struct {
ID cargo.TrackingID `json:"tracking_id,omitempty"`
Err error `json:"error,omitempty"`
}
func (r bookCargoResponse) error() error { return r.Err }
func makeBookCargoEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(bookCargoRequest)
id, err := s.BookNewCargo(req.Origin, req.Destination, req.ArrivalDeadline)
return bookCargoResponse{ID: id, Err: err}, nil
}
}
type loadCargoRequest struct {
ID cargo.TrackingID
}
type loadCargoResponse struct {
Cargo *Cargo `json:"cargo,omitempty"`
Err error `json:"error,omitempty"`
}
func (r loadCargoResponse) error() error { return r.Err }
func makeLoadCargoEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(loadCargoRequest)
c, err := s.LoadCargo(req.ID)
return loadCargoResponse{Cargo: &c, Err: err}, nil
}
}
type requestRoutesRequest struct {
ID cargo.TrackingID
}
type requestRoutesResponse struct {
Routes []cargo.Itinerary `json:"routes,omitempty"`
Err error `json:"error,omitempty"`
}
func (r requestRoutesResponse) error() error { return r.Err }
func makeRequestRoutesEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(requestRoutesRequest)
itin := s.RequestPossibleRoutesForCargo(req.ID)
return requestRoutesResponse{Routes: itin, Err: nil}, nil
}
}
type assignToRouteRequest struct {
ID cargo.TrackingID
Itinerary cargo.Itinerary
}
type assignToRouteResponse struct {
Err error `json:"error,omitempty"`
}
func (r assignToRouteResponse) error() error { return r.Err }
func makeAssignToRouteEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(assignToRouteRequest)
err := s.AssignCargoToRoute(req.ID, req.Itinerary)
return assignToRouteResponse{Err: err}, nil
}
}
type changeDestinationRequest struct {
ID cargo.TrackingID
Destination location.UNLocode
}
type changeDestinationResponse struct {
Err error `json:"error,omitempty"`
}
func (r changeDestinationResponse) error() error { return r.Err }
func makeChangeDestinationEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(changeDestinationRequest)
err := s.ChangeDestination(req.ID, req.Destination)
return changeDestinationResponse{Err: err}, nil
}
}
type listCargosRequest struct{}
type listCargosResponse struct {
Cargos []Cargo `json:"cargos,omitempty"`
Err error `json:"error,omitempty"`
}
func (r listCargosResponse) error() error { return r.Err }
func makeListCargosEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
_ = request.(listCargosRequest)
return listCargosResponse{Cargos: s.Cargos(), Err: nil}, nil
}
}
type listLocationsRequest struct {
}
type listLocationsResponse struct {
Locations []Location `json:"locations,omitempty"`
Err error `json:"error,omitempty"`
}
func makeListLocationsEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
_ = request.(listLocationsRequest)
return listLocationsResponse{Locations: s.Locations(), Err: nil}, nil
}
}