using Newtonsoft.Json; using PCLWebUtility; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Swagger { #region Base service response definition public class IBaseResponse { public bool error { get; set; } public string error_message { get; set; } public int error_code { get; set; } public T results { get; set; } public bool mocked { get; set; } public string execution_time; } #endregion public static class Api { private static string ApiKey = null; private static string BasePath = "http://boite-a-images.herokuapp.com/"; public static void Initialize(string ApiKey) { Api.ApiKey = ApiKey; } #region Base HTTP function definition public static HttpWebRequest addHeaders(HttpWebRequest request) { var headers = new WebHeaderCollection(); headers["X-API-TOKEN"] = Api.ApiKey; //request.Headers.Add("X-API-TOKEN", Api.ApiKey); request.Headers = headers; return request; } public static string ToQueryString(this Dictionary source) { return String.Join("&", source.Select(kvp => String.Format("{0}={1}", WebUtility.UrlEncode(kvp.Key), WebUtility.UrlEncode(kvp.Value))).ToArray()); } public static async Task PostFile(string url, byte[] byteArray, string contentType = "application/json") { HttpWebRequest request = CreateBaseRequest("POST", url, contentType); //request.ContentLength = byteArray.Length; try { Stream dataStream = await request.GetRequestStreamAsync(); await dataStream.WriteAsync(byteArray, 0, byteArray.Length); //dataStream.Close(); } catch (Exception e) { throw e; } var result = await GetResponse(request); return JsonConvert.DeserializeObject(result); } public static async Task PostForm(string url, Dictionary content, string contentType = "application/x-www-form-urlencoded") { HttpWebRequest request = CreateBaseRequest("POST", url, contentType); byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(content.ToQueryString()); //request.ContentLength = byteArray.Length; try { Stream dataStream = await request.GetRequestStreamAsync(); await dataStream.WriteAsync(byteArray, 0, byteArray.Length); //dataStream.Close(); } catch (Exception e) { throw e; } var result = await GetResponse(request); return JsonConvert.DeserializeObject(result); } public static async Task PostRaw(string url, string content, string contentType = "application/json") { HttpWebRequest request = CreateBaseRequest("POST", url, contentType); byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(content); //request.ContentLength = byteArray.Length; try { using (var dataStream = await request.GetRequestStreamAsync()) { await dataStream.WriteAsync(byteArray, 0, byteArray.Length); } } catch (Exception e) { throw e; } var result = await GetResponse(request); return JsonConvert.DeserializeObject(result); } public static async Task PutRaw(string url, string content, string contentType = "application/json") { HttpWebRequest request = CreateBaseRequest("PUT", url, contentType); byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(content); //request.ContentLength = byteArray.Length; try { Stream dataStream = await request.GetRequestStreamAsync(); await dataStream.WriteAsync(byteArray, 0, byteArray.Length); //dataStream.Close(); } catch (Exception e) { throw e; } var result = await GetResponse(request); return JsonConvert.DeserializeObject(result); } public static async Task Get(string url, string contentType = "application/json") { HttpWebRequest request = CreateBaseRequest("GET", url, contentType); var result = await GetResponse(request); return JsonConvert.DeserializeObject(result); } public static async Task Delete(string url, string contentType = "application/json") { HttpWebRequest request = CreateBaseRequest("DELETE", url, contentType); var result = await GetResponse(request); return JsonConvert.DeserializeObject(result); } public static async Task DeleteRaw(string url, string content, string contentType = "application/json") { HttpWebRequest request = CreateBaseRequest("DELETE", url, contentType); var result = await GetResponse(request); return JsonConvert.DeserializeObject(result); } public static async Task DeleteForm(string url, Dictionary content, string contentType = "application/json") { HttpWebRequest request = CreateBaseRequest("DELETE", url, contentType); var result = await GetResponse(request); return JsonConvert.DeserializeObject(result); } public static HttpWebRequest CreateBaseRequest(string method, string url, string contentType = "application/json") { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Api.BasePath + url); request = addHeaders(request); request.Method = method; request.ContentType = contentType; request.Accept = contentType; return request; } public static async Task GetResponse(HttpWebRequest request) { try { HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); var dataStreamResponse = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStreamResponse); return reader.ReadToEnd(); } catch (WebException e) { throw e; } } #endregion } }