This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
MobileServiceClient.cs
682 lines (616 loc) · 30 KB
/
MobileServiceClient.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
// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices.Sync;
using Newtonsoft.Json.Linq;
namespace Microsoft.WindowsAzure.MobileServices
{
/// <summary>
/// Provides basic access to a Microsoft Azure Mobile Service.
/// </summary>
public class MobileServiceClient : IMobileServiceClient, IDisposable
{
private static HttpMethod defaultHttpMethod = HttpMethod.Post;
/// <summary>
/// Name of the config setting that stores the installation ID.
/// </summary>
private const string ConfigureAsyncInstallationConfigPath = "MobileServices.Installation.config";
/// <summary>
/// Name of the JSON member in the config setting that stores the
/// installation ID.
/// </summary>
private const string ConfigureAsyncApplicationIdKey = "applicationInstallationId";
/// <summary>
/// Default empty array of HttpMessageHandlers.
/// </summary>
private static readonly HttpMessageHandler[] EmptyHttpMessageHandlers = new HttpMessageHandler[0];
/// <summary>
/// The id used to identify this installation of the application to
/// provide telemetry data.
/// </summary>
internal string applicationInstallationId;
/// <summary>
/// Gets the Uri to the Mobile Services application that is provided by
/// the call to MobileServiceClient(...).
/// </summary>
public Uri ApplicationUri { get; private set; }
/// <summary>
/// Gets the Mobile Services application's name that is provided by the
/// call to MobileServiceClient(...).
/// </summary>
public string ApplicationKey { get; private set; }
/// <summary>
/// The current authenticated user provided after a successful call to
/// MobileServiceClient.Login().
/// </summary>
public MobileServiceUser CurrentUser { get; set; }
/// <summary>
/// Gets or sets the settings used for serialization.
/// </summary>
public MobileServiceJsonSerializerSettings SerializerSettings
{
get
{
return this.Serializer.SerializerSettings;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
this.Serializer.SerializerSettings = value;
}
}
/// <summary>
/// Instance of <see cref="IMobileServiceSyncContext"/>
/// </summary>
public IMobileServiceSyncContext SyncContext
{
get;
private set;
}
/// <summary>
/// Gets the serializer that is used with the table.
/// </summary>
internal MobileServiceSerializer Serializer { get; set; }
/// <summary>
/// Gets the <see cref="MobileServiceHttpClient"/> associated with this client.
/// </summary>
internal MobileServiceHttpClient HttpClient { get; private set; }
/// <summary>
/// Initializes a new instance of the MobileServiceClient class.
/// </summary>
/// <param name="applicationUrl">
/// The URI for the Microsoft Azure Mobile Service.
/// </param>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "Enables easier copy/paste getting started workflow")]
public MobileServiceClient(string applicationUrl)
: this(new Uri(applicationUrl))
{
}
/// <summary>
/// Initializes a new instance of the MobileServiceClient class.
/// </summary>
/// <param name="applicationUri">
/// The URI for the Microsoft Azure Mobile Service.
/// </param>
public MobileServiceClient(Uri applicationUri)
: this(applicationUri, null)
{
}
/// <summary>
/// Initializes a new instance of the MobileServiceClient class.
/// </summary>
/// <param name="applicationUrl">
/// The URI for the Microsoft Azure Mobile Service.
/// </param>
/// <param name="applicationKey">
/// The application key for the Microsoft Azure Mobile Service.
/// </param>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "Enables easier copy/paste getting started workflow")]
public MobileServiceClient(string applicationUrl, string applicationKey)
: this(new Uri(applicationUrl), applicationKey)
{
}
/// <summary>
/// Initializes a new instance of the MobileServiceClient class.
/// </summary>
/// <param name="applicationUri">
/// The URI for the Microsoft Azure Mobile Service.
/// </param>
/// <param name="applicationKey">
/// The application key for the Microsoft Azure Mobile Service.
/// </param>
public MobileServiceClient(Uri applicationUri, string applicationKey)
: this(applicationUri, applicationKey, null)
{
}
/// <summary>
/// Initializes a new instance of the MobileServiceClient class.
/// </summary>
/// <param name="applicationUrl">
/// The URI for the Microsoft Azure Mobile Service.
/// </param>
/// <param name="applicationKey">
/// The application key for the Microsoft Azure Mobile Service.
/// </param>
/// <param name="handlers">
/// Chain of <see cref="HttpMessageHandler" /> instances.
/// All but the last should be <see cref="DelegatingHandler"/>s.
/// </param>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "Enables easier copy/paste getting started workflow")]
public MobileServiceClient(string applicationUrl, string applicationKey, params HttpMessageHandler[] handlers)
: this(new Uri(applicationUrl), applicationKey, handlers)
{
}
/// <summary>
/// Initializes a new instance of the MobileServiceClient class.
/// </summary>
/// <param name="applicationUri">
/// The URI for the Microsoft Azure Mobile Service.
/// </param>
/// <param name="applicationKey">
/// The application key for the Microsoft Azure Mobile Service.
/// </param>
/// <param name="handlers">
/// Chain of <see cref="HttpMessageHandler" /> instances.
/// All but the last should be <see cref="DelegatingHandler"/>s.
/// </param>
public MobileServiceClient(Uri applicationUri, string applicationKey, params HttpMessageHandler[] handlers)
{
if (applicationUri == null)
{
throw new ArgumentNullException("applicationUri");
}
this.ApplicationUri = applicationUri;
this.ApplicationKey = applicationKey;
this.applicationInstallationId = GetApplicationInstallationId();
handlers = handlers ?? EmptyHttpMessageHandlers;
this.HttpClient = new MobileServiceHttpClient(handlers, this.ApplicationUri, this.applicationInstallationId, this.ApplicationKey);
this.Serializer = new MobileServiceSerializer();
this.SyncContext = new MobileServiceSyncContext(this);
}
/// <summary>
/// This is for unit testing only
/// </summary>
protected MobileServiceClient()
{
}
/// <summary>
/// Returns a <see cref="IMobileServiceTable"/> instance, which provides
/// untyped data operations for that table.
/// </summary>
/// <param name="tableName">
/// The name of the table.
/// </param>
/// <returns>
/// The table.
/// </returns>
public IMobileServiceTable GetTable(string tableName)
{
ValidateTableName(tableName);
return new MobileServiceTable(tableName, this);
}
/// <summary>
/// Returns a <see cref="IMobileServiceSyncTable"/> instance, which provides
/// untyped data operations for that table.
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <returns>The table.</returns>
public IMobileServiceSyncTable GetSyncTable(string tableName)
{
return GetSyncTable(tableName, MobileServiceTableKind.Table);
}
internal MobileServiceSyncTable GetSyncTable(string tableName, MobileServiceTableKind kind)
{
ValidateTableName(tableName);
return new MobileServiceSyncTable(tableName, kind, this);
}
/// <summary>
/// Returns a <see cref="IMobileServiceTable{T}"/> instance, which provides
/// strongly typed data operations for that table.
/// </summary>
/// <typeparam name="T">
/// The type of the instances in the table.
/// </typeparam>
/// <returns>
/// The table.
/// </returns>
public IMobileServiceTable<T> GetTable<T>()
{
string tableName = this.SerializerSettings.ContractResolver.ResolveTableName(typeof(T));
return new MobileServiceTable<T>(tableName, this);
}
/// <summary>
/// Returns a <see cref="IMobileServiceSyncTable{T}"/> instance, which provides
/// strongly typed data operations for local table.
/// </summary>
/// <typeparam name="T">
/// The type of the instances in the table.
/// </typeparam>
/// <returns>
/// The table.
/// </returns>
public IMobileServiceSyncTable<T> GetSyncTable<T>()
{
string tableName = this.SerializerSettings.ContractResolver.ResolveTableName(typeof(T));
return new MobileServiceSyncTable<T>(tableName, MobileServiceTableKind.Table, this);
}
/// <summary>
/// Logs a user into a Windows Azure Mobile Service with the provider and optional token object.
/// </summary>
/// <param name="provider">
/// Authentication provider to use.
/// </param>
/// <param name="token">
/// Provider specific object with existing OAuth token to log in with.
/// </param>
/// <remarks>
/// The token object needs to be formatted depending on the specific provider. These are some
/// examples of formats based on the providers:
/// <list type="bullet">
/// <item>
/// <term>MicrosoftAccount</term>
/// <description><code>{"authenticationToken":"<the_authentication_token>"}</code></description>
/// </item>
/// <item>
/// <term>Facebook</term>
/// <description><code>{"access_token":"<the_access_token>"}</code></description>
/// </item>
/// <item>
/// <term>Google</term>
/// <description><code>{"access_token":"<the_access_token>"}</code></description>
/// </item>
/// </list>
/// </remarks>
/// <returns>
/// Task that will complete when the user has finished authentication.
/// </returns>
[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Login", Justification = "Login is more appropriate than LogOn for our usage.")]
public Task<MobileServiceUser> LoginAsync(MobileServiceAuthenticationProvider provider, JObject token)
{
if (!Enum.IsDefined(typeof(MobileServiceAuthenticationProvider), provider))
{
throw new ArgumentOutOfRangeException("provider");
}
return this.LoginAsync(provider.ToString(), token);
}
/// <summary>
/// Logs a user into a Microsoft Azure Mobile Service with the provider and optional token object.
/// </summary>
/// <param name="provider">
/// Authentication provider to use.
/// </param>
/// <param name="token">
/// Provider specific object with existing OAuth token to log in with.
/// </param>
/// <remarks>
/// The token object needs to be formatted depending on the specific provider. These are some
/// examples of formats based on the providers:
/// <list type="bullet">
/// <item>
/// <term>MicrosoftAccount</term>
/// <description><code>{"authenticationToken":"<the_authentication_token>"}</code></description>
/// </item>
/// <item>
/// <term>Facebook</term>
/// <description><code>{"access_token":"<the_access_token>"}</code></description>
/// </item>
/// <item>
/// <term>Google</term>
/// <description><code>{"access_token":"<the_access_token>"}</code></description>
/// </item>
/// </list>
/// </remarks>
/// <returns>
/// Task that will complete when the user has finished authentication.
/// </returns>
public Task<MobileServiceUser> LoginAsync(string provider, JObject token)
{
if (token == null)
{
throw new ArgumentNullException("token");
}
MobileServiceTokenAuthentication auth = new MobileServiceTokenAuthentication(this, provider, token, parameters: null);
return auth.LoginAsync();
}
/// <summary>
/// Log a user out.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Logout", Justification = "Logout is preferred by design")]
public void Logout()
{
this.CurrentUser = null;
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using an HTTP POST.
/// </summary>
/// <typeparam name="T">The type of instance returned from the Microsoft Azure Mobile Service.</typeparam>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
public Task<T> InvokeApiAsync<T>(string apiName, CancellationToken cancellationToken = default(CancellationToken))
{
return this.InvokeApiAsync<string, T>(apiName, null, null, null, cancellationToken);
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using an HTTP POST with
/// support for sending HTTP content.
/// </summary>
/// <typeparam name="T">The type of instance sent to the Microsoft Azure Mobile Service.</typeparam>
/// <typeparam name="U">The type of instance returned from the Microsoft Azure Mobile Service.</typeparam>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="body">The value to be sent as the HTTP body.</param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
public Task<U> InvokeApiAsync<T, U>(string apiName, T body, CancellationToken cancellationToken = default(CancellationToken))
{
return this.InvokeApiAsync<T, U>(apiName, body, null, null, cancellationToken);
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HTTP Method.
/// Additional data can be passed using the query string.
/// </summary>
/// <typeparam name="T">The type of instance sent to the Microsoft Azure Mobile Service.</typeparam>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
public Task<T> InvokeApiAsync<T>(string apiName, HttpMethod method, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken))
{
return this.InvokeApiAsync<string, T>(apiName, null, method, parameters, cancellationToken);
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HTTP Method.
/// Additional data can be sent though the HTTP content or the query string.
/// </summary>
/// <typeparam name="T">The type of instance sent to the Microsoft Azure Mobile Service.</typeparam>
/// <typeparam name="U">The type of instance returned from the Microsoft Azure Mobile Service.</typeparam>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="body">The value to be sent as the HTTP body.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
public async Task<U> InvokeApiAsync<T, U>(string apiName, T body, HttpMethod method, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken))
{
if (string.IsNullOrWhiteSpace(apiName))
{
throw new ArgumentNullException("apiName");
}
MobileServiceSerializer serializer = this.Serializer;
string content = null;
if (body != null)
{
content = serializer.Serialize(body).ToString();
}
string response = await this.InternalInvokeApiAsync(apiName, content, method, parameters, MobileServiceFeatures.TypedApiCall, cancellationToken);
if (string.IsNullOrEmpty(response))
{
return default(U);
}
return serializer.Deserialize<U>(JToken.Parse(response));
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using an HTTP POST.
/// </summary>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns></returns>
public Task<JToken> InvokeApiAsync(string apiName, CancellationToken cancellationToken = default(CancellationToken))
{
return this.InvokeApiAsync(apiName, null, null, null, cancellationToken);
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using an HTTP POST, with
/// support for sending HTTP content.
/// </summary>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="body">The value to be sent as the HTTP body.</param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
public Task<JToken> InvokeApiAsync(string apiName, JToken body, CancellationToken cancellationToken = default(CancellationToken))
{
return this.InvokeApiAsync(apiName, body, defaultHttpMethod, null, cancellationToken);
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HTTP Method.
/// Additional data will sent to through the query string.
/// </summary>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
public Task<JToken> InvokeApiAsync(string apiName, HttpMethod method, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken))
{
return this.InvokeApiAsync(apiName, null, method, parameters, cancellationToken);
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HTTP method.
/// Additional data can be sent though the HTTP content or the query string.
/// </summary>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="body">The value to be sent as the HTTP body.</param>
/// <param name="method">The HTTP Method.</param>
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
public async Task<JToken> InvokeApiAsync(string apiName, JToken body, HttpMethod method, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken))
{
if (string.IsNullOrWhiteSpace(apiName))
{
throw new ArgumentNullException("apiName");
}
string content = null;
if (body != null)
{
switch (body.Type)
{
case JTokenType.Null:
content = "null";
break;
case JTokenType.Boolean:
content = body.ToString().ToLowerInvariant();
break;
default:
content = body.ToString();
break;
}
}
string response = await this.InternalInvokeApiAsync(apiName, content, method, parameters, MobileServiceFeatures.JsonApiCall, cancellationToken);
return response.ParseToJToken(this.SerializerSettings);
}
/// <summary>
/// Invokes a user-defined custom API of a Windows Azure Mobile Service using the specified HttpMethod.
/// Additional data can be sent though the HTTP content or the query string.
/// </summary>
/// <param name="apiName">The name of the custom AP.</param>
/// <param name="content">The HTTP content.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="requestHeaders">
/// A dictionary of user-defined headers to include in the HttpRequest.
/// </param>
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The HTTP Response from the custom api invocation.</returns>
public async Task<HttpResponseMessage> InvokeApiAsync(string apiName, HttpContent content, HttpMethod method, IDictionary<string, string> requestHeaders, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken))
{
method = method ?? defaultHttpMethod;
HttpResponseMessage response = await this.HttpClient.RequestAsync(method, CreateAPIUriString(apiName, parameters), this.CurrentUser, content, requestHeaders: requestHeaders, features: MobileServiceFeatures.GenericApiCall, cancellationToken: cancellationToken);
return response;
}
/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service.
/// </summary>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="content">The HTTP content, as a string, in json format.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="features">
/// Value indicating which features of the SDK are being used in this call. Useful for telemetry.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
private async Task<string> InternalInvokeApiAsync(string apiName, string content, HttpMethod method, IDictionary<string, string> parameters, MobileServiceFeatures features, CancellationToken cancellationToken = default(CancellationToken))
{
method = method ?? defaultHttpMethod;
if (parameters != null && parameters.Count > 0)
{
features |= MobileServiceFeatures.AdditionalQueryParameters;
}
MobileServiceHttpResponse response = await this.HttpClient.RequestAsync(method, CreateAPIUriString(apiName, parameters), this.CurrentUser, content, false, features: features, cancellationToken: cancellationToken);
return response.Content;
}
/// <summary>
/// Helper function to assemble the Uri for a given custom api.
/// </summary>
/// <param name="apiName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private string CreateAPIUriString(string apiName, IDictionary<string, string> parameters = null)
{
string uriFragment = apiName.StartsWith("/") ? apiName : string.Format(CultureInfo.InvariantCulture, "api/{0}", apiName);
string queryString = MobileServiceUrlBuilder.GetQueryString(parameters, useTableAPIRules: false);
return MobileServiceUrlBuilder.CombinePathAndQuery(uriFragment, queryString);
}
/// <summary>
/// Implemenation of <see cref="IDisposable"/>
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Implemenation of <see cref="IDisposable"/> for
/// derived classes to use.
/// </summary>
/// <param name="disposing">
/// Indicates if being called from the Dispose() method
/// or the finalizer.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
((MobileServiceSyncContext)this.SyncContext).Dispose();
// free managed resources
this.HttpClient.Dispose();
}
}
private static void ValidateTableName(string tableName)
{
if (tableName == null)
{
throw new ArgumentNullException("tableName");
}
if (string.IsNullOrWhiteSpace(tableName))
{
throw new ArgumentException(
string.Format(
CultureInfo.InvariantCulture,
"{0} cannot be null, empty or only whitespace.",
"tableName"));
}
}
/// <summary>
/// Gets the ID used to identify this installation of the
/// application to provide telemetry data. It will either be retrieved
/// from local settings or generated fresh.
/// </summary>
/// <returns>
/// An installation ID.
/// </returns>
private string GetApplicationInstallationId()
{
// Try to get the AppInstallationId from settings
string installationId = null;
object setting = null;
IApplicationStorage applicationStorage = Platform.Instance.ApplicationStorage;
if (applicationStorage.TryReadSetting(ConfigureAsyncInstallationConfigPath, out setting))
{
JToken config = null;
try
{
config = JToken.Parse(setting as string);
installationId = (string)config[ConfigureAsyncApplicationIdKey];
}
catch (Exception)
{
}
}
// Generate a new AppInstallationId if we failed to find one
if (installationId == null)
{
installationId = Guid.NewGuid().ToString();
JObject jobject = new JObject();
jobject[ConfigureAsyncApplicationIdKey] = installationId;
string configText = jobject.ToString();
applicationStorage.WriteSetting(ConfigureAsyncInstallationConfigPath, configText);
}
return installationId;
}
}
}