Merge pull request #391 from Therzok/create-gists

Fixup for #239
This commit is contained in:
Phil Haack
2014-02-26 10:56:42 -08:00
15 changed files with 1232 additions and 6 deletions
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
namespace Octokit.Reactive
{
@@ -16,6 +17,143 @@ namespace Octokit.Reactive
/// <param name="id">The id of the gist</param>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get",
Justification = "Method makes a network request")]
IObservable<Gist> Get(string id);
IObservable<Gist> Get(string id);
/// <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>
IObservable<Gist> GetAll();
/// <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>
IObservable<Gist> GetAll(DateTimeOffset since);
/// <summary>
/// Lists all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
IObservable<Gist> GetAllPublic();
/// <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>
IObservable<Gist> GetAllPublic(DateTimeOffset since);
/// <summary>
/// List the authenticated users starred gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
IObservable<Gist> GetAllStarred();
/// <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>
IObservable<Gist> GetAllStarred(DateTimeOffset since);
/// <summary>
/// List a user's gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="user">The user</param>
IObservable<Gist> GetAllForUser(string user);
/// <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>
IObservable<Gist> GetAllForUser(string user, DateTimeOffset since);
/// <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>
IObservable<Gist> Create(NewGist newGist);
/// <summary>
/// Creates a fork of a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#fork-a-gist
/// </remarks>
/// <param name="id">The id of the gist to fork</param>
IObservable<Gist> Fork(string id);
/// <summary>
/// Edits a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
/// <param name="gistUpdate">The update to the gist</param>
IObservable<Gist> Edit(string id, GistUpdate gistUpdate);
/// <summary>
/// Deletes a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
IObservable<Unit> Delete(string id);
/// <summary>
/// Stars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#star-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
IObservable<Unit> Star(string id);
/// <summary>
/// Unstars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#unstar-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Unstar")]
IObservable<Unit> Unstar(string id);
/// <summary>
/// Checks if the gist is starred
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#check-if-a-gist-is-starred
/// </remarks>
/// <param name="id">The id of the gist</param>
IObservable<bool> IsStarred(string id);
}
}
@@ -1,17 +1,22 @@
using System;
using System.Reactive.Threading.Tasks;
using System.Reactive;
using System.Net;
using Octokit.Reactive.Internal;
namespace Octokit.Reactive
{
public class ObservableGistsClient : IObservableGistsClient
{
readonly IGistsClient _client;
readonly IConnection _connection;
public ObservableGistsClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");
_client = client.Gist;
_connection = client.Connection;
Comment = new ObservableGistCommentsClient(client);
}
@@ -31,5 +36,203 @@ namespace Octokit.Reactive
return _client.Get(id).ToObservable();
}
/// <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>
public IObservable<Gist> Create(NewGist newGist)
{
Ensure.ArgumentNotNull(newGist, "newGist");
return _client.Create(newGist).ToObservable();
}
/// <summary>
/// Creates a fork of a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#fork-a-gist
/// </remarks>
/// <param name="id">The id of the gist to fork</param>
public IObservable<Gist> Fork(string id)
{
return _client.Fork(id).ToObservable();
}
/// <summary>
/// Deletes a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
public IObservable<Unit> Delete(string id)
{
Ensure.ArgumentNotNull(id, "id");
return _client.Delete(id).ToObservable();
}
/// <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>
public IObservable<Gist> GetAll()
{
return _connection.GetAndFlattenAllPages<Gist>(ApiUrls.Gist());
}
/// <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>
public IObservable<Gist> GetAll(DateTimeOffset since)
{
var request = new GistRequest(since);
return _connection.GetAndFlattenAllPages<Gist>(ApiUrls.Gist(), request.ToParametersDictionary());
}
/// <summary>
/// Lists all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
public IObservable<Gist> GetAllPublic()
{
return _connection.GetAndFlattenAllPages<Gist>(ApiUrls.PublicGists());
}
/// <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>
public IObservable<Gist> GetAllPublic(DateTimeOffset since)
{
var request = new GistRequest(since);
return _connection.GetAndFlattenAllPages<Gist>(ApiUrls.PublicGists(), request.ToParametersDictionary());
}
/// <summary>
/// List the authenticated users starred gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
public IObservable<Gist> GetAllStarred()
{
return _connection.GetAndFlattenAllPages<Gist>(ApiUrls.StarredGists());
}
/// <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>
public IObservable<Gist> GetAllStarred(DateTimeOffset since)
{
var request = new GistRequest(since);
return _connection.GetAndFlattenAllPages<Gist>(ApiUrls.StarredGists(), request.ToParametersDictionary());
}
/// <summary>
/// List a user's gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="user">The user</param>
public IObservable<Gist> GetAllForUser(string user)
{
Ensure.ArgumentNotNull(user, "user");
return _connection.GetAndFlattenAllPages<Gist>(ApiUrls.UsersGists(user));
}
/// <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>
public IObservable<Gist> GetAllForUser(string user, DateTimeOffset since)
{
Ensure.ArgumentNotNull(user, "user");
var request = new GistRequest(since);
return _connection.GetAndFlattenAllPages<Gist>(ApiUrls.UsersGists(user), request.ToParametersDictionary());
}
/// <summary>
/// Edits a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
/// <param name="gistUpdate">The update to the gist</param>
public IObservable<Gist> Edit(string id, GistUpdate gistUpdate)
{
Ensure.ArgumentNotNull(id, "id");
Ensure.ArgumentNotNull(gistUpdate, "gistUpdate");
return _client.Edit(id, gistUpdate).ToObservable();
}
/// <summary>
/// Stars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#star-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
public IObservable<Unit> Star(string id)
{
return _client.Star(id).ToObservable();
}
/// <summary>
/// Unstars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#unstar-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
public IObservable<Unit> Unstar(string id)
{
return _client.Unstar(id).ToObservable();
}
/// <summary>
/// Checks if the gist is starred
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#check-if-a-gist-is-starred
/// </remarks>
/// <param name="id">The id of the gist</param>
public IObservable<bool> IsStarred(string id)
{
Ensure.ArgumentNotNullOrEmptyString(id, "id");
return _client.IsStarred(id).ToObservable();
}
}
}
@@ -3,10 +3,15 @@ using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Integration;
using Xunit;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System;
using System.Linq;
public class GistsClientTests
{
readonly IGistsClient _fixture;
readonly string testGistId = "6305249";
public GistsClientTests()
{
@@ -21,7 +26,112 @@ public class GistsClientTests
[IntegrationTest]
public async Task CanGetGist()
{
var retrieved = await _fixture.Get("6305249");
var retrieved = await _fixture.Get(testGistId);
Assert.NotNull(retrieved);
}
[IntegrationTest]
public async Task CanCreateEditAndDeleteAGist()
{
var newGist = new NewGist();
newGist.Description = "my new gist";
newGist.Public = true;
newGist.Files.Add("myGistTestFile.cs", "new GistsClient(connection).Create();");
var createdGist = await _fixture.Create(newGist);
Assert.NotNull(createdGist);
Assert.Equal(newGist.Description, createdGist.Description);
Assert.Equal(newGist.Public, createdGist.Public);
var gistUpdate = new GistUpdate();
gistUpdate.Description = "my newly updated gist";
var gistFileUpdate = new GistFileUpdate
{
NewFileName = "myNewGistTestFile.cs",
Content = "new GistsClient(connection).Edit();"
};
gistUpdate.Files.Add("myGistTestFile.cs", gistFileUpdate);
var updatedGist = await _fixture.Edit(createdGist.Id, gistUpdate);
Assert.NotNull(updatedGist);
Assert.Equal<string>(updatedGist.Description, gistUpdate.Description);
Assert.DoesNotThrow(async () => { await _fixture.Delete(createdGist.Id); });
}
[IntegrationTest]
public async Task CanStarAndUnstarAGist()
{
Assert.DoesNotThrow(async () => { await _fixture.Star(testGistId); });
bool isStarredTrue = await _fixture.IsStarred(testGistId);
Assert.True(isStarredTrue);
Assert.DoesNotThrow(async () => { await _fixture.Unstar(testGistId); });
bool isStarredFalse = await _fixture.IsStarred(testGistId);
Assert.False(isStarredFalse);
}
[IntegrationTest]
public async Task CanForkAGist()
{
var forkedGist = await _fixture.Fork(testGistId);
Assert.NotNull(forkedGist);
await _fixture.Delete(forkedGist.Id);
}
[IntegrationTest]
public async Task CanListGists()
{
// Time is tricky between local and remote, be leinent
var startTime = DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1));
var newGist = new NewGist();
newGist.Description = "my new gist";
newGist.Public = true;
newGist.Files.Add("myGistTestFile.cs", "new GistsClient(connection).Create();");
var createdGist = await _fixture.Create(newGist);
// Test get all Gists
var gists = await _fixture.GetAll();
Assert.NotNull(gists);
// Test get all Gists since startTime
gists = await _fixture.GetAll(startTime);
Assert.NotNull(gists);
Assert.True(gists.Count > 0);
// Make sure we can successfully request gists for another user
Assert.DoesNotThrow(async () => { await _fixture.GetAllForUser("FakeHaacked"); });
Assert.DoesNotThrow(async () => { await _fixture.GetAllForUser("FakeHaacked", startTime); });
// Test public gists
var publicGists = await _fixture.GetAllPublic();
Assert.True(publicGists.Count > 1);
var publicGistsSinceStartTime = await _fixture.GetAllPublic(startTime);
Assert.True(publicGistsSinceStartTime.Count > 0);
// Test starred gists
await _fixture.Star(createdGist.Id);
var starredGists = await _fixture.GetAllStarred();
Assert.NotNull(starredGists);
Assert.True(starredGists.Any(x => x.Id == createdGist.Id));
var starredGistsSinceStartTime = await _fixture.GetAllStarred(startTime);
Assert.NotNull(starredGistsSinceStartTime);
Assert.True(starredGistsSinceStartTime.Any(x => x.Id == createdGist.Id));
await _fixture.Delete(createdGist.Id);
}
}
+250 -2
View File
@@ -1,7 +1,14 @@
using System;
using NSubstitute;
using NSubstitute;
using Octokit;
using Octokit.Internal;
using Octokit.Tests.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Threading.Tasks;
using Xunit;
using Xunit.Extensions;
public class GistsClientTests
{
@@ -19,6 +26,247 @@ public class GistsClientTests
}
}
public class TheGetAllMethods
{
[Fact]
public void RequestsCorrectGetAllUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.GetAll();
connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists"));
}
[Fact]
public void RequestsCorrectGetAllWithSinceUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
DateTimeOffset since = DateTimeOffset.Now;
client.GetAll(since);
connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists"),
Arg.Is<IDictionary<string,string>>(x => x.ContainsKey("since")));
}
[Fact]
public void RequestsCorrectGetAllPublicUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.GetAllPublic();
connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/public"));
}
[Fact]
public void RequestsCorrectGetAllPublicWithSinceUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
DateTimeOffset since = DateTimeOffset.Now;
client.GetAllPublic(since);
connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/public"),
Arg.Is<IDictionary<string, string>>(x => x.ContainsKey("since")));
}
[Fact]
public void RequestsCorrectGetAllStarredUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.GetAllStarred();
connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/starred"));
}
[Fact]
public void RequestsCorrectGetAllStarredWithSinceUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
DateTimeOffset since = DateTimeOffset.Now;
client.GetAllStarred(since);
connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/starred"),
Arg.Is<IDictionary<string, string>>(x => x.ContainsKey("since")));
}
[Fact]
public void RequestsCorrectGetGistsForAUserUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.GetAllForUser("octokit");
connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "users/octokit/gists"));
}
[Fact]
public void RequestsCorrectGetGistsForAUserWithSinceUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
DateTimeOffset since = DateTimeOffset.Now;
client.GetAllForUser("octokit", since);
connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "users/octokit/gists"),
Arg.Is<IDictionary<string, string>>(x => x.ContainsKey("since")));
}
}
public class TheCreateMethod
{
[Fact]
public void PostsToTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
var newGist = new NewGist();
newGist.Description = "my new gist";
newGist.Public = true;
newGist.Files.Add("myGistTestFile.cs", "new GistsClient(connection).Create();");
client.Create(newGist);
connection.Received().Post<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists"), Arg.Any<object>());
}
}
public class TheDeleteMethod
{
[Fact]
public void PostsToTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.Delete("1");
connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "gists/1"));
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
AssertEx.Throws<ArgumentNullException>(async () => await
client.Delete(null));
}
}
public class TheStarMethods
{
[Fact]
public void RequestsCorrectStarUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.Star("1");
connection.Received().Put(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"));
}
[Fact]
public void RequestsCorrectUnstarUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.Unstar("1");
connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"));
}
[Theory]
[InlineData(HttpStatusCode.NoContent, true)]
[InlineData(HttpStatusCode.NotFound, false)]
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
{
var response = Task.Factory.StartNew<IResponse<object>>(() =>
new ApiResponse<object> { StatusCode = status });
var connection = Substitute.For<IConnection>();
connection.GetAsync<object>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"),
null, null).Returns(response);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new GistsClient(apiConnection);
var result = await client.IsStarred("1");
Assert.Equal(expected, result);
}
[Fact]
public async Task ThrowsExceptionForInvalidStatusCode()
{
var response = Task.Factory.StartNew<IResponse<object>>(() =>
new ApiResponse<object> { StatusCode = HttpStatusCode.Conflict });
var connection = Substitute.For<IConnection>();
connection.GetAsync<object>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"),
null, null).Returns(response);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new GistsClient(apiConnection);
AssertEx.Throws<ApiException>(async () => await client.IsStarred("1"));
}
}
public class TheForkMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.Fork("1");
connection.Received().Post<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1/forks"),
Arg.Any<object>());
}
}
public class TheEditMethod
{
[Fact]
public void PostsToTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
var updateGist = new GistUpdate();
updateGist.Description = "my newly updated gist";
var gistFileUpdate = new GistFileUpdate
{
NewFileName = "myNewGistTestFile.cs",
Content = "new GistsClient(connection).Edit();"
};
updateGist.Files.Add("myGistTestFile.cs", gistFileUpdate);
client.Edit("1", updateGist);
connection.Received().Patch<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1"), Arg.Any<object>());
}
}
public class TheCtor
{
[Fact]
+242 -1
View File
@@ -1,4 +1,7 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace Octokit
{
@@ -33,5 +36,243 @@ namespace Octokit
{
return ApiConnection.Get<Gist>(ApiUrls.Gist(id));
}
/// <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>
public Task<Gist> Create(NewGist newGist)
{
Ensure.ArgumentNotNull(newGist, "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="id">The id of the gist to fork</param>
public Task<Gist> Fork(string id)
{
return ApiConnection.Post<Gist>(ApiUrls.ForkGist(id), new object());
}
/// <summary>
/// Deletes a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
public Task Delete(string id)
{
Ensure.ArgumentNotNull(id, "id");
return ApiConnection.Delete(ApiUrls.Gist(id));
}
/// <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>
public Task<IReadOnlyList<Gist>> GetAll()
{
return ApiConnection.GetAll<Gist>(ApiUrls.Gist());
}
/// <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>
public Task<IReadOnlyList<Gist>> GetAll(DateTimeOffset since)
{
var request = new GistRequest(since);
return ApiConnection.GetAll<Gist>(ApiUrls.Gist(), request.ToParametersDictionary());
}
/// <summary>
/// Lists all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
public Task<IReadOnlyList<Gist>> GetAllPublic()
{
return ApiConnection.GetAll<Gist>(ApiUrls.PublicGists());
}
/// <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>
public Task<IReadOnlyList<Gist>> GetAllPublic(DateTimeOffset since)
{
var request = new GistRequest(since);
return ApiConnection.GetAll<Gist>(ApiUrls.PublicGists(), request.ToParametersDictionary());
}
/// <summary>
/// List the authenticated users starred gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
public Task<IReadOnlyList<Gist>> GetAllStarred()
{
return ApiConnection.GetAll<Gist>(ApiUrls.StarredGists());
}
/// <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>
public Task<IReadOnlyList<Gist>> GetAllStarred(DateTimeOffset since)
{
var request = new GistRequest(since);
return ApiConnection.GetAll<Gist>(ApiUrls.StarredGists(), request.ToParametersDictionary());
}
/// <summary>
/// List a user's gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="user">The user</param>
public Task<IReadOnlyList<Gist>> GetAllForUser(string user)
{
Ensure.ArgumentNotNull(user, "user");
return ApiConnection.GetAll<Gist>(ApiUrls.UsersGists(user));
}
/// <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>
public Task<IReadOnlyList<Gist>> GetAllForUser(string user, DateTimeOffset since)
{
Ensure.ArgumentNotNull(user, "user");
var request = new GistRequest(since);
return ApiConnection.GetAll<Gist>(ApiUrls.UsersGists(user), request.ToParametersDictionary());
}
/// <summary>
/// Edits a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
/// <param name="gistUpdate">The update to the gist</param>
public Task<Gist> Edit(string id, GistUpdate gistUpdate)
{
Ensure.ArgumentNotNull(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<Gist>(ApiUrls.Gist(id), gist);
}
/// <summary>
/// Stars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#star-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
public Task Star(string id)
{
return ApiConnection.Put(ApiUrls.StarGist(id));
}
/// <summary>
/// Unstars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#unstar-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
public Task Unstar(string id)
{
return ApiConnection.Delete(ApiUrls.StarGist(id));
}
/// <summary>
/// Checks if the gist is starred
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#check-if-a-gist-is-starred
/// </remarks>
/// <param name="id">The id of the gist</param>
public async Task<bool> IsStarred(string id)
{
Ensure.ArgumentNotNullOrEmptyString(id, "id");
try
{
var response = await Connection.GetAsync<object>(ApiUrls.StarGist(id), null, null)
.ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.NotFound && response.StatusCode != HttpStatusCode.NoContent)
{
throw new ApiException("Invalid Status Code returned. Expected a 204 or a 404", response.StatusCode);
}
return response.StatusCode == HttpStatusCode.NoContent;
}
catch (NotFoundException)
{
return false;
}
}
}
}
+140 -1
View File
@@ -1,4 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
namespace Octokit
@@ -23,5 +25,142 @@ namespace Octokit
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get",
Justification = "Method makes a network request")]
Task<Gist> Get(string id);
/// <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>
Task<IReadOnlyList<Gist>> GetAll();
/// <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>
Task<IReadOnlyList<Gist>> GetAll(DateTimeOffset since);
/// <summary>
/// Lists all public gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
Task<IReadOnlyList<Gist>> GetAllPublic();
/// <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>
Task<IReadOnlyList<Gist>> GetAllPublic(DateTimeOffset since);
/// <summary>
/// List the authenticated users starred gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
Task<IReadOnlyList<Gist>> GetAllStarred();
/// <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>
Task<IReadOnlyList<Gist>> GetAllStarred(DateTimeOffset since);
/// <summary>
/// List a user's gists
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#list-gists
/// </remarks>
/// <param name="user">The user</param>
Task<IReadOnlyList<Gist>> GetAllForUser(string user);
/// <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>
Task<IReadOnlyList<Gist>> GetAllForUser(string user, DateTimeOffset since);
/// <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>
Task<Gist> Create(NewGist newGist);
/// <summary>
/// Creates a fork of a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#fork-a-gist
/// </remarks>
/// <param name="id">The id of the gist to fork</param>
Task<Gist> Fork(string id);
/// <summary>
/// Edits a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
/// <param name="gistUpdate">The update to the gist</param>
Task<Gist> Edit(string id, GistUpdate gistUpdate);
/// <summary>
/// Deletes a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#delete-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
Task Delete(string id);
/// <summary>
/// Stars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#star-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
Task Star(string id);
/// <summary>
/// Unstars a gist
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#unstar-a-gist
/// </remarks>
/// <param name="id">The id of the gist</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Unstar")]
Task Unstar(string id);
/// <summary>
/// Checks if the gist is starred
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/gists/#check-if-a-gist-is-starred
/// </remarks>
/// <param name="id">The id of the gist</param>
Task<bool> IsStarred(string id);
}
}
+33
View File
@@ -605,6 +605,14 @@ namespace Octokit
return "feeds".FormatUri();
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns the list of public gists.
/// </summary>
public static Uri Gist()
{
return "gists".FormatUri();
}
/// <summary>
/// Returns the <see cref="Uri"/> for the specified commit.
/// </summary>
@@ -614,6 +622,31 @@ namespace Octokit
return "gists/{0}".FormatUri(id);
}
public static Uri ForkGist(string id)
{
return "gists/{0}/forks".FormatUri(id);
}
public static Uri PublicGists()
{
return "gists/public".FormatUri();
}
public static Uri StarredGists()
{
return "gists/starred".FormatUri();
}
public static Uri UsersGists(string user)
{
return "users/{0}/gists".FormatUri(user);
}
public static Uri StarGist(string id)
{
return "gists/{0}/star".FormatUri(id);
}
/// <summary>
/// Returns the <see cref="Uri"/> for the comments for the specified gist.
/// </summary>
+25
View File
@@ -0,0 +1,25 @@
using System;
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class GistRequest : RequestParameters
{
public GistRequest(DateTimeOffset since)
{
Since = since;
}
public DateTimeOffset Since { get; set; }
internal string DebuggerDisplay
{
get
{
return String.Format(CultureInfo.InvariantCulture, "Since: {0}", Since);
}
}
}
}
+33
View File
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class GistUpdate
{
public GistUpdate()
{
Files = new Dictionary<string, GistFileUpdate>();
}
public string Description { get; set; }
public IDictionary<string, GistFileUpdate> Files { get; private set; }
internal string DebuggerDisplay
{
get
{
return String.Format(CultureInfo.InvariantCulture, "Description: {0}", Description);
}
}
}
public class GistFileUpdate
{
public string NewFileName { get; set; }
public string Content { get; set; }
}
}
+41
View File
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
using System;
using System.Globalization;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class NewGist
{
public NewGist()
{
Files = new Dictionary<string, string>();
}
/// <summary>
/// The description of the gist.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Indicates whether the gist is public
/// </summary>
public bool Public { get; set; }
/// <summary>
/// Files that make up this gist using the key as Filename
/// and value as Content
/// </summary>
public IDictionary<string, string> Files { get; private set; }
internal string DebuggerDisplay
{
get
{
return String.Format(CultureInfo.InvariantCulture, "Description: {0}", Description);
}
}
}
}
+3
View File
@@ -89,12 +89,15 @@
<Compile Include="Clients\IMilestonesClient.cs" />
<Compile Include="Helpers\ParameterAttribute.cs" />
<Compile Include="Helpers\ReflectionExtensions.cs" />
<Compile Include="Models\Request\GistRequest.cs" />
<Compile Include="Models\Request\GistUpdate.cs" />
<Compile Include="Models\Request\LabelUpdate.cs" />
<Compile Include="Models\Request\MergePullRequest.cs" />
<Compile Include="Models\Request\MilestoneUpdate.cs" />
<Compile Include="Models\Request\NewBlob.cs" />
<Compile Include="Models\Request\NewCommit.cs" />
<Compile Include="Models\Request\NewCommitStatus.cs" />
<Compile Include="Models\Request\NewGist.cs" />
<Compile Include="Models\Request\NewLabel.cs" />
<Compile Include="Models\Request\NewMilestone.cs" />
<Compile Include="Models\Request\NewReference.cs" />
+3
View File
@@ -308,6 +308,9 @@
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
<Compile Include="Models\Request\NewGist.cs" />
<Compile Include="Models\Request\GistUpdate.cs" />
<Compile Include="Models\Request\GistRequest.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
+3
View File
@@ -303,6 +303,9 @@
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
<Compile Include="Models\Request\GistUpdate.cs" />
<Compile Include="Models\Request\GistRequest.cs" />
<Compile Include="Models\Request\NewGist.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+3
View File
@@ -166,6 +166,8 @@
<Compile Include="Http\IResponse.cs" />
<Compile Include="Http\Request.cs" />
<Compile Include="Models\Request\AuthorizationUpdate.cs" />
<Compile Include="Models\Request\GistRequest.cs" />
<Compile Include="Models\Request\GistUpdate.cs" />
<Compile Include="Models\Request\IssueRequest.cs" />
<Compile Include="Models\Request\IssueUpdate.cs" />
<Compile Include="Models\Request\LabelUpdate.cs" />
@@ -176,6 +178,7 @@
<Compile Include="Models\Request\NewBlob.cs" />
<Compile Include="Models\Request\NewCommit.cs" />
<Compile Include="Models\Request\NewCommitStatus.cs" />
<Compile Include="Models\Request\NewGist.cs" />
<Compile Include="Models\Request\NewIssue.cs" />
<Compile Include="Models\Request\NewLabel.cs" />
<Compile Include="Models\Request\NewMilestone.cs" />
+3
View File
@@ -99,6 +99,9 @@
<Compile Include="Clients\ReferencesClient.cs" />
<Compile Include="Models\Request\BodyWrapper.cs" />
<Compile Include="Models\Request\LabelUpdate.cs" />
<Compile Include="Models\Request\GistRequest.cs" />
<Compile Include="Models\Request\GistUpdate.cs" />
<Compile Include="Models\Request\NewGist.cs" />
<Compile Include="Models\Request\NewLabel.cs" />
<Compile Include="Models\Request\NewReference.cs" />
<Compile Include="Models\Request\ReferenceUpdate.cs" />