Skip to content

Instantly share code, notes, and snippets.

View ByteDev's full-sized avatar
:octocat:

ByteDev

:octocat:
View GitHub Profile
using System;
using System.Runtime.Serialization;

// ...

[Serializable]
public class EntityNotFoundException : Exception
{
    private const string DefaultMessage = "Entity does not exist.";
using System;
using System.Runtime.Serialization;

// ...

[Serializable]
public class EntityNotFoundException : Exception
{
 private const string DefaultMessage = "Entity does not exist.";
public class FakeResponseHandler : HttpMessageHandler
{
    private readonly HttpStatusCode _httpStatusCode;
    private readonly HttpContent _httpContent;

    public FakeResponseHandler(HttpStatusCode httpStatusCode) : this(httpStatusCode, null)
    {
    }
var response = await client.SendAsync(request);

using(var stream = await response.Content.ReadAsStreamAsync())
{
    using (var streamReader = new StreamReader(stream))
    {
      using (var jsonTextReader = new JsonTextReader(streamReader))
      {
 var customer = new JsonSerializer().Deserialize(jsonTextReader); 
// Tell HttpClient to auto decompress responses using the 
// particular compression method (e.g. GZip).
var client = new HttpClient(new HttpClientHandler
{
    AutomaticDecompression = DecompressionMethods.GZip
});

// ...
public async Task<Order> GetOrderAsync(CancellationToken cancellationToken = default)
{
    // setup your client & request
  
    var response = await client.SendAsync(request, cancellationToken);
}
var response = await client.SendAsync(request);

if (response.Content.Headers.ContentType.MediaType == "application/json")
{
    var json = await response.Content.ReadAsStringAsync();
  
    var order = JsonConvert.DeserializeObject<Order>(json);
}
var response = await client.SendAsync(request);

if (response.StatusCode == HttpStatusCode.BadRequest)
{
    var details = await response.Content.ReadAsStringAsync();
}
var response = await client.SendAsync(request);

if (response.StatusCode == HttpStatusCode.Unauthorized)
{
    // need to refresh the request security token!
}

if (response.IsSuccessStatusCode)
{
var request = new HttpRequestMessage(HttpMethod.Post, "api/orders");

request.Content = new JsonContent(json);