-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathValueGetRequest.cs
251 lines (204 loc) · 7.27 KB
/
ValueGetRequest.cs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using NATS.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ResgateIO.Service
{
internal class ValueGetRequest : ResourceDecorator, IRequest, IGetRequest, IModelRequest, ICollectionRequest
{
private const string invalidError = "Method call invalid within get request handler";
public object ValueResult { get; private set; }
public ResError ErrorResult { get; private set; }
private ILogger Log { get { return Service.Log; } }
private bool replied = false;
public ValueGetRequest(IResourceContext resource)
: base(resource)
{
}
public override T Value<T>()
{
throw new InvalidOperationException("Value called within get request handler");
}
public override Task<T> ValueAsync<T>()
{
throw new InvalidOperationException("ValueAsync called within get request handler");
}
public override T RequireValue<T>()
{
throw new InvalidOperationException("RequireValue called within get request handler");
}
public override Task<T> RequireValueAsync<T>()
{
throw new InvalidOperationException("RequireValueAsync called within get request handler");
}
public void Error(ResError error)
{
reply();
ErrorResult = error;
}
public void NotFound()
{
Error(ResError.NotFound);
}
/// <summary>
/// Sends a system.invalidQuery response with a default error message.
/// </summary>
public void InvalidQuery()
{
Error(ResError.InvalidQuery);
}
/// <summary>
/// Sends a system.invalidQuery response with a custom error message.
/// </summary>
/// <param name="message">Error message.</param>
public void InvalidQuery(string message)
{
Error(new ResError(ResError.CodeInvalidQuery, message));
}
/// <summary>
/// Sends a system.invalidQuery response with a custom error message and data.
/// </summary>
/// <param name="message">Error message.</param>
/// <param name="data">Additional data. Must be JSON serializable.</param>
public void InvalidQuery(string message, object data)
{
Error(new ResError(ResError.CodeInvalidQuery, message, data));
}
public void Model(object model)
{
Model(model, null);
}
public void Model(object model, string query)
{
reply();
ValueResult = model;
}
public void Collection(object collection)
{
Collection(collection, null);
}
public void Collection(object collection, string query)
{
reply();
ValueResult = collection;
}
public void Timeout(int milliseconds)
{
// Implement when an internal timeout for requests is implemented
}
public void Timeout(TimeSpan duration)
{
Timeout((int)duration.TotalMilliseconds);
}
public bool ForValue { get { return true; } }
public RequestType Type { get { return RequestType.Get; } }
public string Method => throw new InvalidOperationException(invalidError);
public string CID => throw new InvalidOperationException(invalidError);
public JToken Token => throw new InvalidOperationException(invalidError);
public JToken Params => throw new InvalidOperationException(invalidError);
public Dictionary<string, string[]> Header => throw new InvalidOperationException(invalidError);
public string Host => throw new InvalidOperationException(invalidError);
public string RemoteAddr => throw new InvalidOperationException(invalidError);
public string URI => throw new InvalidOperationException(invalidError);
internal async Task ExecuteHandler()
{
try
{
await Handler.Handle(this);
}
catch (ResException ex)
{
if (replied)
{
Service.OnError("Error in value get request for {0}: {1} - {2}", ResourceName, ex.Code, ex.Message);
}
else
{
ErrorResult = new ResError(ex);
}
}
catch (Exception ex)
{
// Log error and rethrow as only ResExceptions are considered valid behaviour
Service.OnError("Error in value get request for {0}: {1}", ResourceName, ex.Message);
throw ex;
}
}
private void reply()
{
if (replied)
{
throw new InvalidOperationException("Response already sent on request");
}
replied = true;
}
public void RawResponse(byte[] data)
{
throw new InvalidOperationException(invalidError);
}
public void Access(bool get, string call)
{
throw new InvalidOperationException(invalidError);
}
public void AccessDenied()
{
throw new InvalidOperationException(invalidError);
}
public void AccessGranted()
{
throw new InvalidOperationException(invalidError);
}
public T ParseToken<T>()
{
throw new InvalidOperationException(invalidError);
}
public void Ok()
{
throw new InvalidOperationException(invalidError);
}
public void Ok(object result)
{
throw new InvalidOperationException(invalidError);
}
public void Resource(string resourceID)
{
throw new InvalidOperationException(invalidError);
}
public void MethodNotFound()
{
throw new InvalidOperationException(invalidError);
}
public void InvalidParams()
{
throw new InvalidOperationException(invalidError);
}
public void InvalidParams(string message)
{
throw new InvalidOperationException(invalidError);
}
public void InvalidParams(string message, object data)
{
throw new InvalidOperationException(invalidError);
}
public T ParseParams<T>()
{
throw new InvalidOperationException(invalidError);
}
public void TokenEvent(object token)
{
throw new InvalidOperationException(invalidError);
}
public void TokenEvent(object token, string tid)
{
throw new InvalidOperationException(invalidError);
}
public void New(Ref rid)
{
throw new InvalidOperationException(invalidError);
}
}
}