/
endpoint.go
99 lines (79 loc) · 3.28 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
package users
import (
"context"
"github.com/go-kit/kit/endpoint"
)
// userCreateRequest represents an HTTP request from the client for user creation
type userCreateRequest struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
FavoriteColor string `json:"fav_color,omitempty"`
}
// userCreateResponse represents an HTTP response from our server for user creation
type userCreateResponse struct {
ID int `json:"id,omitempty"`
Error error `json:"error,omitempty"`
}
// error is the userCreateResponse errorer implementation
func (r userCreateResponse) error() error { return r.Error }
// makeCreateUserEndpoint generates a service endpoint for users
func makeCreateUserEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(*userCreateRequest)
id, err := s.CreateUser(req.ID, req.FirstName, req.LastName, req.FavoriteColor)
return userCreateResponse{ID: id, Error: err}, nil
}
}
// userReadRequest represents an HTTP request to read a single user from the client
type userReadRequest struct {
ID int `json:"id"`
}
// userReadResponse represents an HTTP response containing a user or the error when fetching
type userReadResponse struct {
User User `json:"user,omitempty"`
Error error `json:"error,omitempty"`
}
// error is the userReadResponse errorer implementation
func (r userReadResponse) error() error { return r.Error }
func makeReadUserEindpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(userReadRequest)
u, err := s.ReadUser(req.ID)
return userReadResponse{User: u, Error: err}, nil
}
}
// userUpdateColorRequest represents an HTTP request from the client to update a user's favorite color
type userUpdateColorRequest struct {
ID int `json:"id"`
FavoriteColor string `json:"favorite_color"`
}
// userUpdateColorResponse represents an HTTP response from the server notifying the client of the update status
type userUpdateColorResponse struct {
Error error `json:"error,omitempty"`
}
// error is an errorer implementation for userUpdateColorResponse
func (r userUpdateColorResponse) error() error { return r.Error }
func makeUpdateUserColorEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(userUpdateColorRequest)
err := s.UpdateUserColor(req.ID, req.FavoriteColor)
return userUpdateColorResponse{Error: err}, nil
}
}
// userReadAllRequest represents an HTTP request from the client to get all users
type userReadAllRequest struct{}
// userReadAllResponse represents an HTTP response from the server listing all users
type userReadAllResponse struct {
Users []*User `json:"users,omitempty"`
Error error `json:"error,omitempty"`
}
// error is an errorer implementation for userReadAllResponse
func (r userReadAllResponse) error() error { return r.Error }
// makeReadAllUsersEndpoint creates an HTTP endpoint for retrieving all users
func makeReadAllUsersEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
users := s.Users()
return userReadAllResponse{Users: users, Error: nil}, nil
}
}