internal async void SaveClerk() { using (HttpClient httpClient = new HttpClient()) { var api = new ApiService <Clerk>(httpClient); var response = await api.PutEntityAsync(_clerk.GetToken(), _clerk.ToJson()); if (response.IsSuccessStatusCode) { _clerk.Syncronized = true; MessagingCenter.Send <Clerk>(_clerk, SUCCESS); } else { var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (response.Content != null) { response.Content.Dispose(); } MessagingCenter.Send(new SimpleHttpResponseException(response.StatusCode, response.ReasonPhrase, content), 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); } } }
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); } } }
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(); }