private async Task PostAccidentAsync() { var client = new AutoResolveWebserviceClient<AccidentViewModel>("api/accidents"); var response = await client.PostAsync(parentVm.Accident); if (response.IsSuccessStatusCode) { await DisplayAlert("Thanks, we got that!", "Accident Created", "Ok"); } }
public async Task<CustomerViewModel> PostCustomerAsync(CustomerViewModel customerViewModel) { using (var client = new AutoResolveWebserviceClient<CustomerViewModel>("api/customer")) { var response = await client.PostAsync(customerViewModel); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject<CustomerViewModel>(content); await DisplayAlert("Thanks, we got that!", "Customer Created", "Ok"); return model; } throw new HttpRequestException(); } }
public async Task<bool> Register(RegistrationViewModel model) { if (model != null) { using ( var accountClient = new AutoResolveWebserviceClient<object>("https://axiapp.co.uk/", "api/account/register")) { var response = await accountClient.PostAsync(model); LatestResponseCode = response.StatusCode.ToString(); if (!response.IsSuccessStatusCode) { LatestError = await response.Content.ReadAsStringAsync(); } return response.IsSuccessStatusCode; } } return false; }
public static async Task<HttpResponseMessage> CreateWitnessDeviceAndAPIIfNoKey(WitnessViewModel vm) { vm.Accident = App.CurrentServerSideAccident; var isThereExistingWitness = parentViewModel.Witnessses.FirstOrDefault(x => x.Client_Id == vm.Client_Id); Context.WitnessRepository.AddOrUpdate(vm); if (isThereExistingWitness == null) { parentViewModel.Witnessses.Add(vm); } else { isThereExistingWitness = vm; } if (await App.CheckForValidAccountAndTokenAndRefreshIfExpired()) { if (vm.Id == 0) { using ( var witnessClient = new AutoResolveWebserviceClient<WitnessViewModel>("api/witnesses") ) { witnessClient.AddBearerTokenHeader(parentViewModel.CurrentAccount.LatestBearerToken); var response = await witnessClient.PostAsync(vm); if (response.IsSuccessStatusCode) { var previousClientSideID = vm.Client_Id; var content = await response.Content.ReadAsStringAsync(); var pagedResult = JsonConvert.DeserializeObject<StandardPagedResultV1>(content); var driver = JsonConvert.DeserializeObject<WitnessViewModel>( JsonConvert.SerializeObject(pagedResult.Data)); driver.Client_Id = previousClientSideID; var current = parentViewModel.Witnessses.FirstOrDefault(x => x.Client_Id == previousClientSideID); current = driver; parentViewModel.Witnessses.Add(Context.WitnessRepository.AddOrUpdate(current)); } else { var error = await response.Content.ReadAsStringAsync(); SetLatestAPIErrorMessage(error); } return response; } } } return null; }
public static async Task CreateAccidentDeviceAndAPIIfNoKey(DateTime accidentDate) { parentViewModel.Accident = Context.AccidentRepository.AddOrUpdate(parentViewModel.Accident); if (await App.CheckForValidAccountAndTokenAndRefreshIfExpired()) { if (parentViewModel.Accident.Id == 0) { try { using (var accidentClient = new AutoResolveWebserviceClient<AccidentViewModel>("api/accidents")) { accidentClient.AddBearerTokenHeader(parentViewModel.CurrentAccount.LatestBearerToken); parentViewModel.Accident.AccidentDateTime = accidentDate; var response = await accidentClient.PostAsync(parentViewModel.Accident); if (response.IsSuccessStatusCode) { var previousClientSideId = parentViewModel.Accident.Client_Id; var content = await response.Content.ReadAsStringAsync(); var newAccidentModel = JsonConvert.DeserializeObject<AccidentViewModel>(content); parentViewModel.Accident = newAccidentModel; parentViewModel.Accident.Client_Id = previousClientSideId; //MUST RESET THIS parentViewModel.Accident = Context.AccidentRepository.AddOrUpdate(parentViewModel.Accident); } else { var error = await response.Content.ReadAsStringAsync(); SetLatestAPIErrorMessage(error); } } } catch (Exception ex) { throw new Exception("CreateAccidentDeviceAndAPIIfNoKey Failed", ex); } } //end accidents } }
public static async Task CreateCustomerSettingsDeviceAndAPIIfNoKey() { UserSettings.Customer = App.CurrentServerSideCustomer; UserSettings = Context.CustomerSettingsRepository.AddOrUpdate(UserSettings); if (await App.CheckForValidAccountAndTokenAndRefreshIfExpired()) { if (UserSettings.Id == 0) { //create in API using (var customerSettingsClient = new AutoResolveWebserviceClient<CustomerSettingsViewModel>("api/customersettings")) { customerSettingsClient.AddBearerTokenHeader(parentViewModel.CurrentAccount.LatestBearerToken); var response = await customerSettingsClient.PostAsync(UserSettings); if (response.IsSuccessStatusCode) { var previousClientSideId = UserSettings.Client_Id; //save with new ID var content = await response.Content.ReadAsStringAsync(); var pagedResult = JsonConvert.DeserializeObject<StandardPagedResultV1>(content); var customerSettings = JsonConvert.DeserializeObject<CustomerSettingsViewModel>( JsonConvert.SerializeObject(pagedResult.Data)); UserSettings = customerSettings; UserSettings.Client_Id = previousClientSideId; UserSettings = Context.CustomerSettingsRepository.AddOrUpdate(UserSettings); //end customer settings } else { var error = await response.Content.ReadAsStringAsync(); SetLatestAPIErrorMessage(error); } } } } }
async Task CreateCustomerDeviceAndAPIIfNoKey() { parentViewModel.Customer = Context.CustomerRepository.AddOrUpdate(parentViewModel.Customer); if (await App.CheckForValidAccountAndTokenAndRefreshIfExpired()) { if (parentViewModel.Customer.Id == 0) { //create in API using (var customerClient = new AutoResolveWebserviceClient<CustomerViewModel>("api/customers")) { customerClient.AddBearerTokenHeader(parentViewModel.CurrentAccount.LatestBearerToken); parentViewModel.Customer.Email = parentViewModel.CurrentAccount.Email; var response = await customerClient.PostAsync(parentViewModel.Customer); if (response.IsSuccessStatusCode) { var previousClientSideId = UserSettings.Client_Id; //save with new ID var content = await response.Content.ReadAsStringAsync(); var pagedResult = JsonConvert.DeserializeObject<StandardPagedResultV1>(content); var customer = JsonConvert.DeserializeObject<CustomerViewModel>( JsonConvert.SerializeObject(pagedResult.Data)); parentViewModel.Customer = customer; //need to save a second time after the update to have fresh data parentViewModel.Customer.Client_Id = previousClientSideId; parentViewModel.Customer = Context.CustomerRepository.AddOrUpdate(parentViewModel.Customer); parentViewModel.Accident.Customer = parentViewModel.Customer.Resource_uri; //end customer } else { var error = await response.Content.ReadAsStringAsync(); SetLatestAPIErrorMessage(error); } } } } }