-
Notifications
You must be signed in to change notification settings - Fork 820
/
AsyncServerClient.cs
1021 lines (918 loc) · 33 KB
/
AsyncServerClient.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------
// <copyright file="AsyncServerClient.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using Raven.Abstractions.Exceptions;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.Json;
using Raven.Client.Connection.Async;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Browser;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Raven.Abstractions.Data;
using Raven.Client.Connection;
using Raven.Client.Connection.Profiling;
using Raven.Client.Exceptions;
using Raven.Client.Silverlight.Data;
using Raven.Client.Document;
using Raven.Abstractions.Commands;
using Raven.Abstractions.Indexing;
using Raven.Json.Linq;
using Raven.Client.Extensions;
#if !NET_3_5
namespace Raven.Client.Silverlight.Connection.Async
{
/// <summary>
/// Access the database commands in async fashion
/// </summary>
public class AsyncServerClient : IAsyncDatabaseCommands
{
private readonly string url;
private readonly ICredentials credentials;
private readonly HttpJsonRequestFactory jsonRequestFactory;
private readonly Guid? sessionId;
private readonly DocumentConvention convention;
private readonly ProfilingInformation profilingInformation;
/// <summary>
/// Get the current json request factory
/// </summary>
public HttpJsonRequestFactory JsonRequestFactory
{
get { return jsonRequestFactory; }
}
/// <summary>
/// Initializes a new instance of the <see cref="AsyncServerClient"/> class.
/// </summary>
public AsyncServerClient(string url, DocumentConvention convention, ICredentials credentials, HttpJsonRequestFactory jsonRequestFactory, Guid? sessionId)
{
profilingInformation = ProfilingInformation.CreateProfilingInformation(sessionId);
this.url = url.EndsWith("/") ? url.Substring(0, url.Length - 1) : url;
this.convention = convention;
this.credentials = credentials;
this.jsonRequestFactory = jsonRequestFactory;
this.sessionId = sessionId;
// required to ensure just a single auth dialog
Task task = jsonRequestFactory.CreateHttpJsonRequest(this, (url + "/docs?pageSize=0").NoCache(), "GET", credentials, convention)
.ReadResponseStringAsync();
jsonRequestFactory.ConfigureRequest += (sender, args) =>
{
args.JsonRequest.WaitForTask = task;
};
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
}
public HttpJsonRequest CreateRequest(string relativeUrl, string method)
{
return jsonRequestFactory.CreateHttpJsonRequest(this, url + relativeUrl, method, credentials, convention);
}
/// <summary>
/// Create a new instance of <see cref="IAsyncDatabaseCommands"/> that will interacts
/// with the specified database
/// </summary>
public IAsyncDatabaseCommands ForDatabase(string database)
{
var databaseUrl = RootDatabaseUrl;
databaseUrl = databaseUrl + "databases/" + database + "/";
return new AsyncServerClient(databaseUrl, convention, credentials, jsonRequestFactory, sessionId)
{
operationsHeaders = operationsHeaders
};
}
/// <summary>
/// Create a new instance of <see cref="IAsyncDatabaseCommands"/> that will interacts
/// with the default database
/// </summary>
public IAsyncDatabaseCommands ForDefaultDatabase()
{
var rootDatabaseUrl = RootDatabaseUrl;
if (rootDatabaseUrl == url)
return this;
return new AsyncServerClient(rootDatabaseUrl, convention, credentials, jsonRequestFactory, sessionId)
{
operationsHeaders = operationsHeaders
};
}
private string RootDatabaseUrl
{
get
{
var databaseUrl = url;
var indexOfDatabases = databaseUrl.IndexOf("/databases/", StringComparison.Ordinal);
if (indexOfDatabases != -1)
databaseUrl = databaseUrl.Substring(0, indexOfDatabases);
if (databaseUrl.EndsWith("/") == false)
databaseUrl += "/";
return databaseUrl;
}
}
/// <summary>
/// Returns a new <see cref="IAsyncDatabaseCommands "/> using the specified credentials
/// </summary>
/// <param name="credentialsForSession">The credentials for session.</param>
public IAsyncDatabaseCommands With(ICredentials credentialsForSession)
{
return new AsyncServerClient(url, convention, credentialsForSession, jsonRequestFactory, sessionId);
}
/// <summary>
/// Create a new instance of <see cref="IAsyncDatabaseCommands"/> that will interact
/// with the root database. Useful if the database has works against a tenant database.
/// </summary>
public IAsyncDatabaseCommands GetRootDatabase()
{
var indexOfDatabases = url.IndexOf("/databases/", StringComparison.Ordinal);
if (indexOfDatabases == -1)
return this;
return new AsyncServerClient(url.Substring(0, indexOfDatabases), convention, credentials, jsonRequestFactory, sessionId);
}
private IDictionary<string, string> operationsHeaders = new Dictionary<string, string>();
/// <summary>
/// Gets or sets the operations headers.
/// </summary>
/// <value>The operations headers.</value>
public IDictionary<string, string> OperationsHeaders
{
get { return operationsHeaders; }
}
/// <summary>
/// Begins an async get operation
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public Task<JsonDocument> GetAsync(string key)
{
EnsureIsNotNullOrEmpty(key, "key");
key = key.Replace("\\", @"/"); //NOTE: the present of \ causes the SL networking stack to barf, even though the Uri seemingly makes this translation itself
var request = url.Docs(key)
.NoCache()
.ToJsonRequest(this, credentials, convention);
return request
.ReadResponseStringAsync()
.ContinueWith(task =>
{
try
{
var responseString = task.Result;
return new JsonDocument
{
DataAsJson = RavenJObject.Parse(responseString),
NonAuthoritiveInformation = request.ResponseStatusCode == HttpStatusCode.NonAuthoritativeInformation,
Key = key,
LastModified = DateTime.ParseExact(request.ResponseHeaders[Constants.LastModified].First(), "r", CultureInfo.InvariantCulture).ToLocalTime(),
Etag = new Guid(request.ResponseHeaders["ETag"].First()),
Metadata = request.ResponseHeaders.FilterHeaders(isServerDocument: false)
};
}
catch (AggregateException e)
{
var webException = e.ExtractSingleInnerException() as WebException;
if (webException != null)
{
if (HandleWebExceptionForGetAsync(key, webException))
return null;
}
throw;
}
catch (WebException e)
{
if (HandleWebExceptionForGetAsync(key, e))
return null;
throw;
}
});
}
private static bool HandleWebExceptionForGetAsync(string key, WebException e)
{
var httpWebResponse = e.Response as HttpWebResponse;
if (httpWebResponse == null)
{
return false;
}
if (httpWebResponse.StatusCode == HttpStatusCode.NotFound)
{
return true;
}
if (httpWebResponse.StatusCode == HttpStatusCode.Conflict)
{
var conflicts = new StreamReader(httpWebResponse.GetResponseStream());
var conflictsDoc = RavenJObject.Load(new JsonTextReader(conflicts));
var conflictIds = conflictsDoc.Value<RavenJArray>("Conflicts").Select(x => x.Value<string>()).ToArray();
throw new ConflictException("Conflict detected on " + key +
", conflict must be resolved before the document will be accessible")
{
ConflictedVersionIds = conflictIds
};
}
return false;
}
private T AttemptToProcessResponse<T>(Func<T> process) where T : class
{
try
{
return process();
}
catch (AggregateException e)
{
var webException = e.ExtractSingleInnerException() as WebException;
if (webException == null) throw;
if (HandleException(webException)) return null;
throw;
}
catch (WebException e)
{
if (HandleException(e)) return null;
throw;
}
}
/// <summary>
/// Attempts to handle an exception raised when receiving a response from the server
/// </summary>
/// <param name="e">The exception to handle</param>
/// <returns>returns true if the exception is handled, false if it should be thrown</returns>
private bool HandleException(WebException e)
{
var httpWebResponse = e.Response as HttpWebResponse;
if (httpWebResponse == null)
{
return false;
}
if (httpWebResponse.StatusCode == HttpStatusCode.InternalServerError)
{
var content = new StreamReader(httpWebResponse.GetResponseStream());
var jo = RavenJObject.Load(new JsonTextReader(content));
var error = jo.Deserialize<ServerRequestError>(convention);
throw new WebException(error.Error);
}
return false;
}
/// <summary>
/// Perform a single POST requst containing multiple nested GET requests
/// </summary>
public Task<GetResponse[]> MultiGetAsync(GetRequest[] requests)
{
var postedData = JsonConvert.SerializeObject(requests);
var httpJsonRequest = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/multi_get/", "POST",
credentials, convention);
return httpJsonRequest.WriteAsync(postedData)
.ContinueWith(
task =>
{
task.Wait();// will throw if write errored
return httpJsonRequest.ReadResponseStringAsync()
.ContinueWith(replyTask => JsonConvert.DeserializeObject<GetResponse[]>(replyTask.Result));
})
.Unwrap();
}
public Task<LogItem[]> GetLogsAsync(bool errorsOnly)
{
var requestUri = url + "/logs";
if (errorsOnly)
requestUri += "?type=error";
var request = jsonRequestFactory.CreateHttpJsonRequest(this, requestUri.NoCache(), "GET", credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
return request.ReadResponseStringAsync()
.ContinueWith(task =>
{
using (var reader = new JsonTextReader(new StringReader(task.Result)))
{
return convention.CreateSerializer().Deserialize<LogItem[]>(reader);
}
});
}
public Task<LicensingStatus> GetLicenseStatus()
{
var request = jsonRequestFactory.CreateHttpJsonRequest(this, (url + "/license/status").NoCache(), "GET", credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
return request.ReadResponseStringAsync()
.ContinueWith(task =>
{
using (var reader = new JsonTextReader(new StringReader(task.Result)))
{
return convention.CreateSerializer().Deserialize<LicensingStatus>(reader);
}
});
}
public Task<BuildNumber> GetBuildNumber()
{
var request = jsonRequestFactory.CreateHttpJsonRequest(this, (url + "/build/version").NoCache(), "GET", credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
return request.ReadResponseStringAsync()
.ContinueWith(task =>
{
using (var reader = new JsonTextReader(new StringReader(task.Result)))
{
return convention.CreateSerializer().Deserialize<BuildNumber>(reader);
}
});
}
/// <summary>
/// Using the given Index, calculate the facets as per the specified doc
/// </summary>
public Task<IDictionary<string, IEnumerable<FacetValue>>> GetFacetsAsync(string index, IndexQuery query, string facetSetupDoc)
{
var requestUri = url + string.Format("/facets/{0}?facetDoc={1}&query={2}",
Uri.EscapeUriString(index),
Uri.EscapeDataString(facetSetupDoc),
Uri.EscapeDataString(query.Query));
var request = jsonRequestFactory.CreateHttpJsonRequest(this, requestUri.NoCache(), "GET", credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
return request.ReadResponseStringAsync()
.ContinueWith(task =>
{
using (var reader = new JsonTextReader(new StringReader(task.Result)))
{
var json = (RavenJObject)RavenJToken.Load(reader);
var jsonAsType = json.JsonDeserialization<IDictionary<string, IEnumerable<FacetValue>>>();
return jsonAsType;
}
});
}
/// <summary>
/// Begins an async multi get operation
/// </summary>
public Task<MultiLoadResult> GetAsync(string[] keys, string[] includes)
{
var path = url + "/queries/?";
if (includes != null && includes.Length > 0)
{
path += string.Join("&", includes.Select(x => "include=" + x).ToArray());
}
// if it is too big, we drop to POST (note that means that we can't use the HTTP cache any longer)
// we are fine with that, requests to load > 128 items are going to be rare
HttpJsonRequest request;
if (keys.Length < 128)
{
path += "&" + string.Join("&", keys.Select(x => "id=" + x).ToArray());
request = jsonRequestFactory.CreateHttpJsonRequest(this, path.NoCache(), "GET", credentials, convention);
return request.ReadResponseStringAsync()
.ContinueWith(task => CompleteMultiGet(task));
}
request = jsonRequestFactory.CreateHttpJsonRequest(this, path, "POST", credentials, convention);
return request.WriteAsync(new JArray(keys).ToString(Formatting.None))
.ContinueWith(writeTask => request.ReadResponseStringAsync())
.ContinueWith(task => CompleteMultiGet(task.Result));
}
private static MultiLoadResult CompleteMultiGet(Task<string> task)
{
try
{
var result = RavenJObject.Parse(task.Result);
return new MultiLoadResult
{
Includes = result.Value<RavenJArray>("Includes").Cast<RavenJObject>().ToList(),
Results = result.Value<RavenJArray>("Results").Cast<RavenJObject>().ToList()
};
}
catch (WebException e)
{
var httpWebResponse = e.Response as HttpWebResponse;
if (httpWebResponse == null ||
httpWebResponse.StatusCode != HttpStatusCode.Conflict)
throw;
throw ThrowConcurrencyException(e);
}
}
/// <summary>
/// Begins an async get operation for documents
/// </summary>
/// <param name="start">Paging start</param>
/// <param name="pageSize">Size of the page.</param>
/// <remarks>
/// This is primarily useful for administration of a database
/// </remarks>
public Task<JsonDocument[]> GetDocumentsAsync(int start, int pageSize)
{
return url.Docs(start, pageSize)
.NoCache()
.ToJsonRequest(this, credentials, convention)
.ReadResponseStringAsync()
.ContinueWith(task => RavenJArray.Parse(task.Result)
.Cast<RavenJObject>()
.ToJsonDocuments()
.ToArray());
}
/// <summary>
/// Begins an async get operation for documents whose id starts with the specified prefix
/// </summary>
/// <param name="prefix">Prefix that the ids begin with.</param>
/// <param name="start">Paging start.</param>
/// <param name="pageSize">Size of the page.</param>
/// <remarks>
/// This is primarily useful for administration of a database
/// </remarks>
public Task<JsonDocument[]> GetDocumentsStartingWithAsync(string prefix, int start, int pageSize)
{
return url.DocsStartingWith(prefix, start, pageSize)
.NoCache()
.ToJsonRequest(this, credentials, convention)
.ReadResponseStringAsync()
.ContinueWith(task => RavenJArray.Parse(task.Result)
.Cast<RavenJObject>()
.ToJsonDocuments()
.ToArray());
}
/// <summary>
/// Begins the async query.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="query">The query.</param>
/// <param name="includes">The include paths</param>
/// <returns></returns>
public Task<QueryResult> QueryAsync(string index, IndexQuery query, string[] includes)
{
EnsureIsNotNullOrEmpty(index, "index");
var path = query.GetIndexQueryUrl(url, index, "indexes");
if (includes != null && includes.Length > 0)
{
path += "&" + string.Join("&", includes.Select(x => "include=" + x).ToArray());
}
var request = jsonRequestFactory.CreateHttpJsonRequest(this, path.NoCache(), "GET", credentials, convention);
return request.ReadResponseStringAsync()
.ContinueWith(task => AttemptToProcessResponse(() =>
{
RavenJObject json;
using (var reader = new JsonTextReader(new StringReader(task.Result)))
json = (RavenJObject)RavenJToken.ReadFrom(reader);
return SerializationHelper.ToQueryResult(json, request.ResponseHeaders["ETag"].First());
}));
}
/// <summary>
/// Deletes the document for the specified id asyncronously
/// </summary>
/// <param name="id">The id.</param>
public Task DeleteDocumentAsync(string id)
{
return url.Docs(id)
.ToJsonRequest(this, credentials, convention, OperationsHeaders, "DELETE")
.ReadResponseStringAsync();
}
/// <summary>
/// Puts the document with the specified key in the database
/// </summary>
/// <param name="key">The key.</param>
/// <param name="etag">The etag.</param>
/// <param name="document">The document.</param>
/// <param name="metadata">The metadata.</param>
public Task<PutResult> PutAsync(string key, Guid? etag, RavenJObject document, RavenJObject metadata)
{
if (metadata == null)
metadata = new RavenJObject();
var method = String.IsNullOrEmpty(key) ? "POST" : "PUT";
if (etag != null)
metadata["ETag"] = new RavenJValue(etag.Value.ToString());
var request = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/docs/" + key, method, metadata, credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
return request.WriteAsync(document.ToString())
.ContinueWith(task =>
{
if (task.Exception != null)
throw new InvalidOperationException("Unable to write to server", task.Exception);
return request.ReadResponseStringAsync()
.ContinueWith(task1 =>
{
try
{
return JsonConvert.DeserializeObject<PutResult>(task1.Result, new JsonEnumConverter(), new JsonToJsonConverter());
}
catch (AggregateException e)
{
var webexception = e.ExtractSingleInnerException() as WebException;
if (ShouldThrowForPutAsync(webexception)) throw;
throw ThrowConcurrencyException(webexception);
}
catch (WebException e)
{
if (ShouldThrowForPutAsync(e)) throw;
throw ThrowConcurrencyException(e);
}
});
})
.Unwrap();
}
static bool ShouldThrowForPutAsync(WebException e)
{
if (e == null) return true;
var httpWebResponse = e.Response as HttpWebResponse;
return (httpWebResponse == null ||
httpWebResponse.StatusCode != HttpStatusCode.Conflict);
}
/// <summary>
/// Gets the index definition for the specified name asyncronously
/// </summary>
/// <param name="name">The name.</param>
public Task<IndexDefinition> GetIndexAsync(string name)
{
return url.IndexDefinition(name)
.NoCache()
.ToJsonRequest(this, credentials, convention).ReadResponseStringAsync()
.ContinueWith(task =>
{
var json = JObject.Parse(task.Result);
//NOTE: To review, I'm not confidence this is the correct way to deserialize the index definition
return JsonConvert.DeserializeObject<IndexDefinition>(json["Index"].ToString(), new JsonToJsonConverter());
});
}
/// <summary>
/// Puts the index definition for the specified name asyncronously
/// </summary>
/// <param name="name">The name.</param>
/// <param name="indexDef">The index def.</param>
/// <param name="overwrite">Should overwrite index</param>
public Task<string> PutIndexAsync(string name, IndexDefinition indexDef, bool overwrite)
{
string requestUri = url + "/indexes/" + Uri.EscapeUriString(name);
var webRequest = requestUri
.ToJsonRequest(this, credentials, convention, OperationsHeaders, "HEAD");
return webRequest.ReadResponseStringAsync()
.ContinueWith(task =>
{
try
{
if (overwrite == false)
throw new InvalidOperationException("Cannot put index: " + name + ", index already exists");
}
catch (AggregateException e)
{
var webException = e.ExtractSingleInnerException() as WebException;
if (ShouldThrowForPutIndexAsync(webException))
throw;
}
catch (WebException e)
{
if (ShouldThrowForPutIndexAsync(e))
throw;
}
var request = jsonRequestFactory.CreateHttpJsonRequest(this, requestUri, "PUT", credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
var serializeObject = JsonConvert.SerializeObject(indexDef, new JsonEnumConverter());
return request
.WriteAsync(serializeObject)
.ContinueWith(writeTask => AttemptToProcessResponse(() => request
.ReadResponseStringAsync()
.ContinueWith(readStrTask => AttemptToProcessResponse(() =>
{
//NOTE: JsonConvert.DeserializeAnonymousType() doesn't work in Silverlight because the ctr is private!
var obj = JsonConvert.DeserializeObject<IndexContainer>(readStrTask.Result, new JsonToJsonConverter());
return obj.Index;
})))
).Unwrap();
}).Unwrap();
}
/// <summary>
/// Used for deserialization only :-P
/// </summary>
public class IndexContainer
{
public string Index { get; set; }
}
/// <summary>
/// Deletes the index definition for the specified name asyncronously
/// </summary>
/// <param name="name">The name.</param>
public Task DeleteIndexAsync(string name)
{
return url.Indexes(name)
.ToJsonRequest(this, credentials, convention, OperationsHeaders, "DELETE")
.ReadResponseStringAsync();
}
private static bool ShouldThrowForPutIndexAsync(WebException e)
{
if (e == null) return true;
var response = e.Response as HttpWebResponse;
return (response == null || response.StatusCode != HttpStatusCode.NotFound);
}
/// <summary>
/// Gets the index names from the server asyncronously
/// </summary>
/// <param name="start">Paging start</param>
/// <param name="pageSize">Size of the page.</param>
public Task<string[]> GetIndexNamesAsync(int start, int pageSize)
{
return url.IndexNames(start, pageSize)
.NoCache()
.ToJsonRequest(this, credentials, convention)
.ReadResponseStringAsync()
.ContinueWith(task =>
{
var json = RavenJArray.Parse(task.Result);
return json.Select(x => x.Value<string>()).ToArray();
});
}
/// <summary>
/// Gets the indexes from the server asyncronously
/// </summary>
/// <param name="start">Paging start</param>
/// <param name="pageSize">Size of the page.</param>
public Task<IndexDefinition[]> GetIndexesAsync(int start, int pageSize)
{
var url2 = (url + "/indexes/?start=" + start + "&pageSize=" + pageSize).NoCache();
var request = jsonRequestFactory.CreateHttpJsonRequest(this, url2, "GET", credentials, convention);
return request.ReadResponseStringAsync()
.ContinueWith(task =>
{
var json = RavenJArray.Parse(task.Result);
//NOTE: To review, I'm not confidence this is the correct way to deserialize the index definition
return json
.Select(x => JsonConvert.DeserializeObject<IndexDefinition>(((RavenJObject)x)["definition"].ToString(), new JsonToJsonConverter()))
.ToArray();
});
}
/// <summary>
/// Resets the specified index asyncronously
/// </summary>
/// <param name="name">The name.</param>
public Task ResetIndexAsync(string name)
{
throw new NotImplementedException();
}
private void AddOperationHeaders(HttpWebRequest webRequest)
{
foreach (var header in OperationsHeaders)
{
webRequest.Headers[header.Key] = header.Value;
}
}
/// <summary>
/// Returns a list of suggestions based on the specified suggestion query.
/// </summary>
/// <param name="index">The index to query for suggestions</param>
/// <param name="suggestionQuery">The suggestion query.</param>
public Task<SuggestionQueryResult> SuggestAsync(string index, SuggestionQuery suggestionQuery)
{
if (suggestionQuery == null) throw new ArgumentNullException("suggestionQuery");
var requestUri = url + string.Format("/suggest/{0}?term={1}&field={2}&max={3}&distance={4}&accuracy={5}",
Uri.EscapeUriString(index),
Uri.EscapeDataString(suggestionQuery.Term),
Uri.EscapeDataString(suggestionQuery.Field),
Uri.EscapeDataString(suggestionQuery.MaxSuggestions.ToString()),
Uri.EscapeDataString(suggestionQuery.Distance.ToString()),
Uri.EscapeDataString(suggestionQuery.Accuracy.ToString()));
var request = jsonRequestFactory.CreateHttpJsonRequest(this, requestUri.NoCache(), "GET", credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
return request.ReadResponseStringAsync()
.ContinueWith(task =>
{
using (var reader = new JsonTextReader(new StringReader(task.Result)))
{
var json = (RavenJObject)RavenJToken.Load(reader);
return new SuggestionQueryResult
{
Suggestions = ((RavenJArray)json["Suggestions"]).Select(x => x.Value<string>()).ToArray(),
};
}
});
}
/// <summary>
/// Begins the async batch operation
/// </summary>
/// <param name="commandDatas">The command data.</param>
/// <returns></returns>
public Task<BatchResult[]> BatchAsync(ICommandData[] commandDatas)
{
var metadata = new RavenJObject();
var req = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/bulk_docs", "POST", metadata, credentials, convention);
var jArray = new RavenJArray(commandDatas.Select(x => x.ToJson()));
return req.WriteAsync(jArray.ToString(Formatting.None))
.ContinueWith(writeTask => req.ReadResponseStringAsync())
.ContinueWith(task =>
{
string response;
try
{
response = task.Result.Result;
}
catch (WebException e)
{
var httpWebResponse = e.Response as HttpWebResponse;
if (httpWebResponse == null ||
httpWebResponse.StatusCode != HttpStatusCode.Conflict)
throw;
throw ThrowConcurrencyException(e);
}
return JsonConvert.DeserializeObject<BatchResult[]>(response, new JsonToJsonConverter());
});
}
public class ConcurrencyExceptionResult
{
private readonly string url1;
private readonly Guid actualETag1;
private readonly Guid expectedETag1;
private readonly string error1;
public string url
{
get { return url1; }
}
public Guid actualETag
{
get { return actualETag1; }
}
public Guid expectedETag
{
get { return expectedETag1; }
}
public string error
{
get { return error1; }
}
public ConcurrencyExceptionResult(string url, Guid actualETag, Guid expectedETag, string error)
{
url1 = url;
actualETag1 = actualETag;
expectedETag1 = expectedETag;
error1 = error;
}
}
private static Exception ThrowConcurrencyException(WebException e)
{
using (var sr = new StreamReader(e.Response.GetResponseStream()))
{
var text = sr.ReadToEnd();
var errorResults = JsonConvert.DeserializeAnonymousType(text, new ConcurrencyExceptionResult((string)null, Guid.Empty, Guid.Empty, (string)null));
return new ConcurrencyException(errorResults.error)
{
ActualETag = errorResults.actualETag,
ExpectedETag = errorResults.expectedETag
};
}
}
private static void EnsureIsNotNullOrEmpty(string key, string argName)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentException("Key cannot be null or empty", argName);
}
/// <summary>
/// Begins retrieving the statistics for the database
/// </summary>
/// <returns></returns>
public Task<DatabaseStatistics> GetStatisticsAsync()
{
return url.Stats()
.NoCache()
.ToJsonRequest(this, credentials, convention)
.AddOperationHeader("Raven-Timer-Request", "true")
.ReadResponseStringAsync()
.ContinueWith(task =>
{
var response = task.Result;
var jo = RavenJObject.Parse(response);
return jo.Deserialize<DatabaseStatistics>(convention);
});
}
/// <summary>
/// Gets the list of databases from the server asyncronously
/// </summary>
public Task<string[]> GetDatabaseNamesAsync()
{
return url.Databases()
.NoCache()
.ToJsonRequest(this, credentials, convention)
.ReadResponseStringAsync()
.ContinueWith(task =>
{
var json = (RavenJArray)RavenJToken.Parse(task.Result);
return json
.Select(x => x.Value<RavenJObject>("@metadata").Value<string>("@id").Replace("Raven/Databases/", string.Empty))
.ToArray();
});
}
/// <summary>
/// Puts the attachment with the specified key asyncronously
/// </summary>
/// <param name="key">The key.</param>
/// <param name="etag">The etag.</param>
/// <param name="data">The data.</param>
/// <param name="metadata">The metadata.</param>
public Task PutAttachmentAsync(string key, Guid? etag, byte[] data, RavenJObject metadata)
{
if (metadata == null)
metadata = new RavenJObject();
if (etag != null)
metadata["ETag"] = new RavenJValue(etag.Value.ToString());
var request = jsonRequestFactory.CreateHttpJsonRequest(this, url.Static(key), "PUT", metadata, credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
return request
.WriteAsync(data)
.ContinueWith(write =>
{
if (write.Exception != null)
throw new InvalidOperationException("Unable to write to server");
return request.ReadResponseStringAsync();
}).Unwrap();
}
/// <summary>
/// Gets the attachment by the specified key asyncronously
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public Task<Attachment> GetAttachmentAsync(string key)
{
EnsureIsNotNullOrEmpty(key, "key");
var request = url.Static(key)
.ToJsonRequest(this, credentials, convention);
return request
.ReadResponseBytesAsync()
.ContinueWith(task =>
{
try
{
var buffer = task.Result;
return new Attachment
{
Data = () => new MemoryStream(buffer),
Etag = new Guid(request.ResponseHeaders["ETag"].First()),
Metadata = request.ResponseHeaders.FilterHeaders(isServerDocument: false)
};
}
catch (AggregateException e)
{
var webException = e.ExtractSingleInnerException() as WebException;
if (webException != null)
{
if (HandleWebExceptionForGetAsync(key, webException))
return null;
}
throw;
}
catch (WebException e)
{
if (HandleWebExceptionForGetAsync(key, e))
return null;
throw;
}
});
}
/// <summary>
/// Deletes the attachment with the specified key asyncronously
/// </summary>
/// <param name="key">The key.</param>
/// <param name="etag">The etag.</param>
public Task DeleteAttachmentAsync(string key, Guid? etag)
{
var metadata = new RavenJObject();
if (etag != null)
metadata["ETag"] = new RavenJValue(etag.Value.ToString());
var request = jsonRequestFactory.CreateHttpJsonRequest(this, url.Static(key), "DELETE", metadata, credentials, convention);
request.AddOperationHeaders(OperationsHeaders);
return request.ReadResponseStringAsync();
}
/// <summary>
/// Ensures that the silverlight startup tasks have run
/// </summary>
public Task EnsureSilverlightStartUpAsync()
{
return url
.SilverlightEnsuresStartup()
.NoCache()
.ToJsonRequest(this, credentials, convention)
.ReadResponseBytesAsync();
}
///<summary>
/// Get the possible terms for the specified field in the index asynchronously
/// You can page through the results by use fromValue parameter as the
/// starting point for the next query
///</summary>
///<returns></returns>
public Task<string[]> GetTermsAsync(string index, string field, string fromValue, int pageSize)
{
return url.Terms(index, field, fromValue, pageSize)
.NoCache()
.ToJsonRequest(this, credentials, convention)
.ReadResponseStringAsync()
.ContinueWith(task =>
{
using (var reader = new JsonTextReader(new StringReader(task.Result)))
{
var json = RavenJArray.Load(reader);
return json.Select(x => x.Value<string>()).ToArray();
}
});