/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Net.Http;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Net.Mime;
namespace QuantConnect.Api
{
///
/// API utility methods
///
public static class ApiUtils
{
///
/// Creates a POST with the specified endpoint and payload as form url encoded content.
///
/// The request endpoint
/// The request payload
/// The POST request
public static HttpRequestMessage CreatePostRequest(string endpoint, IEnumerable> payload = null)
{
var request = new HttpRequestMessage(HttpMethod.Post, endpoint);
if (payload != null)
{
request.Content = new FormUrlEncodedContent(payload);
}
return request;
}
///
/// Creates a POST with the specified endpoint and payload as json body
///
/// The request endpoint
/// The request payload
/// Settings for the json serializer
/// The POST request
public static HttpRequestMessage CreateJsonPostRequest(string endpoint, object payload = null, JsonSerializerSettings jsonSerializerSettings = null)
{
var request = new HttpRequestMessage(HttpMethod.Post, endpoint);
if (payload != null)
{
request.Content = new StringContent(payload as string ?? JsonConvert.SerializeObject(payload, jsonSerializerSettings),
new MediaTypeHeaderValue(MediaTypeNames.Application.Json));
}
return request;
}
}
}