Skip to content

Commit

Permalink
I think this is a better way to setup the underlying baseUri on IConn…
Browse files Browse the repository at this point in the history
…eciton, to achieve managemet console access rather than requiring a specific GitHubClient that cant call normal API's

Instead, the management client methods can check the base Url and if it contains /api/v3/ they can set their relative endpoint Uri to include a leading "/" which will cause the /api/v3/ to be removed.
  • Loading branch information
ryangribble committed Apr 15, 2016
1 parent fa08997 commit b83cf88
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class EnterpriseManagementConsoleClientTests

public EnterpriseManagementConsoleClientTests()
{
_github = EnterpriseHelper.GetAuthenticatedManagementConsoleClient();
_github = EnterpriseHelper.GetAuthenticatedClient();
}

[GitHubEnterpriseTest]
Expand Down
5 changes: 0 additions & 5 deletions Octokit.Tests.Integration/EnterpriseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,6 @@ public static IGitHubClient GetAuthenticatedClient()
};
}

public static IGitHubClient GetAuthenticatedManagementConsoleClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitEnterpriseTests"), GitHubEnterpriseUrl, true);
}

public static IGitHubClient GetBasicAuthClient()
{
return new GitHubClient(new ProductHeaderValue("OctokitEnterpriseTests"), GitHubEnterpriseUrl)
Expand Down
17 changes: 17 additions & 0 deletions Octokit/Clients/Enterprise/EnterpriseManagementConsoleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ public EnterpriseManagementConsoleClient(IApiConnection apiConnection)
: base(apiConnection)
{ }

public Uri CorrectEndpointForManagementConsole(Uri endpoint)
{
Ensure.ArgumentNotNull(endpoint, "endpoint");

if (ApiConnection.Connection.BaseAddress != null &&
ApiConnection.Connection.BaseAddress.ToString().EndsWith("/api/v3/", StringComparison.OrdinalIgnoreCase))
{
// We need to get rid of the /api/v3/ for ManagementConsole requests
// if we specify the endpoint starting with a leading slash, that will achieve this
return string.Concat("/", endpoint.ToString()).FormatUri();
}

return endpoint;
}

/// <summary>
/// Gets GitHub Enterprise Maintenance Mode Status
/// </summary>
Expand All @@ -27,6 +42,7 @@ public Task<MaintenanceModeResponse> GetMaintenanceMode(string managementConsole
Ensure.ArgumentNotNullOrEmptyString(managementConsolePassword, "managementConsolePassword");

var endpoint = ApiUrls.EnterpriseManagementConsoleMaintenance(managementConsolePassword);
endpoint = CorrectEndpointForManagementConsole(endpoint);

return ApiConnection.Get<MaintenanceModeResponse>(endpoint);
}
Expand All @@ -44,6 +60,7 @@ public Task<MaintenanceModeResponse> EditMaintenanceMode(UpdateMaintenanceReques
Ensure.ArgumentNotNullOrEmptyString(managementConsolePassword, "managementConsolePassword");

var endpoint = ApiUrls.EnterpriseManagementConsoleMaintenance(managementConsolePassword);
endpoint = CorrectEndpointForManagementConsole(endpoint);

return ApiConnection.Post<MaintenanceModeResponse>(endpoint, maintenance.AsNamedFormEncodingString());
}
Expand Down
44 changes: 3 additions & 41 deletions Octokit/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,7 @@ public GitHubClient(ProductHeaderValue productInformation, ICredentialStore cred
/// The address to point this client to. Typically used for GitHub Enterprise
/// instances</param>
public GitHubClient(ProductHeaderValue productInformation, Uri baseAddress)
: this(productInformation, baseAddress, false)
{
}

/// <summary>
/// Create a new instance of the GitHub API v3 client pointing to the specified baseAddress.
/// </summary>
/// <param name="productInformation">
/// The name (and optionally version) of the product using this library. This is sent to the server as part of
/// the user agent for analytics purposes.
/// </param>
/// <param name="baseAddress">
/// The address to point this client to. Typically used for GitHub Enterprise
/// instances</param>
/// <param name="managementConsole">Specifies if this connection is for accessing the GitHub Enterprise management console</param>
public GitHubClient(ProductHeaderValue productInformation, Uri baseAddress, bool managementConsole)
: this(new Connection(productInformation, FixUpBaseUri(baseAddress, managementConsole)))
: this(new Connection(productInformation, FixUpBaseUri(baseAddress)))
{
}

Expand All @@ -84,24 +68,7 @@ public GitHubClient(ProductHeaderValue productInformation, Uri baseAddress, bool
/// The address to point this client to. Typically used for GitHub Enterprise
/// instances</param>
public GitHubClient(ProductHeaderValue productInformation, ICredentialStore credentialStore, Uri baseAddress)
: this(productInformation, credentialStore, baseAddress, false)
{
}

/// <summary>
/// Create a new instance of the GitHub API v3 client pointing to the specified baseAddress.
/// </summary>
/// <param name="productInformation">
/// The name (and optionally version) of the product using this library. This is sent to the server as part of
/// the user agent for analytics purposes.
/// </param>
/// <param name="credentialStore">Provides credentials to the client when making requests</param>
/// <param name="baseAddress">
/// The address to point this client to. Typically used for GitHub Enterprise
/// instances</param>
/// <param name="managementConsole">Specifies if this connection is for accessing the GitHub Enterprise management console</param>
public GitHubClient(ProductHeaderValue productInformation, ICredentialStore credentialStore, Uri baseAddress, bool managementConsole)
: this(new Connection(productInformation, FixUpBaseUri(baseAddress, managementConsole), credentialStore))
: this(new Connection(productInformation, FixUpBaseUri(baseAddress), credentialStore))
{
}

Expand Down Expand Up @@ -329,7 +296,7 @@ public IReleasesClient Release
/// </remarks>
public IEnterpriseClient Enterprise { get; private set; }

static Uri FixUpBaseUri(Uri uri, bool managementConsole)
static Uri FixUpBaseUri(Uri uri)
{
Ensure.ArgumentNotNull(uri, "uri");

Expand All @@ -338,11 +305,6 @@ static Uri FixUpBaseUri(Uri uri, bool managementConsole)
return GitHubApiUrl;
}

if (managementConsole)
{
return uri;
}

return new Uri(uri, new Uri("/api/v3/", UriKind.Relative));
}
}
Expand Down

0 comments on commit b83cf88

Please sign in to comment.