private async void GetProduct() { using (HttpClient httpClient = new HttpClient()) { var response = await httpClient.GetAsync(URL_PRODUCT + Code); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); var partialProduct = JsonConvert.DeserializeObject <PartialProduct>(result); FillOutTable(partialProduct); } else { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (response.Content != null) { response.Content.Dispose(); } var simpleHttpResponse = new SimpleHttpResponseException(response.StatusCode, response.ReasonPhrase, content); MessagingCenter.Send(simpleHttpResponse, FAIL); } } }
public async void SaveOrder() { using (HttpClient httpClient = new HttpClient()) { var order = CreateOrderString(); //var encoded = Convert.ToBase64String(Encoding.GetEncoding("UTF-8").GetBytes(Clerk.Token + ":" + "")); httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Clerk.GetToken()}"); var stringContent = new StringContent(order, Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(URL_ORDER, stringContent); if (response.IsSuccessStatusCode) { MessagingCenter.Send(this, SUCCESS); Items.Clear(); } else { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (response.Content != null) { response.Content.Dispose(); } var simpleHttpResponse = new SimpleHttpResponseException(response.StatusCode, response.ReasonPhrase, content); MessagingCenter.Send(simpleHttpResponse, FAIL); } } }
private Exception GetSimpleHttpResponseException() { //var ex = new Exception("basic exception"); var ex = new SimpleHttpResponseException(HttpStatusCode.NotFound, "Target missing"); return(ex); }
public async void SaveProduct() { using (HttpClient httpClient = new HttpClient()) { var encoded = Convert.ToBase64String(Encoding.GetEncoding("UTF-8").GetBytes(Clerk.GetToken() + ":" + "")); httpClient.DefaultRequestHeaders.Add("Authorization", $"Basic {encoded}"); var stringContent = new StringContent(Product.ToJson(), Encoding.UTF8, "application/json"); var response = new HttpResponseMessage(); if (Product.Id == 0) { response = await httpClient.PostAsync(URL_PRODUCT, stringContent); } else { response = await httpClient.PutAsync(URL_PRODUCT, stringContent); } if (response.IsSuccessStatusCode) { Product.Syncronized = true; MessagingCenter.Send <Product>(Product, SUCCESS); } else { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (response.Content != null) { response.Content.Dispose(); } Product.Syncronized = false; var simpleHttpResponse = new SimpleHttpResponseException(response.StatusCode, response.ReasonPhrase, content); MessagingCenter.Send(simpleHttpResponse, FAIL); } } SaveProductInternaly(); }
public async void SavePayment() { using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Clerk.GetToken()}"); var stringContent = new StringContent(PaymentToJson(), Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(URL_PAYMENT, stringContent); if (response.IsSuccessStatusCode) { MessagingCenter.Send <String>($"R$ {this.Client.Debt - this.Value}", SUCCESS); } else { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (response.Content != null) { response.Content.Dispose(); } var simpleHttpResponse = new SimpleHttpResponseException(response.StatusCode, response.ReasonPhrase, content); MessagingCenter.Send(simpleHttpResponse, FAIL); } } }