/// <summary> /// Send a request to create a new user with a username/password pair /// </summary> /// <param name="service"></param> /// <param name="user">The user to create</param> /// <param name="usernameCredentials">The user's username credentials</param> /// <returns>An authentication token and user information, or <code>null</code> if the request failed</returns> public static async Task <AuthResponse?> Register(this HttpService service, User user, UsernameCredentials usernameCredentials) { var registration = new UsernameRegistration(usernameCredentials, user); try { return(await service.Session.Send <AuthResponse>(() => HttpRequestMessageExtensions.Create(service.Session, "v1/register/username", HttpMethod.Post, registration))); } catch (Session.BadRequestException e) { switch (e.Error) { case "existing_username": throw new ExistingUsernameException(); case "existing_email": throw new ExistingEmailException(); case "malformed_email": throw new InvalidEmailException(); case "bad_password": throw new BadPasswordException(); case "short_password": throw new BadPasswordException(); } } return(null); }
/// <summary> /// Save the given preferences /// </summary> /// <param name="service"></param> /// <param name="preferences">The preferences to save</param> /// <returns><code>true</code> if the request succeeded, <code>false</code> otherwise</returns> public static async Task <bool> Save(this HttpService service, Preferences preferences) { if (preferences.UserId is string userId) { return(await service.Session.Send(() => HttpRequestMessageExtensions.Create(service.Session, string.Format("v1/users/{0}/preferences/{1}", userId, preferences.Id), HttpMethod.Put, preferences))); } return(false); }
/// <summary> /// Get the preferences for the given identifier /// </summary> /// <param name="service"></param> /// <param name="identifier">The preferences identifier, typically found in <code>User.PreferencesId</code></param> /// <returns>The preferences, or <code>null</code> if the request failed</returns> public static async Task <Preferences?> FetchPreferences(this HttpService service, User user) { if (user.PreferencesId is string prefsId) { return(await service.Session.Send <Preferences>(() => HttpRequestMessageExtensions.Create(service.Session, string.Format("v1/users/{0}/preferences/{1}", user.Id, prefsId), HttpMethod.Get))); } return(null); }
/// <summary> /// Save the given preferences /// </summary> /// <param name="service"></param> /// <param name="preferences">The preferences to save</param> /// <returns><code>MorphicResult.OkResult()</code> if the request succeeded, <code>MorphicResult.ErrorResult()</code> otherwise</returns> public static async Task <MorphicResult <MorphicUnit, MorphicUnit> > SaveAsync(this HttpService service, Preferences preferences) { if (preferences.UserId is string userId) { var success = await service.Send(() => HttpRequestMessageExtensions.Create(service, string.Format("v1/users/{0}/preferences/{1}", userId, preferences.Id), HttpMethod.Put, preferences)); return(success ? MorphicResult.OkResult() : MorphicResult.ErrorResult()); } return(MorphicResult.ErrorResult()); }
/// <summary> /// Send a request to create a new user with a secret key /// </summary> /// <param name="service"></param> /// <param name="user">The user to create</param> /// <param name="usernameCredentials">The user's key credentials</param> /// <returns>An authentication token and user information, or <code>null</code> if the request failed</returns> public static async Task <AuthResponse?> Register(this HttpService service, User user, KeyCredentials keyCredentials) { var registration = new KeyRegistration(keyCredentials, user); return(await service.Session.Send <AuthResponse>(() => HttpRequestMessageExtensions.Create(service.Session, "v1/register/key", HttpMethod.Post, registration))); }
/// <summary> /// Send a request to authenticate with key based credentials /// </summary> /// <param name="service"></param> /// <param name="keyCredentials">The key credentials</param> /// <returns>An authentication token and user information, or <code>null</code> if the request failed</returns> public static async Task <AuthResponse?> AuthenticateKey(this HttpService service, KeyCredentials keyCredentials) { var body = new AuthKeyRequest(keyCredentials.Key); return(await service.Session.Send <AuthResponse>(() => HttpRequestMessageExtensions.Create(service.Session, "v1/auth/key", HttpMethod.Post, body))); }
/// <summary> /// Send a reqeust to authenticate with username based credentials /// </summary> /// <param name="service"></param> /// <param name="usernameCredentials">The username credentials</param> /// <returns>An authentication token and user information, or <code>null</code> if the request failed</returns> public static async Task <AuthResponse?> AuthenticateUsername(this HttpService service, UsernameCredentials usernameCredentials) { var body = new AuthUsernameRequest(usernameCredentials.Username, usernameCredentials.Password); return(await service.Session.Send <AuthResponse>(() => HttpRequestMessageExtensions.Create(service.Session, "v1/auth/username", HttpMethod.Post, body))); }
/// <summary> /// Save the given user /// </summary> /// <param name="service"></param> /// <param name="user">The user to save</param> /// <returns><code>true</code> if the request succeeded, <code>false</code> otherwise</returns> public static async Task <bool> Save(this HttpService service, User user) { return(await service.Session.Send(() => HttpRequestMessageExtensions.Create(service.Session, string.Format("v1/users/{0}", user.Id), HttpMethod.Put, user))); }
/// <summary> /// Get the user for the given identifier /// </summary> /// <param name="service"></param> /// <param name="identifier">The user identifier</param> /// <returns>The user, or <code>null</code> if the request failed</returns> public static async Task <User?> FetchUser(this HttpService service, string identifier) { return(await service.Session.Send <User>(() => HttpRequestMessageExtensions.Create(service.Session, string.Format("v1/users/{0}", identifier), HttpMethod.Get))); }
public static async Task <UserCommunityDetail?> FetchUserCommunity(this HttpService service, string userIdentifier, string communityId) { return(await service.Send <UserCommunityDetail>(() => HttpRequestMessageExtensions.Create(service, string.Format("v1/users/{0}/communities/{1}", userIdentifier, communityId), HttpMethod.Get))); }