Skip to content

Utility class packages used at Stack Exchange...but on NuGet!

License

Notifications You must be signed in to change notification settings

KrisJohnstone/StackExchange.Utils

 
 

Repository files navigation

Stack Exchange Utility Packages

Home for the StackExchange.Util.* packages.

StackExchange.Utils.Http

Http is a helper class for making HttpClient calls. The send, deserialization, options, and verb portions are exchangable. Some examples:

POSTing a string and expecting a string:

var result = await Http.Request("https://example.com")
                       .SendPlaintext("test")
                       .ExpectString()
                       .PostAsync();

POSTing JSON and expecting protobuf back:

var result = await Http.Request("https://example.com")
                       .SendJson(new { name = "my thing" })
                       .ExpectProtobuf<MyType>()
                       .PostAsync();

Sending nothing and GETing JSON back:

var result = await Http.Request("https://example.com")
                       .ExpectJson<MyType>()
                       .GetAsync();

Sending nothing and GETing JSON back, with a timeout, ignoring 404 responses:

var result = await Http.Request("https://example.com")
                       .IgnoredResponseStatuses(HttpStatusCode.NotFound)
                       .WithTimeout(TimeSpan.FromSeconds(20))
                       .ExpectJson<MyType>()
                       .GetAsync();
// Handle the response:
if (result.Success)
{
    //result.Data is MyType, deserialized from the returned JSON
}
else
{
    // result.Error
    // result.StatusCode
    // result.RawRequest
    // result.RawResponse
}

License

StackExchange.Utils is licensed under the MIT license.

About

Utility class packages used at Stack Exchange...but on NuGet!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 96.8%
  • PowerShell 2.8%
  • Batchfile 0.4%