/// <summary> /// Returns the organization with the specified name. /// </summary> public async Task <OrganizationResult> GetAsync(string orgName, CancellationToken cancellationToken = default(CancellationToken)) { var requestUrl = context.CreateRequestUri($"orgs/{orgName}"); using (var httpClient = await context.CreateHttpClientAsync()) { var response = await httpClient.GetAsync(requestUrl, cancellationToken : cancellationToken); var responseJson = await response.Content.ReadAsStringAsync(); var result = JsonEntity.ParseJson <OrganizationResult>(responseJson); return(result); } }
/// <summary> /// Search for users. /// </summary> public async Task <IReadOnlyCollection <UserResult> > SearchAsync(string query, int?limit = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(query)) { return(Array.Empty <UserResult>()); } var parameters = new Dictionary <string, string>(2); parameters.Add("q", query); if (limit.HasValue) { parameters.Add("limit", limit.Value.ToString()); } var requestUrl = context.CreateRequestUri("users/search", parameters); using (var httpClient = await context.CreateHttpClientAsync()) { var response = await httpClient.GetAsync(requestUrl, cancellationToken); var responseJson = await response.Content.ReadAsStringAsync(); var result = JsonEntity.ParseJson <DataResultWrapper <UserResult[]> >(responseJson); return(result?.Data ?? Array.Empty <UserResult>()); } }
/// <summary> /// Returns the organizations of the current user. /// </summary> public async Task <IReadOnlyCollection <OrganizationResult> > GetOrganizationsAsync(CancellationToken cancellationToken = default(CancellationToken)) { var requestUrl = context.CreateRequestUri($"user/orgs"); using (var httpClient = await context.CreateHttpClientAsync()) { var response = await httpClient.GetAsync(requestUrl, cancellationToken : cancellationToken); var responseJson = await response.Content.ReadAsStringAsync(); var results = JsonEntity.ParseJsonArray <OrganizationResult>(responseJson); return(results ?? Array.Empty <OrganizationResult>()); } }
/// <summary> /// Creates a user. /// </summary> public async Task <UserResult> CreateUserAsync(CreateUserOption user, CancellationToken cancellationToken = default(CancellationToken)) { if (user == null) { throw new ArgumentNullException(nameof(user)); } var requestUrl = context.CreateRequestUri($"admin/users"); using (var httpClient = await context.CreateHttpClientAsync()) using (var requestContent = new StringContent(user.ToJson(), Encoding.UTF8, mediaType: "application/json")) { var response = await httpClient.PostAsync(requestUrl, requestContent, cancellationToken : cancellationToken); var responseJson = await response.Content.ReadAsStringAsync(); var result = JsonEntity.ParseJson <UserResult>(responseJson); return(result); } }