-
Notifications
You must be signed in to change notification settings - Fork 5
/
RecipesService.dart
150 lines (122 loc) · 4.57 KB
/
RecipesService.dart
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
import 'dart:async';
import 'package:grpc/grpc.dart';
import 'package:homerecipes/generated/defs/recipes-service.pbgrpc.dart';
class RecipesService {
static void addRecipe(String recipeName, String recipeCuisine) async {
print('Calling addRecipe endpoint');
final channel = ClientChannel(
// '10.0.2.2', // localhost via the Android emulator
'104.45.178.90', // running on Azure AKS
port: 50000,
options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
);
final stub = RecipesServiceClient(channel);
var recipeToAdd = Recipe();
recipeToAdd.name = recipeName;
recipeToAdd.cuisine = recipeCuisine;
try {
final response =
await stub.addRecipe(AddRecipeRequest()..recipe = recipeToAdd);
print('Received response from server as $response');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
static void listAllRecipes(
StreamController<ListAllRecipesResponse> streamController) async {
print('Calling listAllRecipes endpoint');
final channel = ClientChannel(
// '10.0.2.2', // localhost via the Android emulator
'104.45.178.90', // running on Azure AKS
port: 50000,
options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
);
final stub = RecipesServiceClient(channel);
try {
await for (var response in stub.listAllRecipes(ListAllRecipesRequest())) {
streamController.add(response);
}
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
static void listAllIngredientsAtHome(
Stream<ListAllIngredientsAtHomeRequest> request) async {
print('Calling listAllIngredientsAtHome endpoint');
final channel = ClientChannel(
// '10.0.2.2', // localhost via the Android emulator
'104.45.178.90', // running on Azure AKS
port: 50000,
options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
);
final stub = RecipesServiceClient(channel);
try {
final response = await stub.listAllIngredientsAtHome(request);
print('Received response from server as $response');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
static void getIngredientsForAllRecipes(int selectedIndex, StreamController<GetIngredientsForAllRecipesResponse> streamController) async {
print('Calling getIngredientsForAllRecipes endpoint');
// Create connection to start communication
final channel = ClientChannel(
// '10.0.2.2', // localhost via the Android emulator
'104.45.178.90', // running on Azure AKS
port: 50000,
options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
);
final stub = RecipesServiceClient(channel);
// Create list of requests (based on selected tab)
var requestList = getRequestList(selectedIndex);
// Stub to convert requests to streams
Stream<GetIngredientsForAllRecipesRequest> outgoingRequests() async* {
for (final request in requestList) {
// Short delay to simulate some other interaction.
await Future.delayed(Duration(milliseconds: 10));
yield request;
}
}
// Make request on connection and add response to the response stream
try {
await for (var response in stub.getIngredientsForAllRecipes(outgoingRequests())) {
streamController.add(response);
}
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
static List<GetIngredientsForAllRecipesRequest> getRequestList(int selectedIndex) {
// Stub to create multiple requests
GetIngredientsForAllRecipesRequest createRequest(
String recipeName, String recipeCuisine) {
var recipeToAdd = Recipe();
recipeToAdd.name = recipeName;
recipeToAdd.cuisine = recipeCuisine;
var request = GetIngredientsForAllRecipesRequest();
request.recipe = recipeToAdd;
return request;
}
if (selectedIndex == 0) {
return <GetIngredientsForAllRecipesRequest>[
createRequest('Croissants', ''),
createRequest('Chicken pasta bake', ''),
createRequest('Roast salmon with preserved lemon', ''),
];
} else if (selectedIndex == 1) {
return <GetIngredientsForAllRecipesRequest>[
createRequest('Nachos', ''),
createRequest('Croissants', ''),
];
} else {
return <GetIngredientsForAllRecipesRequest>[
createRequest('Bread', ''),
createRequest('Roast salmon with preserved lemon', ''),
];
}
}
}