route GetOrCreateApplicationAuthentication to new endpoint, if user has set a Fingerprint

This commit is contained in:
Brendan Forster
2014-12-23 14:37:08 +09:30
parent 0bb9e4064e
commit 037aeb7b1e
6 changed files with 109 additions and 9 deletions
+33 -8
View File
@@ -1,4 +1,5 @@
#if NET_45
using System;
#if NET_45
using System.Collections.Generic;
#endif
using System.Threading.Tasks;
@@ -86,7 +87,6 @@ namespace Octokit
Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret");
Ensure.ArgumentNotNull(newAuthorization, "authorization");
var endpoint = ApiUrls.AuthorizationsForClient(clientId);
var requestData = new
{
client_secret = clientSecret,
@@ -95,7 +95,18 @@ namespace Octokit
note_url = newAuthorization.NoteUrl
};
return ApiConnection.Put<Authorization>(endpoint, requestData);
if (String.IsNullOrWhiteSpace(newAuthorization.Fingerprint))
{
// use classic API
var endpoint = ApiUrls.AuthorizationsForClient(clientId);
return ApiConnection.Put<Authorization>(endpoint, requestData);
}
else
{
// use new API
var endpoint = ApiUrls.AuthorizationsForClient(clientId, newAuthorization.Fingerprint);
return ApiConnection.Put<Authorization>(endpoint, requestData, null, previewAcceptsHeader);
}
}
/// <summary>
@@ -129,7 +140,6 @@ namespace Octokit
Ensure.ArgumentNotNull(newAuthorization, "authorization");
Ensure.ArgumentNotNullOrEmptyString(twoFactorAuthenticationCode, "twoFactorAuthenticationCode");
var endpoint = ApiUrls.AuthorizationsForClient(clientId);
var requestData = new
{
client_secret = clientSecret,
@@ -140,10 +150,25 @@ namespace Octokit
try
{
return await ApiConnection.Put<Authorization>(
endpoint,
requestData,
twoFactorAuthenticationCode);
if (String.IsNullOrWhiteSpace(newAuthorization.Fingerprint))
{
// use classic API
var endpoint = ApiUrls.AuthorizationsForClient(clientId);
return await ApiConnection.Put<Authorization>(
endpoint,
requestData,
twoFactorAuthenticationCode);
}
else
{
// use new API
var endpoint = ApiUrls.AuthorizationsForClient(clientId, newAuthorization.Fingerprint);
return await ApiConnection.Put<Authorization>(
endpoint,
requestData,
twoFactorAuthenticationCode,
previewAcceptsHeader);
}
}
catch (AuthorizationException e)
{
+18 -1
View File
@@ -26,10 +26,27 @@ namespace Octokit
/// <summary>
/// Returns the <see cref="Uri"/> that returns all authorizations for a given client
/// </summary>
/// <param name="clientId">The application client Id</param>
/// <param name="clientId">
/// The 20 character OAuth app client key for which to create the token.
/// </param>
public static Uri AuthorizationsForClient(string clientId)
{
return "authorizations/clients/{0}".FormatUri(clientId);
}
/// <summary>
/// Returns the <see cref="Uri"/> that authorizations for a given client and fingerprint
/// </summary>
/// <param name="clientId">
/// The 20 character OAuth app client key for
/// which to create the token.</param>
/// <param name="fingerprint">
/// A unique string to distinguish an authorization from others created
/// for the same client and user.
/// </param>
public static Uri AuthorizationsForClient(string clientId, string fingerprint)
{
return "authorizations/clients/{0}/{1}".FormatUri(clientId, fingerprint);
}
}
}
+21
View File
@@ -250,6 +250,27 @@ namespace Octokit
return response.BodyAsObject;
}
/// <summary>
/// Creates or replaces the API resource at the specified URI.
/// </summary>
/// <typeparam name="T">The API resource's type.</typeparam>
/// <param name="uri">URI of the API resource to create or replace</param>
/// <param name="data">Object that describes the API resource; this will be serialized and used as the request's body</param>
/// <param name="twoFactorAuthenticationCode">The two-factor authentication code in response to the current user's previous challenge</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <returns>The created API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public async Task<T> Put<T>(Uri uri, object data, string twoFactorAuthenticationCode, string accepts)
{
Ensure.ArgumentNotNull(uri, "uri");
Ensure.ArgumentNotNull(data, "data");
var response = await Connection.Put<T>(uri, data, twoFactorAuthenticationCode).ConfigureAwait(false);
return response.BodyAsObject;
}
/// <summary>
/// Updates the API resource at the specified URI.
/// </summary>
+11
View File
@@ -224,6 +224,17 @@ namespace Octokit
twoFactorAuthenticationCode);
}
public Task<IResponse<T>> Put<T>(Uri uri, object body, string twoFactorAuthenticationCode, string accepts)
{
return SendData<T>(uri,
HttpMethod.Put,
body,
accepts,
null,
CancellationToken.None,
twoFactorAuthenticationCode);
}
Task<IResponse<T>> SendData<T>(
Uri uri,
HttpMethod method,
+13
View File
@@ -155,6 +155,19 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<T> Put<T>(Uri uri, object data, string twoFactorAuthenticationCode);
/// <summary>
/// Creates or replaces the API resource at the specified URI.
/// </summary>
/// <typeparam name="T">The API resource's type.</typeparam>
/// <param name="uri">URI of the API resource to create or replace</param>
/// <param name="data">Object that describes the API resource; this will be serialized and used as the request's body</param>
/// <param name="twoFactorAuthenticationCode">The two-factor authentication code in response to the current user's previous challenge</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <returns>The created API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<T> Put<T>(Uri uri, object data, string twoFactorAuthenticationCode, string accepts);
/// <summary>
/// Updates the API resource at the specified URI.
/// </summary>
+13
View File
@@ -137,6 +137,19 @@ namespace Octokit
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
Task<IResponse<T>> Put<T>(Uri uri, object body, string twoFactorAuthenticationCode);
/// <summary>
/// Performs an asynchronous HTTP PUT request using the provided two factor authentication code.
/// Attempts to map the response body to an object of type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T">The type to map the response to</typeparam>
/// <param name="uri">URI endpoint to send request to</param>
/// <param name="body">The object to serialize as the body of the request</param>
/// <param name="twoFactorAuthenticationCode">Two factory authentication code to use</param>
/// <param name="accepts">Specifies accepted response media types.</param>
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
Task<IResponse<T>> Put<T>(Uri uri, object body, string twoFactorAuthenticationCode, string accepts);
/// <summary>
/// Performs an asynchronous HTTP PUT request that expects an empty response.
/// </summary>