/// <summary> /// Couples phone and user through a token id. /// </summary> /// <param name="url">Url to post too.</param> /// <param name="token">token id belonging to the user</param> /// <returns>ReturnUserModel</returns> public static async Task <ReturnUserModel> Couple(string url, string username, string password) { var model = new ReturnUserModel(); try { HttpClientHandler handler = new HttpClientHandler(); _httpClient = new HttpClient(handler); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url + "/auth"); var sendthis = new LoginModel(); sendthis.Username = username; sendthis.Password = password; var json = JsonConvert.SerializeObject(sendthis); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); request.Content = stringContent; // Send request HttpResponseMessage response = await _httpClient.SendAsync(request); // Read response string jsonString = await response.Content.ReadAsStringAsync(); var isValid = IsValidJson(jsonString); if (string.IsNullOrEmpty(jsonString) || !isValid) { model.Error = new Error { Message = "Netværksfejl" }; model.User = null; } else if (!response.IsSuccessStatusCode) { model.Error = DeserializeError(jsonString); model.User = null; } else { // Deserialize string to object UserInfoModel user = JsonConvert.DeserializeObject <UserInfoModel>(jsonString); user = RemoveTrailer(user); model.User = user; model.Error = new Error(); // tom } //return model; return(model); } catch (Exception e) { model.User = null; model.Error = new Error { Message = e.Message, }; return(model); } }
/// <summary> /// Fetches the UserInfoModel belonging to the token. /// </summary> /// <param name="authorization">the token belonging to the user.</param> /// <param name="mun">the Municipality the user belongs to.</param> /// <returns>ReturnUserModel</returns> public static async Task <ReturnUserModel> RefreshModel(Authorization authorization, Municipality mun) { var model = new ReturnUserModel(); try { HttpClientHandler handler = new HttpClientHandler(); _httpClient = new HttpClient(handler); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, mun.APIUrl + "/userInfo"); var json = JsonConvert.SerializeObject(authorization); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); request.Content = stringContent; // Send request HttpResponseMessage response = await _httpClient.SendAsync(request); // Read response string jsonString = await response.Content.ReadAsStringAsync(); var isValid = IsValidJson(jsonString); if (response.IsSuccessStatusCode && isValid) { // Deserialize string to object UserInfoModel user = JsonConvert.DeserializeObject <UserInfoModel>(jsonString); user = RemoveTrailer(user); model.User = user; model.Error = new Error(); // tom } else if (string.IsNullOrEmpty(jsonString) || !isValid) { model.Error = new Error { Message = "Netværksfejl", ErrorCode = "404", }; model.User = null; } else if (!response.IsSuccessStatusCode) { model.Error = DeserializeError(jsonString); model.User = null; } //return model; return(model); } catch (Exception e) { model.User = null; model.Error = new Error { Message = e.Message, }; return(model); } }
/// <summary> /// Used to submit drivereport after finished drive. /// </summary> /// <param name="report">the report of the drive.</param> /// <param name="authorization">the token belonging to the user.</param> /// <param name="munUrl">the municipalicy url to be called</param> /// <returns>ReturnUserModel</returns> public static async Task <ReturnUserModel> SubmitDrive(DriveReport report, Authorization authorization, string munUrl) { var model = new ReturnUserModel(); try { var sendthis = new DriveSubmit(); sendthis.Authorization = authorization; sendthis.DriveReport = report; var json = JsonConvert.SerializeObject(sendthis); HttpClientHandler handler = new HttpClientHandler(); _httpClient = new HttpClient(handler); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, munUrl + "/report"); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); request.Content = stringContent; // Send request HttpResponseMessage response = await _httpClient.SendAsync(request); // Read response string jsonString = await response.Content.ReadAsStringAsync(); var isValid = IsValidJson(jsonString); if (response.IsSuccessStatusCode) { model.Error = null; } else if (string.IsNullOrEmpty(jsonString) || !isValid) { model.Error = new Error { Message = "Netværksfejl", ErrorCode = "404", }; model.User = null; } else { model.Error = DeserializeError(jsonString); model.User = null; } //return model; return(model); } catch (Exception e) { model.Error = new Error { ErrorCode = "Exception", Message = "Der skete en uhåndteret fejl. Kontakt venligst Support" }; return(model); } }
/// <summary> /// Method that handles the upload result /// </summary> /// <param name="user">the ReturnUserModel the api returned after upload attempt</param> /// <param name="page">the parent page, used to open popup</param> private void HandleUploadResult(ReturnUserModel model, StoredReportsPage page) { if (model.Error != null) { page.ClosePopup(); var popup = page.CreateMessagePopup("Kunne ikke upload på nuværende tidspunkt. Prøv igen senere\nFejl: " + model.Error.Message); page.PopUpLayout.ShowPopup(popup); return; } else { var item = (StoredReportCellModel)page.List.SelectedItem; ReportListHandler.RemoveReportFromList(item.report).ContinueWith((result) => { Definitions.RefreshMainView = true; page.ClosePopup(); InitializeCollection(result.Result); var popup = page.CreateMessagePopup("Kørsels rapport blev uploadet"); page.PopUpLayout.ShowPopup(popup); }, TaskScheduler.FromCurrentSynchronizationContext()); } }
/// <summary> /// Method that handles the Upload Result /// </summary> private void HandleUploadResult(ReturnUserModel model, object sender) { UploadingSpinnerVisibility = false; _timerContinue = false; if (model.Error != null) { ErrorText = "Der skete en fejl ved afsendelsen af din rapport!" + "\nFejl: " + model.Error.Message + "\nPrøv igen eller tryk på 'Gem' og send rapporten fra hovedmenuen på et andet tidspunkt."; UploadingTextVisibility = false; ErrorVisibility = true; } else { UploaderText = "Din indberetning er modtaget"; Device.StartTimer(TimeSpan.FromSeconds(_minimumWait), () => { Dispose(); App.Navigation.PopToRootAsync(); return false; }); } }