mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-02 19:00:47 +00:00
Change username=>login, add docs to GitHubClient
This commit is contained in:
@@ -14,7 +14,7 @@ namespace Burr.Tests
|
||||
var client = new GitHubClient();
|
||||
|
||||
// create a client with basic auth
|
||||
client = new GitHubClient { Username = "tclem", Password = "pwd" };
|
||||
client = new GitHubClient { Login = "tclem", Password = "pwd" };
|
||||
|
||||
// create a client with an oauth token
|
||||
client = new GitHubClient { Token = "oauthtoken" };
|
||||
@@ -37,7 +37,7 @@ namespace Burr.Tests
|
||||
|
||||
public async Task UserApi()
|
||||
{
|
||||
var client = new GitHubClient{ Username = "octocat", Password = "pwd" };
|
||||
var client = new GitHubClient{ Login = "octocat", Password = "pwd" };
|
||||
|
||||
// Get the authenticated user
|
||||
var authUser = await client.GetUserAsync();
|
||||
|
||||
@@ -79,7 +79,6 @@ namespace Burr.Tests
|
||||
((bool)jObj["Hireable"]).Should().Be(false);
|
||||
((string)jObj["Bio"]).Should().Be("once upon a time...");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Burr.Tests
|
||||
var client = new GitHubClient();
|
||||
|
||||
client.AuthenticationType.Should().Be(AuthenticationType.Anonymous);
|
||||
client.Username.Should().BeNull();
|
||||
client.Login.Should().BeNull();
|
||||
client.Password.Should().BeNull();
|
||||
client.Token.Should().BeNull();
|
||||
}
|
||||
@@ -29,10 +29,10 @@ namespace Burr.Tests
|
||||
[Fact]
|
||||
public void CanCreateBasicAuthClient()
|
||||
{
|
||||
var client = new GitHubClient { Username = "tclem", Password = "pwd" };
|
||||
var client = new GitHubClient { Login = "tclem", Password = "pwd" };
|
||||
|
||||
client.AuthenticationType.Should().Be(AuthenticationType.Basic);
|
||||
client.Username.Should().Be("tclem");
|
||||
client.Login.Should().Be("tclem");
|
||||
client.Password.Should().Be("pwd");
|
||||
client.Token.Should().BeNull();
|
||||
}
|
||||
@@ -44,7 +44,7 @@ namespace Burr.Tests
|
||||
|
||||
client.AuthenticationType.Should().Be(AuthenticationType.Oauth);
|
||||
client.Token.Should().Be("abiawethoasdnoi");
|
||||
client.Username.Should().BeNull();
|
||||
client.Login.Should().BeNull();
|
||||
client.Password.Should().BeNull();
|
||||
}
|
||||
|
||||
@@ -63,9 +63,9 @@ namespace Burr.Tests
|
||||
[InlineData(" ")]
|
||||
[InlineData(null)]
|
||||
[Theory]
|
||||
public void InvalidUserNameFallsBackToAnon(string t)
|
||||
public void InvalidLoginFallsBackToAnon(string t)
|
||||
{
|
||||
var client = new GitHubClient { Username = t };
|
||||
var client = new GitHubClient { Login = t };
|
||||
|
||||
client.AuthenticationType.Should().Be(AuthenticationType.Anonymous);
|
||||
}
|
||||
@@ -129,7 +129,7 @@ namespace Burr.Tests
|
||||
c.Setup(x => x.GetAsync<User>(endpoint)).Returns(fakeUserResponse);
|
||||
var client = new GitHubClient
|
||||
{
|
||||
Username = "tclem",
|
||||
Login = "tclem",
|
||||
Password = "pwd",
|
||||
Connection = c.Object
|
||||
};
|
||||
|
||||
+37
-14
@@ -10,6 +10,9 @@ using Burr.Http;
|
||||
|
||||
namespace Burr
|
||||
{
|
||||
/// <summary>
|
||||
/// A Client for the GitHub API v3. You can read more about the api here: http://developer.github.com
|
||||
/// </summary>
|
||||
public class GitHubClient
|
||||
{
|
||||
static Uri github = new Uri("https://api.github.com");
|
||||
@@ -20,6 +23,9 @@ namespace Burr
|
||||
return builder.Run(new HttpClientAdapter());
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of the GitHub API v3 client.
|
||||
/// </summary>
|
||||
public GitHubClient()
|
||||
{
|
||||
AuthenticationType = AuthenticationType.Anonymous;
|
||||
@@ -28,6 +34,11 @@ namespace Burr
|
||||
public AuthenticationType AuthenticationType { get; private set; }
|
||||
|
||||
Uri baseAddress;
|
||||
|
||||
/// <summary>
|
||||
/// The base address of the GitHub API. This defaults to https://api.github.com,
|
||||
/// but you can change it if needed (to talk to a GitHub:Enterprise server for instance).
|
||||
/// </summary>
|
||||
public Uri BaseAddress
|
||||
{
|
||||
get { return baseAddress ?? (baseAddress = github); }
|
||||
@@ -50,18 +61,22 @@ namespace Burr
|
||||
}
|
||||
}
|
||||
|
||||
string username;
|
||||
public string Username
|
||||
string login;
|
||||
|
||||
/// <summary>
|
||||
/// GitHub login (or email address). Setting this property will enable basic authentication.
|
||||
/// </summary>
|
||||
public string Login
|
||||
{
|
||||
get { return username; }
|
||||
get { return login; }
|
||||
set
|
||||
{
|
||||
if (value == username) return;
|
||||
if (value == login) return;
|
||||
|
||||
Token = null;
|
||||
|
||||
username = value;
|
||||
if (username.IsNotBlank())
|
||||
login = value;
|
||||
if (login.IsNotBlank())
|
||||
{
|
||||
AuthenticationType = AuthenticationType.Basic;
|
||||
}
|
||||
@@ -69,6 +84,10 @@ namespace Burr
|
||||
}
|
||||
|
||||
string password;
|
||||
|
||||
/// <summary>
|
||||
/// GitHub password. Setting this property will enable basic authentication.
|
||||
/// </summary>
|
||||
public string Password
|
||||
{
|
||||
get { return password; }
|
||||
@@ -87,6 +106,10 @@ namespace Burr
|
||||
}
|
||||
|
||||
string token;
|
||||
|
||||
/// <summary>
|
||||
/// Oauth2 token. Setting this property will enable oauth.
|
||||
/// </summary>
|
||||
public string Token
|
||||
{
|
||||
get { return token; }
|
||||
@@ -94,7 +117,7 @@ namespace Burr
|
||||
{
|
||||
if (value == token) return;
|
||||
|
||||
Username = null;
|
||||
Login = null;
|
||||
Password = null;
|
||||
|
||||
token = value;
|
||||
@@ -106,19 +129,19 @@ namespace Burr
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="User"/> for the specified username. Returns the
|
||||
/// Authenticated <see cref="User"/> if no username is given.
|
||||
/// Returns a <see cref="User"/> for the specified login (username). Returns the
|
||||
/// Authenticated <see cref="User"/> if no login (username) is given.
|
||||
/// </summary>
|
||||
/// <param name="username">Optional GitHub username</param>
|
||||
/// <param name="login">Optional GitHub login (username)</param>
|
||||
/// <returns>A <see cref="User"/></returns>
|
||||
public async Task<User> GetUserAsync(string username = null)
|
||||
public async Task<User> GetUserAsync(string login = null)
|
||||
{
|
||||
if (username.IsBlank() && AuthenticationType == AuthenticationType.Anonymous)
|
||||
if (login.IsBlank() && AuthenticationType == AuthenticationType.Anonymous)
|
||||
{
|
||||
throw new AuthenticationException("You must be authenticated to call this method. Either supply a username/password or an oauth token.");
|
||||
throw new AuthenticationException("You must be authenticated to call this method. Either supply a login/password or an oauth token.");
|
||||
}
|
||||
|
||||
var endpoint = username.IsBlank() ? "/user" : string.Format("/users/{0}", username);
|
||||
var endpoint = login.IsBlank() ? "/user" : string.Format("/users/{0}", login);
|
||||
var res = await Connection.GetAsync<User>(endpoint);
|
||||
|
||||
return res.BodyAsObject;
|
||||
|
||||
Reference in New Issue
Block a user