Exemplo n.º 1
0
        /// <summary>
        /// Executes a request and transforms a 412 and 409 response to respective exception type.
        /// </summary>
        private async Task <JToken> TransformHttpException(MobileServiceSerializer serializer, Func <Task <JToken> > action)
        {
            try
            {
                return(await action());
            }
            catch (MobileServiceInvalidOperationException ex)
            {
                if (ex.Response.StatusCode != HttpStatusCode.PreconditionFailed &&
                    ex.Response.StatusCode != HttpStatusCode.Conflict)
                {
                    throw;
                }

                T item = default(T);
                try
                {
                    item = serializer.Deserialize <T>(ex.Value);
                }
                catch { }

                if (ex.Response.StatusCode == HttpStatusCode.PreconditionFailed)
                {
                    throw new MobileServicePreconditionFailedException <T>(ex, item);
                }
                else if (ex.Response.StatusCode == HttpStatusCode.Conflict)
                {
                    throw new MobileServiceConflictException <T>(ex, item);
                }
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Refresh the current instance with the latest values from the
        /// table.
        /// </summary>
        /// <param name="instance">
        /// The instance to refresh.
        /// </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 refresh has finished.
        /// </returns>
        public async Task RefreshAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            object id = serializer.GetId(instance, allowDefault: true);

            if (MobileServiceSerializer.IsDefaultId(id))
            {
                return;
            }
            if (id is string)
            {
                MobileServiceSerializer.EnsureValidStringId(id, allowDefault: true);
            }

            // Get the latest version of this element
            JObject refreshed = await this.GetSingleValueAsync(id, parameters);

            // Deserialize that value back into the current instance
            serializer.Deserialize <T>(refreshed, instance);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Undeletes an <paramref name="instance"/> from the table.
        /// </summary>
        /// <param name="instance">The instance to undelete 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 undelete finishes.</returns>
        public async Task UndeleteAsync(T instance, IDictionary <string, string> parameters)
        {
            Arguments.IsNotNull(instance, nameof(instance));

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            JObject value        = serializer.Serialize(instance) as JObject;
            JToken  updatedValue = await TransformHttpException(serializer, () => UndeleteAsync(value, parameters, MobileServiceFeatures.TypedTable));

            serializer.Deserialize(updatedValue, instance);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Inserts a new instance into the table.
        /// </summary>
        /// <param name="instance">
        /// The instance to insert.
        /// </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 insertion has finished.
        /// </returns>
        public async Task InsertAsync(T instance, IDictionary <string, string> parameters)
        {
            Arguments.IsNotNull(instance, nameof(instance));

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            JObject value = serializer.Serialize(instance) as JObject;

            value = MobileServiceSerializer.RemoveSystemProperties(value, out string unused);
            JToken insertedValue = await TransformHttpException(serializer, () => InsertAsync(value, parameters, MobileServiceFeatures.TypedTable));

            serializer.Deserialize(insertedValue, instance);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Undeletes an <paramref name="instance"/> from the table.
        /// </summary>
        /// <param name="instance">The instance to undelete 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 undelete finishes.</returns>
        public async Task UndeleteAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            JObject value = serializer.Serialize(instance) as JObject;

            JToken updatedValue = await TransformHttpException(serializer, () => this.UndeleteAsync(value, parameters, MobileServiceFeatures.TypedTable));

            serializer.Deserialize <T>(updatedValue, instance);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Inserts a new instance into the table.
        /// </summary>
        /// <param name="instance">
        /// The instance to insert.
        /// </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 insertion has finished.
        /// </returns>
        public async Task InsertAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            JObject value = serializer.Serialize(instance) as JObject;

            string unused;

            value = MobileServiceSerializer.RemoveSystemProperties(value, out unused);

            JToken insertedValue = await TransformHttpException(serializer, () => this.InsertAsync(value, parameters, MobileServiceFeatures.TypedTable));

            serializer.Deserialize <T>(insertedValue, instance);
        }
        /// <summary>
        /// Inserts a new instance into the table.
        /// </summary>
        /// <param name="instance">
        /// The instance to insert.
        /// </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 insertion has finished.
        /// </returns>
        public async Task InsertAsync(T instance, IDictionary <string, string> parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            JObject value = serializer.Serialize(instance) as JObject;

            if (!this.hasIntegerId)
            {
                string unused;
                value = RemoveSystemProperties(value, out unused);
            }
            JToken insertedValue = await this.InsertAsync(value, parameters);

            serializer.Deserialize <T>(insertedValue, instance);
        }
        /// <summary>
        /// Invokes a user-defined custom API of a Windows 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 Windows Azure Mobile Service.</typeparam>
        /// <typeparam name="U">The type of instance returned from the Windows 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>
        /// <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)
        {
            if (string.IsNullOrWhiteSpace(apiName))
            {
                throw new ArgumentNullException("apiName");
            }

            MobileServiceSerializer serializer = this.Serializer;
            string content = null;

            if (body != null)
            {
                content = serializer.Serialize(body);
            }

            string response = await this.InternalInvokeApiAsync(apiName, content, method, parameters);

            return(serializer.Deserialize <U>(response));
        }
        /// <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)));
        }