/// <summary> /// Edits the specified user. /// </summary> public async Task <UserResult> EditUserAsync(string username, EditUserOption user, CancellationToken cancellationToken = default(CancellationToken)) { if (username == null) { throw new ArgumentNullException(nameof(username)); } if (string.IsNullOrWhiteSpace(username)) { throw new ArgumentException($"{nameof(username)} is required", paramName: nameof(username)); } if (user == null) { throw new ArgumentNullException(nameof(user)); } var requestUrl = context.CreateRequestUri($"admin/users/{username}"); using (var httpClient = await context.CreateHttpClientAsync()) using (var requestContent = new StringContent(user.ToJson(), Encoding.UTF8, mediaType: "application/json")) { var response = await httpClient.PutAsync(requestUrl, requestContent, cancellationToken); var responseJson = await response.Content.ReadAsStringAsync(); var result = JsonEntity.ParseJson <UserResult>(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> /// Creates and returns a token with the specified <paramref name="name"/> for the user with the specified username. /// </summary> public async Task <TokenResult> CreateTokenAsync(string username, string name, CancellationToken cancellationToken = default(CancellationToken)) { if (username == null) { throw new ArgumentNullException(nameof(username)); } if (string.IsNullOrWhiteSpace(username)) { throw new ArgumentException($"{nameof(username)} is required", paramName: nameof(username)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException($"{nameof(name)} is required", paramName: nameof(name)); } var requestUrl = context.CreateRequestUri($"users/{username}/tokens"); using (var httpClient = await context.CreateHttpClientAsync()) using (var requestContent = new FormUrlEncodedContent(new Dictionary <string, string> { ["name"] = name })) { var response = await httpClient.PostAsync(requestUrl, requestContent, cancellationToken : cancellationToken); var responseJson = await response.Content.ReadAsStringAsync(); var result = JsonEntity.ParseJson <TokenResult>(responseJson); return(result); } }
/// <summary> /// Returns the teams in the specified organization. /// </summary> public async Task <IReadOnlyCollection <TeamResult> > GetTeamsAsync(string orgName, CancellationToken cancellationToken = default(CancellationToken)) { var requestUrl = context.CreateRequestUri($"orgs/{orgName}/teams"); using (var httpClient = await context.CreateHttpClientAsync()) { var response = await httpClient.GetAsync(requestUrl, cancellationToken : cancellationToken); var responseJson = await response.Content.ReadAsStringAsync(); var results = JsonEntity.ParseJsonArray <TeamResult>(responseJson); return(results); } }
/// <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> /// 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); } }
/// <summary> /// Creates a team in the specified organization. /// </summary> public async Task <TeamResult> CreateTeam(string orgName, CreateTeamOption team, CancellationToken cancellationToken = default(CancellationToken)) { if (orgName == null) { throw new ArgumentNullException(nameof(orgName)); } if (string.IsNullOrWhiteSpace(orgName)) { throw new ArgumentException($"{nameof(orgName)} is required", paramName: nameof(orgName)); } if (team == null) { throw new ArgumentNullException(nameof(team)); } var requestUrl = context.CreateRequestUri($"admin/orgs/{orgName}/teams"); using (var httpClient = await context.CreateHttpClientAsync()) using (var requestContent = new StringContent(team.ToJson(), Encoding.UTF8, mediaType: "application/json")) { HttpResponseMessage response; try { response = await httpClient.PostAsync(requestUrl, requestContent, cancellationToken : cancellationToken); } catch (GogsKitResponseException ex) { if (ex.ResponseStatusCode == 422) { throw new GogsKitAlreadyExistsException($"The team with name \"{team.Name}\" already exists, or there's a bug in your version of GOGS that doesn't allow you to create a second team for an organization."); } throw; } var responseJson = await response.Content.ReadAsStringAsync(); var result = JsonEntity.ParseJson <TeamResult>(responseJson); return(result); } }
/// <summary> /// Returns the access tokens for the user with the specified username. /// </summary> public async Task <IReadOnlyCollection <TokenResult> > GetTokensAsync(string username, CancellationToken cancellationToken = default(CancellationToken)) { if (username == null) { throw new ArgumentNullException(nameof(username)); } if (string.IsNullOrWhiteSpace(username)) { throw new ArgumentException($"{nameof(username)} is required", paramName: nameof(username)); } var requestUrl = context.CreateRequestUri($"users/{username}/tokens"); using (var httpClient = await context.CreateHttpClientAsync()) { var response = await httpClient.GetAsync(requestUrl, cancellationToken : cancellationToken); var responseJson = await response.Content.ReadAsStringAsync(); var results = JsonEntity.ParseJsonArray <TokenResult>(responseJson); return(results ?? Array.Empty <TokenResult>()); } }