Files
octokit.net/Octokit/Clients/GistsClient.cs
Victor 6c43183837 fix: comment id model update to long instead of int
* #2927: comment id model update to long instead of int

* #2927: code review fixes (1)

* #2927: code review fixes (2)

* #2927: comment id model update to long instead of int: unit tests fix

* #2927: code review fixes

* Fixed most names of parameters

---------

Co-authored-by: Victor Vorobyev <victor@myrtle-sa.com>
Co-authored-by: Brian C. Arnold <brian.arnold@spiderrock.net>
2024-06-10 08:12:08 -05:00

488 lines
18 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
/// <summary>
/// A client for GitHub's Gists API.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/gists/">Gists API documentation</a> for more information.
/// </remarks>
public class GistsClient : ApiClient, IGistsClient
{
/// <summary>
/// Instantiates a new GitHub Gists API client.
/// </summary>
/// <param name="apiConnection">An API connection</param>
public GistsClient(IApiConnection apiConnection) :
base(apiConnection)
{
Comment = new GistCommentsClient(apiConnection);
}
public IGistCommentsClient Comment { get; set; }
/// <summary>
/// Gets a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#get-a-single-gist
/// </remarks>
/// <param name="gistId">The id of the gist</param>
[ManualRoute("GET", "/gists/{gist_id}")]
public Task<Gist> Get(string gistId)
{
return ApiConnection.Get<Gist>(ApiUrls.Gist(gistId));
}
/// <summary>
/// Creates a new gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#create-a-gist
/// </remarks>
/// <param name="newGist">The new gist to create</param>
[ManualRoute("GET", "/gists")]
public Task<Gist> Create(NewGist newGist)
{
Ensure.ArgumentNotNull(newGist, nameof(newGist));
//Required to create anonymous object to match signature of files hash.
// Allowing the serializer to handle Dictionary<string,NewGistFile>
// will fail to match.
var filesAsJsonObject = new JsonObject();
foreach (var kvp in newGist.Files)
{
filesAsJsonObject.Add(kvp.Key, new { Content = kvp.Value });
}
var gist = new
{
Description = newGist.Description,
Public = newGist.Public,
Files = filesAsJsonObject
};
return ApiConnection.Post<Gist>(ApiUrls.Gist(), gist);
}
/// <summary>
/// Creates a fork of a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#fork-a-gist
/// </remarks>
/// <param name="gistId">The id of the gist to fork</param>
[ManualRoute("POST", "/gists/{gist_id}/forks")]
public Task<Gist> Fork(string gistId)
{
return ApiConnection.Post<Gist>(ApiUrls.ForkGist(gistId), new object());
}
/// <summary>
/// Deletes a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="gistId">The id of the gist</param>
[ManualRoute("DELETE", "/gists/{gist_id}")]
public Task Delete(string gistId)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
return ApiConnection.Delete(ApiUrls.Gist(gistId));
}
/// <summary>
/// List the authenticated users gists or if called anonymously,
/// this will return all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
[ManualRoute("GET", "/gists")]
public Task<IReadOnlyList<Gist>> GetAll()
{
return GetAll(ApiOptions.None);
}
/// <summary>
/// List the authenticated users gists or if called anonymously,
/// this will return all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/gists")]
public Task<IReadOnlyList<Gist>> GetAll(ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return ApiConnection.GetAll<Gist>(ApiUrls.Gist(), options);
}
/// <summary>
/// List the authenticated users gists or if called anonymously,
/// this will return all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="since">Only gists updated at or after this time are returned</param>
[ManualRoute("GET", "/gists")]
public Task<IReadOnlyList<Gist>> GetAll(DateTimeOffset since)
{
return GetAll(since, ApiOptions.None);
}
/// <summary>
/// List the authenticated users gists or if called anonymously,
/// this will return all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="since">Only gists updated at or after this time are returned</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/gists")]
public Task<IReadOnlyList<Gist>> GetAll(DateTimeOffset since, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
var request = new GistRequest(since);
return ApiConnection.GetAll<Gist>(ApiUrls.Gist(), request.ToParametersDictionary(), options);
}
/// <summary>
/// Lists all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
[ManualRoute("GET", "/gists/public")]
public Task<IReadOnlyList<Gist>> GetAllPublic()
{
return GetAllPublic(ApiOptions.None);
}
/// <summary>
/// Lists all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/gists/public")]
public Task<IReadOnlyList<Gist>> GetAllPublic(ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return ApiConnection.GetAll<Gist>(ApiUrls.PublicGists(), options);
}
/// <summary>
/// Lists all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="since">Only gists updated at or after this time are returned</param>
[ManualRoute("GET", "/gists/public")]
public Task<IReadOnlyList<Gist>> GetAllPublic(DateTimeOffset since)
{
return GetAllPublic(since, ApiOptions.None);
}
/// <summary>
/// Lists all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="since">Only gists updated at or after this time are returned</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/gists/public")]
public Task<IReadOnlyList<Gist>> GetAllPublic(DateTimeOffset since, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
var request = new GistRequest(since);
return ApiConnection.GetAll<Gist>(ApiUrls.PublicGists(), request.ToParametersDictionary(), options);
}
/// <summary>
/// List the authenticated users starred gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
[ManualRoute("GET", "/gists/starred")]
public Task<IReadOnlyList<Gist>> GetAllStarred()
{
return GetAllStarred(ApiOptions.None);
}
/// <summary>
/// List the authenticated users starred gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/gists/starred")]
public Task<IReadOnlyList<Gist>> GetAllStarred(ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return ApiConnection.GetAll<Gist>(ApiUrls.StarredGists(), options);
}
/// <summary>
/// List the authenticated users starred gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="since">Only gists updated at or after this time are returned</param>
[ManualRoute("GET", "/gists/starred")]
public Task<IReadOnlyList<Gist>> GetAllStarred(DateTimeOffset since)
{
return GetAllStarred(since, ApiOptions.None);
}
/// <summary>
/// List the authenticated users starred gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="since">Only gists updated at or after this time are returned</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/gists/starred")]
public Task<IReadOnlyList<Gist>> GetAllStarred(DateTimeOffset since, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
var request = new GistRequest(since);
return ApiConnection.GetAll<Gist>(ApiUrls.StarredGists(), request.ToParametersDictionary(), options);
}
/// <summary>
/// List a user's gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="user">The user</param>
[ManualRoute("GET", "/users/{username}/gists")]
public Task<IReadOnlyList<Gist>> GetAllForUser(string user)
{
Ensure.ArgumentNotNullOrEmptyString(user, nameof(user));
return GetAllForUser(user, ApiOptions.None);
}
/// <summary>
/// List a user's gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="user">The user</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/users/{username}/gists")]
public Task<IReadOnlyList<Gist>> GetAllForUser(string user, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(user, nameof(user));
Ensure.ArgumentNotNull(options, nameof(options));
return ApiConnection.GetAll<Gist>(ApiUrls.UsersGists(user), options);
}
/// <summary>
/// List a user's gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="user">The user</param>
/// <param name="since">Only gists updated at or after this time are returned</param>
[ManualRoute("GET", "/users/{username}/gists")]
public Task<IReadOnlyList<Gist>> GetAllForUser(string user, DateTimeOffset since)
{
Ensure.ArgumentNotNullOrEmptyString(user, nameof(user));
return GetAllForUser(user, since, ApiOptions.None);
}
/// <summary>
/// List a user's gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="user">The user</param>
/// <param name="since">Only gists updated at or after this time are returned</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/users/{username}/gists")]
public Task<IReadOnlyList<Gist>> GetAllForUser(string user, DateTimeOffset since, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(user, nameof(user));
Ensure.ArgumentNotNull(options, nameof(options));
var request = new GistRequest(since);
return ApiConnection.GetAll<Gist>(ApiUrls.UsersGists(user), request.ToParametersDictionary(), options);
}
/// <summary>
/// List gist commits
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists-commits
/// </remarks>
/// <param name="gistId">The id of the gist</param>
[ManualRoute("GET", "/gists/{gist_id}/commits")]
public Task<IReadOnlyList<GistHistory>> GetAllCommits(string gistId)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
return GetAllCommits(gistId, ApiOptions.None);
}
/// <summary>
/// List gist commits
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists-commits
/// </remarks>
/// <param name="gistId">The id of the gist</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/gists/{gist_id}/commits")]
public Task<IReadOnlyList<GistHistory>> GetAllCommits(string gistId, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
Ensure.ArgumentNotNull(options, nameof(options));
return ApiConnection.GetAll<GistHistory>(ApiUrls.GistCommits(gistId), options);
}
/// <summary>
/// List gist forks
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists-forks
/// </remarks>
/// <param name="gistId">The id of the gist</param>
[ManualRoute("GET", "/gists/{gist_id}/forks")]
public Task<IReadOnlyList<GistFork>> GetAllForks(string gistId)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
return GetAllForks(gistId, ApiOptions.None);
}
/// <summary>
/// List gist forks
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists-forks
/// </remarks>
/// <param name="gistId">The id of the gist</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/gists/{gist_id}/forks")]
public Task<IReadOnlyList<GistFork>> GetAllForks(string gistId, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
Ensure.ArgumentNotNull(options, nameof(options));
return ApiConnection.GetAll<GistFork>(ApiUrls.ForkGist(gistId), options);
}
/// <summary>
/// Edits a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="gistId">The id of the gist</param>
/// <param name="gistUpdate">The update to the gist</param>
[ManualRoute("PATCH", "/gists/{gist_id}")]
public Task<Gist> Edit(string gistId, GistUpdate gistUpdate)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
Ensure.ArgumentNotNull(gistUpdate, nameof(gistUpdate));
var filesAsJsonObject = new JsonObject();
foreach (var kvp in gistUpdate.Files)
{
filesAsJsonObject.Add(kvp.Key, new { Content = kvp.Value.Content, Filename = kvp.Value.NewFileName });
}
var gist = new
{
Description = gistUpdate.Description,
Files = filesAsJsonObject
};
return ApiConnection.Patch<Gist>(ApiUrls.Gist(gistId), gist);
}
/// <summary>
/// Stars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#star-a-gist
/// </remarks>
/// <param name="gistId">The id of the gist</param>
[ManualRoute("PUT", "/gists/{gist_id}/star")]
public Task Star(string gistId)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
return ApiConnection.Put(ApiUrls.StarGist(gistId));
}
/// <summary>
/// Unstars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#unstar-a-gist
/// </remarks>
/// <param name="gistId">The id of the gist</param>
[ManualRoute("DELETE", "/gists/{gist_id}/star")]
public Task Unstar(string gistId)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
return ApiConnection.Delete(ApiUrls.StarGist(gistId));
}
/// <summary>
/// Checks if the gist is starred
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#check-if-a-gist-is-starred
/// </remarks>
/// <param name="gistId">The id of the gist</param>
[ManualRoute("GET", "/gists/{gist_id}/star")]
public async Task<bool> IsStarred(string gistId)
{
Ensure.ArgumentNotNullOrEmptyString(gistId, nameof(gistId));
try
{
var response = await Connection.Get<object>(ApiUrls.StarGist(gistId), null, null).ConfigureAwait(false);
return response.HttpResponse.IsTrue();
}
catch (NotFoundException)
{
return false;
}
}
}
}