whitesharx/httx

View on GitHub
Assets/Httx/Runtime/Requests/Types/Json.cs

Summary

Maintainability
A
50 mins
Test Coverage
// Copyright (C) 2020 White Sharx (https://whitesharx.com) - All Rights Reserved.
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
//

using System.Collections.Generic;
using System.Linq;
using Httx.Requests.Extensions;

namespace Httx.Requests.Types {
  public class Json<TBody> : BaseRequest {
    public Json(string url, TBody body = default) : base(null) {
      Url = url;

      if (typeof(TBody) == typeof(byte[])) {
        Body = (byte[])(object)body;
      } else {
        Body = Equals(body, default(TBody))
            ? default
            : this.ResolveBodyMapper<TBody>(Context.Instance).AsBody(body);
      }
    }

    public override string Url { get; }
    public override IEnumerable<byte> Body { get; }

    public override IEnumerable<KeyValuePair<string, object>> Headers {
      get {
        var headers = new Dictionary<string, object> {
            { "Accept", "application/json;charset=UTF-8" }
        };

        if (null != Body && 0 != Body.Count()) {
          headers.Add("Content-Type", "application/json;charset=UTF-8");
        }

        return headers;
      }
    }
  }

  public class Json : Json<byte[]> {
    public Json(string url, byte[] body = default) : base(url, body) { }
  }
}