forked from simplefx/Simple.OData
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathResponseReader.cs
More file actions
305 lines (272 loc) · 9.16 KB
/
ResponseReader.cs
File metadata and controls
305 lines (272 loc) · 9.16 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System.Net;
using Microsoft.OData;
using Microsoft.OData.Edm;
namespace Simple.OData.Client.V4.Adapter
{
public class ResponseReader(ISession session, IEdmModel model) : ResponseReaderBase(session)
{
private readonly IEdmModel _model = model;
public override Task<ODataResponse> GetResponseAsync(HttpResponseMessage responseMessage)
{
return GetResponseAsync(new ODataResponseMessage(responseMessage));
}
public async Task<ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage)
{
if (responseMessage.StatusCode == (int)HttpStatusCode.NoContent)
{
return ODataResponse.FromStatusCode(TypeCache, responseMessage.StatusCode, responseMessage.Headers);
}
var readerSettings = _session.ToReaderSettings();
using var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model);
var payloadKind = messageReader.DetectPayloadKind().ToList();
if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error))
{
return ODataResponse.FromStatusCode(TypeCache, responseMessage.StatusCode, responseMessage.Headers);
}
else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value))
{
if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection))
{
throw new NotImplementedException();
}
else
{
return ODataResponse.FromValueStream(TypeCache, await responseMessage.GetStreamAsync().ConfigureAwait(false), responseMessage is ODataBatchOperationResponseMessage);
}
}
else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch))
{
return await ReadResponse(messageReader.CreateODataBatchReader()).ConfigureAwait(false);
}
else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.ResourceSet))
{
return ReadResponse(messageReader.CreateODataResourceSetReader(), responseMessage);
}
else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection))
{
return ReadResponse(messageReader.CreateODataCollectionReader());
}
else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property))
{
if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Resource))
{
return ReadResponse(messageReader.CreateODataResourceReader(), responseMessage);
}
else
{
var property = messageReader.ReadProperty();
return ODataResponse.FromProperty(TypeCache, property.Name, GetPropertyValue(property.Value));
}
}
else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Delta))
{
if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Resource))
{
return ReadResponse(messageReader.CreateODataResourceReader(), responseMessage);
}
else
{
return ReadResponse(messageReader.CreateODataDeltaResourceSetReader(), responseMessage);
}
}
else
{
return ReadResponse(messageReader.CreateODataResourceReader(), responseMessage);
}
}
private async Task<ODataResponse> ReadResponse(ODataBatchReader odataReader)
{
var batch = new List<ODataResponse>();
while (odataReader.Read())
{
switch (odataReader.State)
{
case ODataBatchReaderState.ChangesetStart:
break;
case ODataBatchReaderState.Operation:
var operationMessage = odataReader.CreateOperationResponseMessage();
if (operationMessage.StatusCode == (int)HttpStatusCode.NoContent)
{
batch.Add(ODataResponse.FromStatusCode(TypeCache, operationMessage.StatusCode, operationMessage.Headers));
}
else if (operationMessage.StatusCode >= (int)HttpStatusCode.BadRequest)
{
batch.Add(ODataResponse.FromStatusCode(TypeCache,
operationMessage.StatusCode,
operationMessage.Headers,
await operationMessage.GetStreamAsync().ConfigureAwait(false),
_session.Settings.WebRequestExceptionMessageSource));
}
else
{
batch.Add(await GetResponseAsync(operationMessage).ConfigureAwait(false));
}
break;
case ODataBatchReaderState.ChangesetEnd:
break;
}
}
return ODataResponse.FromBatch(TypeCache, batch);
}
private ODataResponse ReadResponse(ODataCollectionReader odataReader)
{
var collection = new List<object>();
while (odataReader.Read())
{
if (odataReader.State == ODataCollectionReaderState.Completed)
{
break;
}
switch (odataReader.State)
{
case ODataCollectionReaderState.CollectionStart:
break;
case ODataCollectionReaderState.Value:
collection.Add(GetPropertyValue(odataReader.Item));
break;
case ODataCollectionReaderState.CollectionEnd:
break;
}
}
return ODataResponse.FromCollection(TypeCache, collection);
}
private ODataResponse ReadResponse(ODataReader odataReader, IODataResponseMessageAsync responseMessage)
{
ResponseNode? rootNode = null;
var nodeStack = new Stack<ResponseNode>();
while (odataReader.Read())
{
if (odataReader.State == ODataReaderState.Completed)
{
break;
}
switch (odataReader.State)
{
case ODataReaderState.ResourceSetStart:
case ODataReaderState.DeltaResourceSetStart:
StartFeed(nodeStack, CreateAnnotations(odataReader.Item as ODataResourceSetBase));
break;
case ODataReaderState.ResourceSetEnd:
case ODataReaderState.DeltaResourceSetEnd:
EndFeed(nodeStack, CreateAnnotations(odataReader.Item as ODataResourceSetBase), ref rootNode);
break;
case ODataReaderState.ResourceStart:
StartEntry(nodeStack);
break;
case ODataReaderState.ResourceEnd:
EndEntry(nodeStack, ref rootNode, odataReader.Item);
break;
case ODataReaderState.NestedResourceInfoStart:
StartNavigationLink(nodeStack, (odataReader.Item as ODataNestedResourceInfo).Name);
break;
case ODataReaderState.NestedResourceInfoEnd:
EndNavigationLink(nodeStack);
break;
}
}
return ODataResponse.FromNode(TypeCache, rootNode, responseMessage.Headers);
}
protected override void ConvertEntry(ResponseNode entryNode, object entry)
{
if (entry is not null)
{
var odataEntry = entry as ODataResource;
foreach (var property in odataEntry.Properties)
{
entryNode.Entry.Data.Add(property.Name, GetPropertyValue(((ODataProperty)property).Value));
}
entryNode.Entry.SetAnnotations(CreateAnnotations(odataEntry));
}
}
private static ODataFeedAnnotations CreateAnnotations(ODataResourceSetBase feed)
{
return new ODataFeedAnnotations()
{
Id = feed.Id?.AbsoluteUri,
Count = feed.Count,
DeltaLink = feed.DeltaLink,
NextPageLink = feed.NextPageLink,
InstanceAnnotations = feed.InstanceAnnotations,
};
}
private ODataEntryAnnotations CreateAnnotations(ODataResource odataEntry)
{
string? id = null;
Uri? readLink = null;
Uri? editLink = null;
string? etag = null;
if (_session.Adapter.GetMetadata().IsTypeWithId(odataEntry.TypeName))
{
try
{
// odataEntry.Id is null for transient entities (s. http://docs.oasis-open.org/odata/odata-json-format/v4.0/errata03/os/odata-json-format-v4.0-errata03-os-complete.html#_Toc453766634)
id = odataEntry.Id?.AbsoluteUri;
readLink = odataEntry.ReadLink;
editLink = odataEntry.EditLink;
etag = odataEntry.ETag;
}
catch (ODataException)
{
// Ignored
}
}
return new ODataEntryAnnotations
{
Id = id,
TypeName = odataEntry.TypeName,
ReadLink = readLink,
EditLink = editLink,
ETag = etag,
MediaResource = CreateAnnotations(odataEntry.MediaResource),
InstanceAnnotations = odataEntry.InstanceAnnotations,
};
}
private static ODataMediaAnnotations? CreateAnnotations(ODataStreamReferenceValue value)
{
return value is null ? null : new ODataMediaAnnotations
{
ContentType = value.ContentType,
ReadLink = value.ReadLink,
EditLink = value.EditLink,
ETag = value.ETag,
};
}
private object GetPropertyValue(object value)
{
if (value is ODataResource resource)
{
return resource.Properties.ToDictionary(x => x.Name, x => GetPropertyValue(((ODataProperty)x).Value));
}
else if (value is ODataCollectionValue collectionValue)
{
return collectionValue.Items.Select(GetPropertyValue).ToList();
}
else if (value is ODataEnumValue enumValue)
{
return enumValue.Value;
}
else if (value is ODataUntypedValue untypedValue)
{
var result = untypedValue.RawValue;
if (!string.IsNullOrEmpty(result))
{
// Remove extra quoting as has been read as a string
// Don't just replace \" in case we have embedded quotes
if (result.StartsWith("\"", StringComparison.Ordinal) && result.EndsWith("\"", StringComparison.Ordinal))
{
result = result.Substring(1, result.Length - 2);
}
}
return result;
}
else if (value is ODataStreamReferenceValue referenceValue)
{
return CreateAnnotations(referenceValue);
}
else
{
return value;
}
}
}
}