Skip to content
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

Starting work on HttpClientFactory documentation #5483

Merged
merged 44 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1b897ac
Starting work on HttpClientFactory documentation (WIP)
stevejgordon Feb 15, 2018
37a56fb
Fixing some minor issues
stevejgordon Feb 15, 2018
0523d99
Updating per initial feedback
stevejgordon Feb 16, 2018
ca0cc90
Adding refit example
stevejgordon Feb 16, 2018
7026d16
Further feedback adjustments
stevejgordon Feb 16, 2018
ed1b3dd
Adding delegating handler middleware section
stevejgordon Feb 16, 2018
bfed490
Minor typo
stevejgordon Feb 16, 2018
812cf28
Initial Polly example
stevejgordon Feb 16, 2018
86e239d
Updating with latest feedback
stevejgordon Feb 19, 2018
ec14f81
Pass to remove you/your + improve readability and clarity
stevejgordon Feb 19, 2018
d527081
Additional tidy-up
stevejgordon Feb 19, 2018
ad34a62
Edits for latest feedback
stevejgordon Feb 21, 2018
37a5deb
UE pass
scottaddie Feb 21, 2018
b5aa44b
Snippet fixes
stevejgordon Feb 22, 2018
135a6e1
Add ms.custom metadata
scottaddie Feb 22, 2018
b62b134
Adding WIP sample project
stevejgordon Feb 23, 2018
c71f194
Minor edits to Fundamentals index page
scottaddie Feb 23, 2018
a7e1c51
Relocate master TOC link
scottaddie Feb 23, 2018
590150d
Feedback and sample update to 2.1 preview
stevejgordon Feb 27, 2018
567afbb
Fixing some missed HttpClientFactory items
stevejgordon Mar 1, 2018
5d18655
Initial snippets from sample
stevejgordon Mar 1, 2018
2251cb9
Fixing a copy/paste mistake
stevejgordon Mar 1, 2018
c8b6438
fixing code link
stevejgordon Mar 1, 2018
aaf26d3
Remove gerund from title
scottaddie Mar 1, 2018
c9d0daf
Fix wording in the description metadata value
scottaddie Mar 1, 2018
7825296
Minor verbiage tweaks
scottaddie Mar 2, 2018
b077c4b
WIP
stevejgordon Mar 20, 2018
44be1a5
Working on Polly sample and documentation
stevejgordon Apr 4, 2018
f95bd56
Using code from sample in docs
stevejgordon Apr 4, 2018
d231feb
UE pass
scottaddie Apr 5, 2018
957f760
Convert usage H2 headings to H3
scottaddie Apr 5, 2018
f715142
Adding initial logging information
stevejgordon Apr 6, 2018
05e139f
Verbiage tweaks
scottaddie Apr 10, 2018
af9c32f
Updating to 2.1 preview 2
stevejgordon Apr 16, 2018
aaf3240
Removing HTTPS redirection since launchSettings does not default to c…
stevejgordon Apr 17, 2018
fdf6d7c
Add monikerRange metadata for 2.1+
scottaddie Apr 23, 2018
c7d9e6d
Major sample and doc content update
stevejgordon Apr 26, 2018
b2c7d60
Adding section about configuring HttpMessageHandler
stevejgordon Apr 27, 2018
699e07d
Updating with feedback from Ryan
stevejgordon Apr 27, 2018
e4bd7f0
Feedback and updates
stevejgordon Apr 28, 2018
d7a5b8c
Incorporate Acrolinx feedback
scottaddie Apr 30, 2018
4c76827
Updating sample with preferred idioms and minor doc tweaks
stevejgordon May 2, 2018
8a37fc7
Fixing grammar and spelling
stevejgordon May 2, 2018
b34cda6
Add 2.1 Preview include
scottaddie May 2, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aspnetcore/fundamentals/http-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Using `IHttpClientFactory` in this fashion is a great way to refactor an existin

### Named clients

If an app requires multiple distinct uses of `HttpClient`, each with different configurations, an option is to use **named clients**. Configuration for a named `HttpClient` can be specified during registration in `ConfigureServices`.
If an app requires multiple distinct uses of `HttpClient`, each with a different configuration, an option is to use **named clients**. Configuration for a named `HttpClient` can be specified during registration in `ConfigureServices`.

[!code-csharp[](http-requests/samples/Startup.cs?name=snippet2)]

Expand All @@ -60,7 +60,7 @@ To consume a named client, a string parameter can be passed to `CreateClient`. S

[!code-csharp[](http-requests/samples/Pages/NamedClient.cshtml.cs?name=snippet1&highlight=14-18)]

In the preceding code, the request doesn't need to specify a hostname. I can provide just the path, since the base address configured for the client is used.
In the preceding code, the request doesn't need to specify a hostname. It can pass just the path, since the base address configured for the client is used.

### Typed clients

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace HttpClientFactorySample.GitHub
{
Expand All @@ -23,33 +22,15 @@ public GitHubService(HttpClient client)
Client = client;
}

public async Task<GitHubIssue> GetLatestDocsIssue()
public async Task<IEnumerable<GitHubIssue>> GetAspNetDocsIssues()
{
var response = await Client.GetAsync("/repos/aspnet/docs/issues?state=open&sort=created&direction=desc", HttpCompletionOption.ResponseHeadersRead);
var response = await Client.GetAsync("/repos/aspnet/docs/issues?state=open&sort=created&direction=desc");

response.EnsureSuccessStatusCode();

using (var stream = await response.Content.ReadAsStreamAsync())
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
var result = await response.Content.ReadAsAsync<IEnumerable<GitHubIssue>>();

while (await jsonReader.ReadAsync())
{
if (jsonReader.TokenType == JsonToken.StartObject)
{
var issue = serializer.Deserialize<GitHubIssue>(jsonReader);

if (issue != null)
{
return issue; // we only want the first object
}
}
}
}

return null;
return result;
}
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace HttpClientFactorySample.GitHub
{
Expand All @@ -21,9 +20,9 @@ public async Task<IEnumerable<string>> GetRepos()

response.EnsureSuccessStatusCode();

var responseContent = await response.Content.ReadAsStringAsync();
var result = await response.Content.ReadAsAsync<IEnumerable<string>>();

return JsonConvert.DeserializeObject<IEnumerable<string>>(responseContent);
return result;
}
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
@page
@model BasicUsageModel
@{
ViewData["Title"] = "Branches for Docs Repo";
}

<h1>Branches for Docs Repo</h1>
<h1>@ViewData["Title"]</h1>

<ul>
@foreach (var branch in Model.Branches)
{
<li>@branch.Name</li>
}
</ul>
@if (Model.GetBranchesError)
{
<p>Unable to get branches from GitHub. Please try again later.</p>
}
else
{
<ul>
@foreach (var branch in Model.Branches)
{
<li>@branch.Name</li>
}
</ul>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using HttpClientFactorySample.GitHub;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;

namespace HttpClientFactorySample.Pages
{
Expand All @@ -15,14 +14,16 @@ public class BasicUsageModel : PageModel

public IEnumerable<GitHubBranch> Branches { get; private set; }

public bool GetBranchesError { get; private set; }

public BasicUsageModel(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}

public async Task OnGet()
{
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/aspnet/docs/branches" );
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/aspnet/docs/branches");
request.Headers.Add("Accept", "application/vnd.github.v3+json");
request.Headers.Add("User-Agent", "HttpClientFactory-Sample");

Expand All @@ -32,13 +33,13 @@ public async Task OnGet()

if (response.IsSuccessStatusCode)
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rynowak Do you think this is a reasonable approach when consuming directly inside a Razor Page?

var data = await response.Content.ReadAsStringAsync();
Branches = JsonConvert.DeserializeObject<IEnumerable<GitHubBranch>>(data);
Branches = await response.Content.ReadAsAsync<IEnumerable<GitHubBranch>>();
}
else
{
GetBranchesError = true;
Branches = Array.Empty<GitHubBranch>();
}
}
}
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@

<h1>@ViewData["Title"]</h1>

<ul>
@if (Model.GetPullRequestsError)
{
<p>Unable to get issues from GitHub. Please try again later.</p>
}
else if (Model.HasPullRequests)
{
@foreach (var pr in Model.PullRequests)
{
<li>@pr.Title</li>
}
</ul>
}
else
{
<p>No pull requests found.</p>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using HttpClientFactorySample.GitHub;
Expand All @@ -15,6 +16,10 @@ public class NamedClientModel : PageModel

public IEnumerable<GitHubPullRequest> PullRequests { get; private set; }

public bool GetPullRequestsError { get; private set; }

public bool HasPullRequests => PullRequests.Any();

public NamedClientModel(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
Expand All @@ -30,11 +35,11 @@ public async Task OnGet()

if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
PullRequests = JsonConvert.DeserializeObject<IEnumerable<GitHubPullRequest>>(data);
PullRequests = await response.Content.ReadAsAsync<IEnumerable<GitHubPullRequest>>();
}
else
{
GetPullRequestsError = true;
PullRequests = Array.Empty<GitHubPullRequest>();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
@page
@model TypedClientModel
@{
ViewData["Title"] = "Latest GitHub Issues for ASP.NET Docs";
}

<h1>Latest GitHub Issue for ASP.NET Docs</h1>
<h1>@ViewData["Title"]</h1>

@if (Model.HasIssue)
@if (Model.GetIssuesError)
{
<h2>Title: @Model.LastestIssue.Title</h2>
<p><a href="@Model.LastestIssue.Url">View Issue</a></p>
<p>Unable to get issues from GitHub. Please try again later.</p>
}
else if (Model.HasIssue)
{
@foreach (var issue in Model.LastestIssues)
{
<h2>Title: @issue.Title</h2>
<p><a href="@issue.Url">View Issue</a></p>
}
}
else
{
<p>No issues found.</p>
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using HttpClientFactorySample.GitHub;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand All @@ -9,9 +13,11 @@ public class TypedClientModel : PageModel
{
private readonly GitHubService _gitHubService;

public GitHubIssue LastestIssue { get; private set; }
public IEnumerable<GitHubIssue> LastestIssues { get; private set; }

public bool HasIssue => LastestIssue != null;
public bool HasIssue => LastestIssues.Any();

public bool GetIssuesError { get; private set; }

public TypedClientModel(GitHubService gitHubService)
{
Expand All @@ -20,7 +26,15 @@ public TypedClientModel(GitHubService gitHubService)

public async Task OnGet()
{
LastestIssue = await _gitHubService.GetLatestDocsIssue();
try
{
LastestIssues = await _gitHubService.GetAspNetDocsIssues();
}
catch(HttpRequestException)
{
GetIssuesError = true;
LastestIssues = Array.Empty<GitHubIssue>();
}
}
}
#endregion
Expand Down