Complete Gist API

This commit is contained in:
Robert Mills
2013-12-02 22:44:28 -05:00
committed by Marius Ungureanu
parent c82498a439
commit 91db7e59e9
9 changed files with 566 additions and 2 deletions
+176
View File
@@ -23,6 +23,107 @@ 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"), null);
}
[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"),
Arg.Is<IDictionary<string, string>>(x => x.ContainsKey("since")));
}
[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"),
Arg.Is<IDictionary<string, string>>(x => x.ContainsKey("since")));
}
[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"),
Arg.Is<IDictionary<string, string>>(x => x.ContainsKey("since")));
}
[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]
@@ -67,6 +168,81 @@ public class GistsClientTests
}
}
public class TheStarMethods
{
[Fact]
public void RequestsCorrectStarUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.Star("1");
connection.Received().Get<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"), null);
}
[Fact]
public void RequestsCorrectUnstarUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.Unstar("1");
connection.Received().Get<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"), null);
}
[Fact]
public void RequestsCorrectCheckIfStarredUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new GistsClient(connection);
client.IsStarred("1");
connection.Received().Get<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"), null);
}
}
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/fork"),
Arg.Is<object>(o => o == null));
}
}
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().Post<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1"), Arg.Any<object>());
}
}
public class TheCtor
{
[Fact]
+199 -1
View File
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace Octokit
@@ -64,6 +66,18 @@ namespace Octokit
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>
@@ -77,5 +91,189 @@ namespace Octokit
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>
/// <param name="since">Only gists updated at or after this time are returned</param>
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/#edit-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;
}
}
}
}
+122 -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
@@ -24,6 +26,78 @@ namespace Octokit
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>
@@ -33,6 +107,25 @@ namespace Octokit
/// <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>
@@ -41,5 +134,33 @@ namespace Octokit
/// </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);
}
}
+25
View File
@@ -622,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>
+15
View File
@@ -0,0 +1,15 @@
using System;
using Octokit.Internal;
namespace Octokit
{
public class GistRequest : RequestParameters
{
public GistRequest(DateTimeOffset since)
{
Since = since;
}
public DateTimeOffset Since { get; set; }
}
}
+23
View File
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using Octokit.Internal;
namespace Octokit
{
public class GistUpdate
{
public GistUpdate()
{
Files = new Dictionary<string, GistFileUpdate>();
}
public string Description { get; set; }
public IDictionary<string, GistFileUpdate> Files { get; private set; }
}
public class GistFileUpdate
{
public string NewFileName { get; set; }
public string Content { get; set; }
}
}
+2
View File
@@ -89,6 +89,8 @@
<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" />
+2
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" />
+2
View File
@@ -99,6 +99,8 @@
<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" />