/// <summary> /// Inserts an <paramref name="instance"/> into the table. /// </summary> /// <param name="instance"> /// The instance to insert into the table. /// </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> /// <returns> /// A task that will complete when the insert finishes. /// </returns> internal async Task <JToken> InsertAsync(JObject instance, IDictionary <string, string> parameters, MobileServiceFeatures features) { if (instance == null) { throw new ArgumentNullException("instance"); } // Make sure the instance doesn't have an int id set for an insertion object id = MobileServiceSerializer.GetId(instance, ignoreCase: false, allowDefault: true); bool isStringIdOrDefaultIntId = id is string || MobileServiceSerializer.IsDefaultId(id); if (!isStringIdOrDefaultIntId) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "Cannot insert if the {0} member is already set.", MobileServiceSystemColumns.Id), "instance"); } features = this.AddRequestFeatures(features, parameters); string uriString = GetUri(this.TableName, null, parameters); return(await this.TransformHttpException(async() => { MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Post, uriString, this.MobileServiceClient.CurrentUser, instance.ToString(Formatting.None), true, features: this.Features | features); return GetJTokenFromResponse(response); })); }
/// <summary> /// Excutes a query against the table. /// </summary> /// <param name="query"> /// A query to execute. /// </param> /// <param name="parameters"> /// A dictionary of user-defined parameters and values to include in /// the request URI query string. /// </param> /// <returns> /// A task that will return with results when the query finishes. /// </returns> public async Task <JToken> ReadAsync(string query, IDictionary <string, string> parameters) { parameters = AddSystemProperties(this.SystemProperties, parameters); string uriPath = MobileServiceUrlBuilder.CombinePaths(TableRouteSeparatorName, this.TableName); string parametersString = MobileServiceUrlBuilder.GetQueryString(parameters); // Concatenate the query and the user-defined query string parameters if (!string.IsNullOrEmpty(parametersString)) { if (!string.IsNullOrEmpty(query)) { query += '&' + parametersString; } else { query = parametersString; } } string uriString = MobileServiceUrlBuilder.CombinePathAndQuery(uriPath, query); MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Get, uriString, this.MobileServiceClient.CurrentUser, null, true); return(response.Content.ParseToJToken(this.MobileServiceClient.SerializerSettings)); }
/// <summary> /// Performs a web request and includes the standard Mobile Services /// headers. It will use an HttpClient without any http handlers. /// </summary> /// <param name="method"> /// The HTTP method used to request the resource. /// </param> /// <param name="uriPathAndQuery"> /// The URI of the resource to request (relative to the Mobile Services /// runtime). /// </param> /// <param name="user"> /// The object representing the user on behalf of whom the request will be sent. /// </param> /// <param name="content"> /// Optional content to send to the resource. /// </param> /// <param name="features"> /// Optional MobileServiceFeatures used for telemetry purpose. /// </param>> /// <returns> /// The content of the response as a string. /// </returns> public async Task <string> RequestWithoutHandlersAsync(HttpMethod method, string uriPathAndQuery, MobileServiceUser user, string content = null, MobileServiceFeatures features = MobileServiceFeatures.None) { IDictionary <string, string> requestHeaders = FeaturesHelper.AddFeaturesHeader(requestHeaders: null, features: features); MobileServiceHttpResponse response = await this.RequestAsync(false, method, uriPathAndQuery, user, content, false, requestHeaders); return(response.Content); }
/// <summary> /// Inserts an <paramref name="instance"/> into the table. /// </summary> /// <param name="instance"> /// The instance to insert into the table. /// </param> /// <param name="parameters"> /// A dictionary of user-defined parameters and values to include in /// the request URI query string. /// </param> /// <returns> /// A task that will complete when the insert finishes. /// </returns> public async Task <JToken> InsertAsync(JObject instance, IDictionary <string, string> parameters) { if (instance == null) { throw new ArgumentNullException("instance"); } // Make sure the instance doesn't have an int id set for an insertion object id = MobileServiceSerializer.GetId(instance, ignoreCase: false, allowDefault: true); bool isStringIdOrDefaultIntId = id is string || MobileServiceSerializer.IsDefaultId(id); if (!isStringIdOrDefaultIntId) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceTable_InsertWithExistingId, MobileServiceSerializer.IdPropertyName), "instance"); } parameters = AddSystemProperties(this.SystemProperties, parameters); string uriString = GetUri(this.TableName, null, parameters); MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Post, uriString, this.MobileServiceClient.CurrentUser, instance.ToString(Formatting.None), true); return(GetJTokenFromResponse(response)); }
/// <summary> /// Invokes a user-defined custom API of a Windows 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> /// <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 = null) { method = method ?? defaultHttpMethod; MobileServiceHttpResponse response = await this.HttpClient.RequestAsync(method, CreateAPIUriString(apiName, parameters), this.CurrentUser, content, false); return(response.Content); }
/// <summary> /// Parses the response content into a JToken and adds the version system property /// if the ETag was returned from the server. /// </summary> /// <param name="response">The response to parse.</param> /// <returns>The parsed JToken.</returns> private JToken GetJTokenFromResponse(MobileServiceHttpResponse response) { JToken jtoken = response.Content.ParseToJToken(this.MobileServiceClient.SerializerSettings); if (response.Etag != null) { jtoken[MobileServiceSystemColumns.Version] = GetValueFromEtag(response.Etag); } return(jtoken); }
/// <summary> /// Executes a lookup against a table. /// </summary> /// <param name="id"> /// The id of the instance to lookup. /// </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> /// <returns> /// A task that will return with a result when the lookup finishes. /// </returns> internal async Task <JToken> LookupAsync(object id, IDictionary <string, string> parameters, MobileServiceFeatures features) { MobileServiceSerializer.EnsureValidId(id); features = this.AddRequestFeatures(features, parameters); string uriString = GetUri(this.TableName, id, parameters); MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Get, uriString, this.MobileServiceClient.CurrentUser, null, true, features : this.Features | features); return(GetJTokenFromResponse(response)); }
/// <summary> /// Executes a lookup against a table. /// </summary> /// <param name="id"> /// The id of the instance to lookup. /// </param> /// <param name="parameters"> /// A dictionary of user-defined parameters and values to include in /// the request URI query string. /// </param> /// <returns> /// A task that will return with a result when the lookup finishes. /// </returns> public async Task <JToken> LookupAsync(object id, IDictionary <string, string> parameters) { MobileServiceSerializer.EnsureValidId(id); parameters = AddSystemProperties(this.SystemProperties, parameters); string uriString = GetUri(this.TableName, id, parameters); MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Get, uriString, this.MobileServiceClient.CurrentUser, null, true); return(GetJTokenFromResponse(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> /// Updates an <paramref name="instance"/> in the table. /// </summary> /// <param name="instance"> /// The instance to update in the table. /// </param> /// <param name="parameters"> /// A dictionary of user-defined parameters and values to include in /// the request URI query string. /// </param> /// <returns> /// A task that will complete when the update finishes. /// </returns> public async Task <JToken> UpdateAsync(JObject instance, IDictionary <string, string> parameters) { if (instance == null) { throw new ArgumentNullException("instance"); } MobileServiceInvalidOperationException error = null; object id = MobileServiceSerializer.GetId(instance); string version = null; if (!MobileServiceSerializer.IsIntegerId(id)) { instance = RemoveSystemProperties(instance, out version); } parameters = AddSystemProperties(this.SystemProperties, parameters); try { Dictionary <string, string> headers = null; string content = instance.ToString(Formatting.None); string uriString = GetUri(this.TableName, id, parameters); if (version != null) { headers = new Dictionary <string, string>(); headers.Add("If-Match", GetEtagFromValue(version)); } MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(patchHttpMethod, uriString, this.MobileServiceClient.CurrentUser, content, true, headers); return(GetJTokenFromResponse(response)); } catch (MobileServiceInvalidOperationException ex) { if (ex.Response != null && ex.Response.StatusCode != HttpStatusCode.PreconditionFailed) { throw; } error = ex; } JToken value = await this.ParseContent(error.Response); throw new MobileServicePreconditionFailedException(error, value); }
/// <summary> /// Deletes an <paramref name="instance"/> from the table. /// </summary> /// <param name="instance"> /// The instance to delete from the table. /// </param> /// <param name="parameters"> /// A dictionary of user-defined parameters and values to include in /// the request URI query string. /// </param> /// <returns> /// A task that will complete when the delete finishes. /// </returns> public async Task <JToken> DeleteAsync(JObject instance, IDictionary <string, string> parameters) { if (instance == null) { throw new ArgumentNullException("instance"); } object id = MobileServiceSerializer.GetId(instance); parameters = AddSystemProperties(this.SystemProperties, parameters); string uriString = GetUri(this.TableName, id, parameters); MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Delete, uriString, this.MobileServiceClient.CurrentUser, null, false); return(GetJTokenFromResponse(response)); }
/// <summary> /// Deletes an <paramref name="instance"/> from the table. /// </summary> /// <param name="instance"> /// The instance to delete from the table. /// </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> /// <returns> /// A task that will complete when the delete finishes. /// </returns> internal async Task <JToken> DeleteAsync(JObject instance, IDictionary <string, string> parameters, MobileServiceFeatures features) { if (instance == null) { throw new ArgumentNullException("instance"); } object id = MobileServiceSerializer.GetId(instance); features = this.AddRequestFeatures(features, parameters); Dictionary <string, string> headers = StripSystemPropertiesAndAddVersionHeader(ref instance, ref parameters, id); string uriString = GetUri(this.TableName, id, parameters); return(await TransformHttpException(async() => { MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Delete, uriString, this.MobileServiceClient.CurrentUser, null, false, headers, this.Features | features); return GetJTokenFromResponse(response); })); }
/// <summary> /// Updates an <paramref name="instance"/> in the table. /// </summary> /// <param name="instance"> /// The instance to update in the table. /// </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> /// <returns> /// A task that will complete when the update finishes. /// </returns> internal async Task <JToken> UpdateAsync(JObject instance, IDictionary <string, string> parameters, MobileServiceFeatures features) { if (instance == null) { throw new ArgumentNullException("instance"); } features = this.AddRequestFeatures(features, parameters); object id = MobileServiceSerializer.GetId(instance); Dictionary <string, string> headers = StripSystemPropertiesAndAddVersionHeader(ref instance, ref parameters, id); string content = instance.ToString(Formatting.None); string uriString = GetUri(this.TableName, id, parameters); return(await this.TransformHttpException(async() => { MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(patchHttpMethod, uriString, this.MobileServiceClient.CurrentUser, content, true, headers, this.Features | features); var result = GetJTokenFromResponse(response); return RemoveUnrequestedSystemProperties(result, parameters, response.Etag); })); }
private async Task <QueryResult> ReadAsync(string uriString, MobileServiceFeatures features) { MobileServiceHttpResponse response = await this.MobileServiceClient.HttpClient.RequestAsync(HttpMethod.Get, uriString, this.MobileServiceClient.CurrentUser, null, true, features : this.Features | features); return(QueryResult.Parse(response, this.MobileServiceClient.SerializerSettings, validate: false)); }
/// <summary> /// Parses the response content into a JToken and adds the version system property /// if the ETag was returned from the server. /// </summary> /// <param name="response">The response to parse.</param> /// <returns>The parsed JToken.</returns> private JToken GetJTokenFromResponse(MobileServiceHttpResponse response) { JToken jtoken = response.Content.ParseToJToken(this.MobileServiceClient.SerializerSettings); if (response.Etag != null) { jtoken[MobileServiceSerializer.VersionSystemPropertyString] = GetValueFromEtag(response.Etag); } return jtoken; }
/// <summary> /// Performs a web request and includes the standard Mobile Services /// headers. It will use an HttpClient without any http handlers. /// </summary> /// <param name="method"> /// The HTTP method used to request the resource. /// </param> /// <param name="uriPathAndQuery"> /// The URI of the resource to request (relative to the Mobile Services /// runtime). /// </param> /// <param name="user"> /// The object representing the user on behalf of whom the request will be sent. /// </param> /// <param name="content"> /// Optional content to send to the resource. /// </param> /// <returns> /// The content of the response as a string. /// </returns> public async Task <string> RequestWithoutHandlersAsync(HttpMethod method, string uriPathAndQuery, MobileServiceUser user, string content = null) { MobileServiceHttpResponse response = await this.RequestAsync(false, method, uriPathAndQuery, user, content, false); return(response.Content); }