-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
133 lines (116 loc) · 3.44 KB
/
transport.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
package service
import (
"context"
"encoding/json"
"errors"
om "github.com/bbakla/hands-on-microservices-kubernetes/pkg/object_model"
"github.com/go-kit/kit/endpoint"
"net/http"
"strings"
)
type followRequest struct {
Followed string `json:"followed"`
Follower string `json:"follower"`
}
type followResponse struct {
Err string `json:"err"`
}
type unfollowRequest struct {
Followed string `json:"followed"`
Follower string `json:"follower"`
}
type unfollowResponse struct {
Err string `json:"err"`
}
type getByUsernameRequest struct {
Username string `json:"username"`
}
type getFollowersResponse struct {
Followers map[string]bool `json:"followers"`
Err string `json:"err"`
}
type getFollowingResponse struct {
Following map[string]bool `json:"following"`
Err string `json:"err"`
}
func decodeFollowRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request followRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
return nil, err
}
return request, nil
}
func decodeUnfollowRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request unfollowRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
return nil, err
}
return request, nil
}
func decodeGetFollowingRequest(_ context.Context, r *http.Request) (interface{}, error) {
parts := strings.Split(r.URL.Path, "/")
username := parts[len(parts)-1]
if username == "" || username == "following" {
return nil, errors.New("user name must not be empty")
}
request := getByUsernameRequest{Username: username}
return request, nil
}
func decodeGetFollowersRequest(_ context.Context, r *http.Request) (interface{}, error) {
parts := strings.Split(r.URL.Path, "/")
username := parts[len(parts)-1]
if username == "" || username == "followers" {
return nil, errors.New("user name must not be empty")
}
request := getByUsernameRequest{Username: username}
return request, nil
}
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}
func makeFollowEndpoint(svc om.SocialGraphManager) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(followRequest)
err := svc.Follow(req.Followed, req.Follower)
res := followResponse{}
if err != nil {
res.Err = err.Error()
}
return res, nil
}
}
func makeUnfollowEndpoint(svc om.SocialGraphManager) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(unfollowRequest)
err := svc.Unfollow(req.Followed, req.Follower)
res := unfollowResponse{}
if err != nil {
res.Err = err.Error()
}
return res, nil
}
}
func makeGetFollowingEndpoint(svc om.SocialGraphManager) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(getByUsernameRequest)
followingMap, err := svc.GetFollowing(req.Username)
res := getFollowingResponse{Following: followingMap}
if err != nil {
res.Err = err.Error()
}
return res, nil
}
}
func makeGetFollowersEndpoint(svc om.SocialGraphManager) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(getByUsernameRequest)
followersMap, err := svc.GetFollowers(req.Username)
res := getFollowersResponse{Followers: followersMap}
if err != nil {
res.Err = err.Error()
}
return res, nil
}
}