Beispiel #1
0
        public async Task CreateShoppingListsAsync(string owner, string name)
        {
            Shared.Model.ShoppingList shoppingList = new Shared.Model.ShoppingList()
            {
                PartitionKey = owner,
                RowKey       = name
            };

            RestClient  client  = new RestClient(configuration.GetSection("FunctionHost").Value);
            RestRequest request = new RestRequest("/CreateShoppingList", Method.POST);
            var         cancellationTokenSource = new CancellationTokenSource();

            string body = JsonConvert.SerializeObject(shoppingList);

            request.AddParameter("application/json; charset=utf-8", body, ParameterType.RequestBody);
            request.AddHeader("Ocp-Apim-Subscription-Key", configuration.GetSection("APIKey").Value);
            request.RequestFormat = DataFormat.Json;

            try
            {
                var result = await client.ExecuteTaskAsync <List <Shared.Model.ShoppingList> >(request, cancellationTokenSource.Token, Method.POST).ConfigureAwait(false);

                if (!result.IsSuccessful)
                {
                    throw new Exception($"Error while creating the List : {result.StatusCode} - {result.ErrorMessage}");
                }
            }
            catch (Exception error)
            {
                // log.LogError(error.Message);
                // log.LogDebug(error.StackTrace);
                throw;
            }
        }
Beispiel #2
0
        public async Task <List <Shared.Model.ShoppingList> > GetShoppingListsAsync(string owner, string cacheVersion = null)
        {
            Shared.Model.ShoppingList shoppingList = new Shared.Model.ShoppingList()
            {
                PartitionKey = owner
            };

            RestClient  client  = new RestClient(configuration.GetSection("FunctionHost").Value);
            RestRequest request = new RestRequest("/GetShoppingLists", Method.POST);
            var         cancellationTokenSource = new CancellationTokenSource();

            string body = JsonConvert.SerializeObject(shoppingList);

            request.AddParameter("application/json; charset=utf-8", body, ParameterType.RequestBody);
            request.AddHeader("Ocp-Apim-Subscription-Key", configuration.GetSection("APIKey").Value);

            if (cacheVersion != null)
            {
                request.AddHeader("cacheId", cacheVersion);
            }

            request.RequestFormat = DataFormat.Json;

            try
            {
                var result = await client.ExecuteTaskAsync <List <Shared.Model.ShoppingList> >(request, cancellationTokenSource.Token, Method.POST).ConfigureAwait(false);

                if (!result.IsSuccessful)
                {
                    // log.LogError($"Rest Request wasn't successful: {result.ErrorMessage}");
                    return(new List <Shared.Model.ShoppingList>()
                    {
                        new Shared.Model.ShoppingList()
                        {
                            PartitionKey = "Error"
                        }
                    });
                }

                return(result.Data);
            }
            catch (Exception error)
            {
                // log.LogError(error.Message);
                // log.LogDebug(error.StackTrace);
                throw;
            }
        }
        public static async Task <Message> DeleteShoppingList(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] dynamic shoppingList,
            [Table("ShoppingLists")] CloudTable cloudTable, ILogger log)
        {
            #region Null Checks
            if (shoppingList == null)
            {
                throw new ArgumentNullException(nameof(shoppingList));
            }

            if (cloudTable == null)
            {
                throw new ArgumentNullException(nameof(cloudTable));
            }
            #endregion

            Shared.Model.ShoppingList list = new Shared.Model.ShoppingList(shoppingList.RowKey.ToString(), shoppingList.PartitionKey.ToString())
            {
                ETag = "*"
            };

            var operation = TableOperation.Delete(list);

            try
            {
                _ = await cloudTable.ExecuteAsync(operation).ConfigureAwait(false);

                return(new Message()
                {
                    Body = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(list)),
                    ScheduledEnqueueTimeUtc = DateTime.UtcNow.AddDays(1)
                });
            }
            catch (StorageException ex)
            {
                log.LogError(ex, $"{Constants.ErrorMessageDeleteAList}{ex.RequestInformation.ExtendedErrorInformation.ErrorMessage}");
                throw;
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"{Constants.UnknownError}{ex.Message}");
                throw;
            }
        }