private async Task SaveProfile() { try { // Create a new profile var request = new CreateProfileRequest() { Name = ProfileName, Passphrase = ProfilePassphrase, ConfirmPassphrase = ConfirmProfilePassphrase }; // Call api to create the profile. var profile = await KryptPadApi.CreateProfileAsync(request); // Go to profile await KryptPadApi.LoadProfileAsync(profile, ProfilePassphrase); // Success, tell the app we are signed in (App.Current as App).IsSignedIn = true; // Redirect to the main item list page NavigationHelper.Navigate(typeof(ItemsPage), null); // Hide the dialog Hide(); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception) { // Failed await DialogHelper.ShowConnectionErrorMessageDialog(); } }
/// <summary> /// Creates a new profile /// </summary> /// <param name="profile"></param> /// <param name="passphrase"></param> /// <returns></returns> public static async Task<ApiProfile> CreateProfileAsync(CreateProfileRequest profile) { using (var client = new HttpClient()) { // Authorize the request. await AuthorizeRequest(client); // Create JSON content. var content = JsonContent(profile); // Send request and get a response var response = await client.PostAsync(GetUrl($"api/profiles"), content); // Deserialize the object based on the result if (response.IsSuccessStatusCode) { // Read the data var data = await response.Content.ReadAsStringAsync(); // Create an ApiProfile object var p = new ApiProfile() { Name = profile.Name, Id = Convert.ToInt32(data) }; // Return the profile object return p; } else { throw await CreateException(response); } } }