using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Windows;

public static class Http
    {


        public static string PostJson<T>(string url, Dictionary<string, string> headers, T jsonobject, int timeout = 10000, bool wlog=true)
        {
            RestClient client = new RestClient(url) { Timeout = timeout };
            client.UseSerializer(new CustSerialize());
            
            RestRequest request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            if (headers != null)
            {
                foreach (var head in headers)
                {
                    request.AddHeader(head.Key, head.Value);
                }
            }
            request.AddJsonBody(jsonobject);
            IRestResponse response = client.Execute(request);
            string s = response.Content;
            //if (wlog)
            //    LogNet.log.Info("Return:" + s);
            return s;
        }

    }
    public class CustSerialize : RestSharp.Serialization.IRestSerializer
    {
        public string ContentType { get; set; } = "application/json";

        public string[] SupportedContentTypes { get; set; } = new string[] { "application/json" };

        public DataFormat DataFormat { get; set; } = DataFormat.Json;

        public T Deserialize<T>(IRestResponse response)
        {
            return JsonConvert.DeserializeObject<T>(response.Content);
        }

        public string Serialize(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        public string Serialize(Parameter parameter)
        {
            return JsonConvert.SerializeObject(parameter.Value);

        }
    }