Skip to content

Instantly share code, notes, and snippets.

public void ConfigureServices(IServiceCollectionservices)
{
services.AddMemoryCache();
services.AddHttpClient<IUserApiAuthenticationService, UserApiAuthenticationService>()
.ConfigureHttpClient(c => c.BaseAddress ="http://urltotheuserapi.com");
services.AddTransient<UserApiAuthenticationHandler>();
services.AddHttpClient<UserService, UserService>()
.ConfigureHttpClient(c => c.BaseAddress ="http://urltotheuserapi.com")
.AddHttpMessageHandler<UserApiAuthenticationHanler>();
public class AuthResponse
{
public string Token { get; set; }
public DateTime Expiration { get; set; }
}
public interface IUserService
{
Task<IReadOnlyCollection<User>> GetAllUsers();
Task UpdateUser(User userToUpdate);
}
var body = new { login = "login", password = "password" };
var response = await _httpClient.PostAsync("login", new StringContent(JsonConvert.SerializeObject(body)));
var authResponse = await response.Content.ReadAsAsync<AuthResponse>();
public class UserService :
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IReadOnlyCollection<User>> GetAllUsers()
{
public class UserApiAuthenticationHandler : DelegatingHandler
{
private readonly IUserApiAuthenticationService _authenticationService;
public UserApiAuthenticationHandler(IUserApiAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationTokencancellationToken)
public class UserApiAuthenticationService : IUserApiAuthenticationService
{
private readonly IMemoryCache _memoryCache;
private readonly HttpClient _httpClient;
public UserApiAuthenticationService(HttpClient httpClient, IMemoryCache memoryCache)
{
_httpClient = httpClient;
_memoryCache = memoryCache;
}
public class UserService : IUserService
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IReadOnlyCollection<User>> GetAllUsers()
{
@TechWatching
TechWatching / TestingHandlebars.csx
Created March 9, 2019 15:27
C# script that use the Handlebars.Net library to do HTML templating
#r "nuget: Handlebars.Net, 1.9.5"
using HandlebarsDotNet;
string html = @"
<ul class=""people"">
{{#each people}}
<li>{{FirstName}} {{LastName}} - {{Job}}</li>
{{/each}}
</ul>";
@TechWatching
TechWatching / swapi.http
Created March 4, 2019 06:10
HTTP requests for the Star Wars API
#### List of start wars planets
GET https://swapi.co/api/planets/ HTTP/1.1
### Get Luke Skywalker
# @name lukeRequest
GET https://swapi.co/api/people/?search=Luke HTTP/1.1
### Get Luke Skywalker home planet
GET {{lukeRequest.response.body.results[0].homeworld}} HTTP/1.1