예제 #1
0
 private BethanysPieShopRemoteServices.Pie ConvertToRemotePie(BethanysPieShopStockApp.Models.Pie localPie)
 {
     return(new BethanysPieShopRemoteServices.Pie
     {
         Id = localPie.Id,
         Description = localPie.Description,
         ImageUrl = localPie.ImageUrl,
         InStock = localPie.InStock,
         PieName = localPie.PieName,
         Price = localPie.Price
     });
 }
        public async Task AddPieAsync(Pie pie)
        {
            try
            {
                HttpClient httpClient = CreateHttpClient(baseUrl);

                var content = new StringContent(JsonConvert.SerializeObject(pie));
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                string jsonResult = string.Empty;

                var responseMessage = await Policy
                                      .Handle <HttpRequestException>(ex =>
                {
                    Debug.WriteLine($"{ex.GetType().Name + " : " + ex.Message}");
                    return(true);
                })
                                      .WaitAndRetryAsync
                                      (
                    5,
                    retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                                      )
                                      .ExecuteAsync(async() => await httpClient.PostAsync(baseUrl, content));

                if (responseMessage.IsSuccessStatusCode)
                {
                    jsonResult = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return;
                }

                if (responseMessage.StatusCode == HttpStatusCode.Forbidden ||
                    responseMessage.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new ServiceAuthenticationException(jsonResult);
                }

                throw new HttpRequestExceptionEx(responseMessage.StatusCode, jsonResult);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"{ e.GetType().Name + " : " + e.Message}");
                throw;
            }
        }
예제 #3
0
 public static void AddPie(Pie pie)
 {
     pie.Id       = Guid.NewGuid();
     pie.ImageUrl = "https://gillcleerenpluralsight.blob.core.windows.net/files/cherrypiesmall.jpg";
     Pies.Add(pie);
 }