-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.go
204 lines (194 loc) · 5.57 KB
/
invoke.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package invoke
import (
"context"
"fmt"
"strings"
"github.com/Jiali-Xing/hotelApp/internal/config"
hotelpb "github.com/Jiali-Xing/hotelproto"
)
func Invoke[T any](ctx context.Context, app string, method string, input interface{}) (T, error) {
var res T
client, err := getClient(app)
if err != nil {
config.DebugLog("Error getting client for app %s: %v", app, err)
return res, err
}
// Convert method name to lower case to make comparison case-insensitive
method = strings.ToLower(method)
switch app {
case "user":
userClient, ok := client.(hotelpb.UserServiceClient)
if !ok {
return res, fmt.Errorf("invalid client type for service: %s", app)
}
switch method {
case "login":
req, ok := input.(*hotelpb.LoginRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := userClient.Login(ctx, req)
if err != nil {
config.DebugLog("Error calling Login method: %v", err)
return res, err
}
if resp == nil {
config.DebugLog("Received nil response from login gRPC call")
return res, fmt.Errorf("received nil response from login gRPC call")
}
config.DebugLog("Received response from Login method: %v", resp)
res = any(resp).(T)
case "registeruser":
req, ok := input.(*hotelpb.RegisterUserRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := userClient.RegisterUser(ctx, req)
if err != nil {
return res, err
}
if resp == nil {
config.DebugLog("Received nil response from register user gRPC call")
return res, fmt.Errorf("received nil response from register user gRPC call")
}
config.DebugLog("Received response from RegisterUser method: %v", resp)
res = any(resp).(T)
// Add more user methods here
default:
return res, fmt.Errorf("unsupported method: %s", method)
}
case "search":
searchClient, ok := client.(hotelpb.SearchServiceClient)
if !ok {
return res, fmt.Errorf("invalid client type for service: %s", app)
}
switch method {
case "nearby":
req, ok := input.(*hotelpb.NearbyRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := searchClient.Nearby(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
case "storehotellocation":
req, ok := input.(*hotelpb.StoreHotelLocationRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := searchClient.StoreHotelLocation(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
default:
return res, fmt.Errorf("unsupported method: %s", method)
}
case "reservation":
reservationClient, ok := client.(hotelpb.ReservationServiceClient)
if !ok {
return res, fmt.Errorf("invalid client type for service: %s", app)
}
switch method {
case "addhotelavailability":
req, ok := input.(*hotelpb.AddHotelAvailabilityRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := reservationClient.AddHotelAvailability(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
case "makereservation":
req, ok := input.(*hotelpb.MakeReservationRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := reservationClient.MakeReservation(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
case "checkavailability":
req, ok := input.(*hotelpb.CheckAvailabilityRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := reservationClient.CheckAvailability(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
// Add more reservation methods here
default:
return res, fmt.Errorf("unsupported method: %s", method)
}
case "rate":
rateClient, ok := client.(hotelpb.RateServiceClient)
if !ok {
return res, fmt.Errorf("invalid client type for service: %s", app)
}
switch method {
case "getrates":
req, ok := input.(*hotelpb.GetRatesRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := rateClient.GetRates(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
case "storerate":
req, ok := input.(*hotelpb.StoreRateRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := rateClient.StoreRate(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
// Add more rate methods here
default:
return res, fmt.Errorf("unsupported method: %s", method)
}
case "profile":
profileClient, ok := client.(hotelpb.ProfileServiceClient)
if !ok {
return res, fmt.Errorf("invalid client type for service: %s", app)
}
switch method {
case "getprofiles":
req, ok := input.(*hotelpb.GetProfilesRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := profileClient.GetProfiles(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
case "storeprofile":
req, ok := input.(*hotelpb.StoreProfileRequest)
if !ok {
return res, fmt.Errorf("invalid input type for method: %s", method)
}
resp, err := profileClient.StoreProfile(ctx, req)
if err != nil {
return res, err
}
res = any(resp).(T)
// Add more profile methods here
default:
return res, fmt.Errorf("unsupported method: %s", method)
}
// Add more services here
default:
return res, fmt.Errorf("unknown app: %s", app)
}
return res, nil
}