Skip to content

Instantly share code, notes, and snippets.

View tmenier's full-sized avatar

Todd Menier tmenier

View GitHub Profile
@tmenier
tmenier / NewtonsoftJsonSerializer.cs
Created June 4, 2022 21:36
A Newtonsoft-based JSON serializer for Flurl.Http 4+
using System.IO;
using Newtonsoft.Json;
namespace Flurl.Http.Configuration
{
/// <summary>
/// ISerializer implementation based on Newtonsoft.Json.
/// Default serializer used in calls to GetJsonAsync, PostJsonAsync, etc.
/// </summary>
public class NewtonsoftJsonSerializer : ISerializer
@tmenier
tmenier / Throttler.cs
Created January 31, 2020 13:32
Task.WhenAll on steroids
public static class Throttler
{
/// <summary>
/// Concurrently run a task for each data item. Like Task.WhenAll, except you can cap the number allowed to run at a time,
/// and enforce a minimum pause between the start of each.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items">A list of data items.</param>
/// <param name="minPause">Minimum wait time in milliseconds between starting tasks.</param>
/// <param name="maxConcurrent">Maximum tasks allowed to run at the same time.</param>
[Test]
public async Task StackOverflowQuestion() {
var message = "";
try {
var cli = new FlurlClient("https://www.google.com").EnableCookies();
var s = await cli.Request().GetStringAsync();
var result = await cli.Request("foo").PostUrlEncodedAsync(new { x = "x" });
}
catch (FlurlHttpException ex) {
message = ex.Message;
// 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
@tmenier
tmenier / FlurlAndVcr.cs
Last active March 6, 2018 12:55
Flurl.Http + vcr-sharp
// 1. define a custom HttpClientFactory
public class VcrHttpClientFactory : DefaultHttpClientFactory
{
private readonly string _cassette;
public VcrHttpClientFactory(string cassette) {
_cassette = cassette;
}
public override HttpMessageHandler CreateMessageHandler() {
@tmenier
tmenier / flurl-example.cs
Last active March 5, 2022 19:30
Flurl example - post URL-encoded, receive JSON
var result = await "http://localhost:8080/lzp/servRoot"
.PostUrlEncodedAsync(new {
className = "com.lzp.service.UserInfoService",
methodName = "login",
user_name = "123",
user_pwd = "123"
})
.ReceiveJson<TestResultModel>();
@tmenier
tmenier / CopyDeployableWebFiles.ps1
Created February 24, 2014 23:25
A PowerShell function to copy all deployable files from an ASP.NET Web Application project (WebForms or MVC) to another directory. Takes the path to the project file and the path to the destination folder. Useful in CI environments. In my tests, running this after msbuild has built the project is much faster than including a WebDeploy step in th…
function copy-deployable-web-files($proj_path, $deploy_dir) {
# copy files where Build Action = "Content"
$proj_dir = split-path -parent $proj_path
[xml]$xml = get-content $proj_path
$xml.Project.ItemGroup | % { $_.Content } | % { $_.Include } | ? { $_ } | % {
$from = "$proj_dir\$_"
$to = split-path -parent "$deploy_dir\$_"
if (!(test-path $to)) { md $to }
cp $from $to
}