-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpClient injection #131
HttpClient injection #131
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was the intention that this method could be used by the client to configure the HttpClient
passed to the ctor? If so, should it be public?
Octokit.GraphQL.Core/Connection.cs
Outdated
@@ -61,12 +75,26 @@ public Connection(ProductHeaderValue productInformation, Uri uri, ICredentialSto | |||
} | |||
} | |||
|
|||
private HttpClient CreateHttpClient(ProductHeaderValue header) | |||
private static void ConfigureHttpClient(HttpClient httpClient, ProductHeaderValue header) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was the intention that this method could be used by the client to configure the HttpClient
passed to the ctor? If so, should it be public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this was more sort-of so that if the user didn't configure the client fully, a default configuration would be applied, though turns out I forgot to plumb that into the new constructor.
I'm not entirely sure what the constructor options that supporting this should be yet, I just added a minimum to make the test work to show how you could use it.
So I had the obvious realization the other day that a lot of this has already come from octokit/octokit.net for good reason and should continue to be based off of that. The constructor of Connection should more closely match what is found here.. |
Octokit.GraphQL.Core/Connection.cs
Outdated
@@ -40,6 +40,20 @@ public Connection(ProductHeaderValue productInformation, Uri uri, ICredentialSto | |||
HttpClient = CreateHttpClient(productInformation); | |||
} | |||
|
|||
/// <inheritdoc /> | |||
public Connection(HttpClient httpClient, ICredentialStore credentialStore) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All constructors should roll into this one, and the signature should be..
Connection(ProductHeaderValue productInformation, Uri uri, ICredentialStore credentialStore, HttpClient httpClient)
Octokit.GraphQL.Core/Connection.cs
Outdated
@@ -40,6 +40,20 @@ public Connection(ProductHeaderValue productInformation, Uri uri, ICredentialSto | |||
HttpClient = CreateHttpClient(productInformation); | |||
} | |||
|
|||
/// <inheritdoc /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason we have /// <inheritdoc/>
on all these constructors.
Remove this line and merge the branch overeager-inheritdoc
into this branch to take care of the others.
Octokit.GraphQL.Core/Connection.cs
Outdated
public Connection(HttpClient httpClient, ICredentialStore credentialStore) | ||
{ | ||
HttpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); | ||
CredentialStore = credentialStore ?? throw new ArgumentNullException(nameof(credentialStore)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, we should have been doing this in the previous constructor.
Be sure to add more for all the arguments when you add them.
Octokit.GraphQL.Core/Connection.cs
Outdated
|
||
if (httpClient.BaseAddress == null) | ||
{ | ||
httpClient.BaseAddress = GithubApiUri; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much like in octokit.net we shouldn't handle this here, just set a member variable and we will handle this later.
The rest of the differences between us and octokit.net is how we execute things.
|
Octokit.GraphQL.Core/Connection.cs
Outdated
} | ||
} | ||
|
||
private static HttpClient CreateHttpClient(ProductHeaderValue header) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method isn't needed, we should just new HttpClient()
in the constructors above.
Octokit.GraphQL.Core/Connection.cs
Outdated
if (httpClient.DefaultRequestHeaders.UserAgent.Count == 0) | ||
{ | ||
var userAgent = new ProductInfoHeaderValue(header.Name, header.Version); | ||
httpClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting userAgent and headers should be performed when we are executing the request.
The final major difference in octokit.net is they have an interface for |
@martincostello thanks for getting this conversation started 🎖. |
@StanleyGoldman Sure, I'll take a look at incorporating the feedback at some point later this week or early next 👍 |
@StanleyGoldman I've done some refactoring based on the feedback and rebased onto master. The changes I've made are:
|
Hey @martincostello I 💟 all that you are doing, but we gotta be careful of not doing too much in a single pull request. I haven't gotten the chance to look at this pull request again in deatil, but based on what you said in your last comment. It sounds like we can move some of these changes out to a different pull request. Would it be possible to rebase this branch and move items |
@StanleyGoldman No problem. I'll do those now, then rebase this once those are in. Any ideas roughly on how to go about fixing the issues with the type name formatting breaking those expression tests? |
@StanleyGoldman Have raised the two PRs for items 7 and 8. |
Thanks @martincostello
LOL. No clue yet. |
Okay, thanks for entertaining me there and separating out those other issues. @grokys is AFK for a few more days and I'd like to keep on moving forward. I pushed a temporary branch called Would you mind rebasing this branch onto that and changing this pull request to target that branch? |
Sure. I'll do that shortly. |
Rebased and target branch updated. |
Blargh, I messed up something slightly in #140. I'll fix that now, then rebase this again later if you could update |
Merged. |
Updated with the latest |
Wanna rebase this onto |
Sure 😄 |
Add a test showing a proof-of-concept for injection of HttpClient into Connection so that IHttpClientFactory can be used for scenarios such as ASP.NET Core applications. Relates to #125.
Route all constructors into new constructor that accepts HttpClient. Add extra constructor to simplify dependency injection for GitHub.com. Add XML documentation to class and members based on Octokit. Add parameter validation. Change GithubApiUri to a property. Set HttpRequestMessage properties (User Agent etc.) per request. Add ConfigureAwait(false) to awaits. Add optional parameter for using a CancellationToken.
Rebased. Also, pending further code review, I should add a bunch more unit tests for the changes in |
You are never going to hear me argue against more tests... 😼 |
Add a TODO comment suggesting changing ICredentialStore to support CancellationToken.
Make the run method in Connection virtual to allow more useful derived classes.
Add unit tests to cover the basic behaviour of managing HTTP requests.
Have added some new unit tests for the I've also made It also occurred to me that passing the |
We haven't forgotten you, just tending to our other projects. |
The interface change makes sense, oddly enough though no one seems to have encountered this issue with octokit.net. Connection.Run
|
I made that change you were referring to and made some minor tweaks |
|
||
namespace Octokit.GraphQL.IntegrationTests.Configuration | ||
{ | ||
public static class HttpClientFactoryTests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the test class is static
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xunit supports classes/methods being static for tests if everything is self contained to the fact/theory - happy to make non-static if that’s your convention.
public static class HttpClientFactoryTests | ||
{ | ||
[Fact] | ||
public static async Task Can_Configure_With_HttpClient_Factory() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with this method?
Other than that, this is looking pretty good IMO. |
Fix unit test broken by refactoring.
I've fixed up a test, but other than any other feedback, I think I'm done on this PR. I've also done a commit on a separate branch (17c3cf4) to plumb in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Adds a test showing a proof-of-concept for injection of
HttpClient
intoConnection
so thatIHttpClientFactory
can be used for scenarios such as ASP.NET Core 2.1+ applications.Relates to #125.
Depends on: