using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's Gists API. /// /// /// See the Gists API documentation for more information. /// public class GistsClient : ApiClient, IGistsClient { /// /// Instantiates a new GitHub Gists API client. /// /// An API connection public GistsClient(IApiConnection apiConnection) : base(apiConnection) { Comment = new GistCommentsClient(apiConnection); } public IGistCommentsClient Comment { get; set; } /// /// Gets a gist /// /// /// http://developer.github.com/v3/gists/#get-a-single-gist /// /// The id of the gist public Task Get(string id) { return ApiConnection.Get(ApiUrls.Gist(id)); } /// /// Creates a new gist /// /// /// http://developer.github.com/v3/gists/#create-a-gist /// /// The new gist to create public Task Create(NewGist newGist) { Ensure.ArgumentNotNull(newGist, "newGist"); //Required to create anonymous object to match signature of files hash. // Allowing the serializer to handle Dictionary // 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(ApiUrls.Gist(), gist); } /// /// Creates a fork of a gist /// /// /// http://developer.github.com/v3/gists/#fork-a-gist /// /// The id of the gist to fork public Task Fork(string id) { return ApiConnection.Post(ApiUrls.ForkGist(id), new object()); } /// /// Deletes a gist /// /// /// http://developer.github.com/v3/gists/#delete-a-gist /// /// The id of the gist public Task Delete(string id) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); return ApiConnection.Delete(ApiUrls.Gist(id)); } /// /// List the authenticated user’s gists or if called anonymously, /// this will return all public gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// public Task> GetAll() { return GetAll(ApiOptions.None); } /// /// List the authenticated user’s gists or if called anonymously, /// this will return all public gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Options for changing the API response public Task> GetAll(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Gist(), options); } /// /// List the authenticated user’s gists or if called anonymously, /// this will return all public gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Only gists updated at or after this time are returned public Task> GetAll(DateTimeOffset since) { return GetAll(since, ApiOptions.None); } /// /// List the authenticated user’s gists or if called anonymously, /// this will return all public gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Only gists updated at or after this time are returned /// Options for changing the API response public Task> GetAll(DateTimeOffset since, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); var request = new GistRequest(since); return ApiConnection.GetAll(ApiUrls.Gist(), request.ToParametersDictionary(), options); } /// /// Lists all public gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// public Task> GetAllPublic() { return GetAllPublic(ApiOptions.None); } /// /// Lists all public gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Options for changing the API response public Task> GetAllPublic(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.PublicGists(), options); } /// /// Lists all public gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Only gists updated at or after this time are returned public Task> GetAllPublic(DateTimeOffset since) { return GetAllPublic(since, ApiOptions.None); } /// /// Lists all public gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Only gists updated at or after this time are returned /// Options for changing the API response public Task> GetAllPublic(DateTimeOffset since, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); var request = new GistRequest(since); return ApiConnection.GetAll(ApiUrls.PublicGists(), request.ToParametersDictionary(), options); } /// /// List the authenticated user’s starred gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// public Task> GetAllStarred() { return GetAllStarred(ApiOptions.None); } /// /// List the authenticated user’s starred gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Options for changing the API response public Task> GetAllStarred(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.StarredGists(), options); } /// /// List the authenticated user’s starred gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Only gists updated at or after this time are returned public Task> GetAllStarred(DateTimeOffset since) { return GetAllStarred(since, ApiOptions.None); } /// /// List the authenticated user’s starred gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// Only gists updated at or after this time are returned /// Options for changing the API response public Task> GetAllStarred(DateTimeOffset since, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); var request = new GistRequest(since); return ApiConnection.GetAll(ApiUrls.StarredGists(), request.ToParametersDictionary(), options); } /// /// List a user's gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// The user public Task> GetAllForUser(string user) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); return GetAllForUser(user, ApiOptions.None); } /// /// List a user's gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// The user /// Options for changing the API response public Task> GetAllForUser(string user, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.UsersGists(user), options); } /// /// List a user's gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// The user /// Only gists updated at or after this time are returned public Task> GetAllForUser(string user, DateTimeOffset since) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); return GetAllForUser(user, since, ApiOptions.None); } /// /// List a user's gists /// /// /// http://developer.github.com/v3/gists/#list-gists /// /// The user /// Only gists updated at or after this time are returned /// Options for changing the API response public Task> GetAllForUser(string user, DateTimeOffset since, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); Ensure.ArgumentNotNull(options, "options"); var request = new GistRequest(since); return ApiConnection.GetAll(ApiUrls.UsersGists(user), request.ToParametersDictionary(), options); } /// /// List gist commits /// /// /// http://developer.github.com/v3/gists/#list-gists-commits /// /// The id of the gist public Task> GetAllCommits(string id) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); return GetAllCommits(id, ApiOptions.None); } /// /// List gist commits /// /// /// http://developer.github.com/v3/gists/#list-gists-commits /// /// The id of the gist /// Options for changing the API response public Task> GetAllCommits(string id, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.GistCommits(id), options); } /// /// List gist forks /// /// /// http://developer.github.com/v3/gists/#list-gists-forks /// /// The id of the gist public Task> GetAllForks(string id) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); return GetAllForks(id, ApiOptions.None); } /// /// List gist forks /// /// /// http://developer.github.com/v3/gists/#list-gists-forks /// /// The id of the gist /// Options for changing the API response public Task> GetAllForks(string id, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.ForkGist(id), options); } /// /// Edits a gist /// /// /// http://developer.github.com/v3/gists/#delete-a-gist /// /// The id of the gist /// The update to the gist public Task Edit(string id, GistUpdate gistUpdate) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); Ensure.ArgumentNotNull(gistUpdate, "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(ApiUrls.Gist(id), gist); } /// /// Stars a gist /// /// /// http://developer.github.com/v3/gists/#star-a-gist /// /// The id of the gist public Task Star(string id) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); return ApiConnection.Put(ApiUrls.StarGist(id)); } /// /// Unstars a gist /// /// /// http://developer.github.com/v3/gists/#unstar-a-gist /// /// The id of the gist public Task Unstar(string id) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); return ApiConnection.Delete(ApiUrls.StarGist(id)); } /// /// Checks if the gist is starred /// /// /// http://developer.github.com/v3/gists/#check-if-a-gist-is-starred /// /// The id of the gist public async Task IsStarred(string id) { Ensure.ArgumentNotNullOrEmptyString(id, "id"); try { var response = await Connection.Get(ApiUrls.StarGist(id), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } } }