Last active
April 12, 2019 14:46
-
-
Save tmenier/d28216566a6fbf5ddc432b62c736da69 to your computer and use it in GitHub Desktop.
Flurl implementation of Scott Hanselman's SimpleCastClient https://www.hanselman.com/blog/HttpClientFactoryForTypedHttpClientInstancesInASPNETCore21.aspx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Do this in Startup. All calls to SimpleCast will use the same HttpClient instance. | |
FlurlHttp.ConfigureClient(Configuration["SimpleCastServiceUri"], cli => cli | |
.Configure(settings => | |
{ | |
// keeps logging & error handling out of SimpleCastClient | |
settings.BeforeCall = call => logger.LogWarning($"Calling {call.Request.RequestUri}"); | |
settings.OnError = call => logger.LogError($"Call to SimpleCast failed: {call.Exception}"); | |
}) | |
// adds default headers to send with every call | |
.WithHeaders(new | |
{ | |
Accept = "application/json", | |
User_Agent = "MyCustomUserAgent" // Flurl will convert that underscore to a hyphen | |
})); | |
using Flurl; | |
using Flurl.Http; | |
public class SimpleCastClient | |
{ | |
// look ma, no client! | |
private readonly string _baseUrl; | |
private readonly string _apiKey; | |
public SimpleCastClient(IConfiguration config) | |
{ | |
_baseUrl = config["SimpleCastServiceUri"]; | |
_apiKey = config["SimpleCastAPIKey"]; | |
} | |
public Task<List<Show>> GetShows() | |
{ | |
return _baseUrl | |
.AppendPathSegment("v1/podcasts/shownum/episodes.json") | |
.SetQueryParam("api_key", _apiKey) | |
.GetJsonAsync<List<Show>>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment