using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Newtonsoft.Json.Linq; namespace AyaNova.PlugIn.V8 { class util { const string TEST_ROUTE = "ServerInfo"; const string API_BASE_ROUTE = "api/v8/"; static HttpClient client = new HttpClient(); //url once known to be good static string ApiBaseUrl { get; set; } internal static string JWT { get; set; } static bool Initialized { get; set; } public util() { Initialized = false; JWT = string.Empty; } private static void InitClient() { if (Initialized) return; client.BaseAddress = new Uri(ApiBaseUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); Initialized = true; } /// /// Only a return value of "OK" is ok /// /// /// public static async Task TestUrlAsync(string serverUrl) { if (string.IsNullOrEmpty(serverUrl)) return "Server url required"; if (!serverUrl.Contains("/api/")) { if (!serverUrl.EndsWith("/")) serverUrl += "/"; serverUrl += API_BASE_ROUTE; } if (!serverUrl.StartsWith("http")) serverUrl = "http://" + serverUrl; //try to connect, ping the server api if (!Initialized) { client.BaseAddress = new Uri(serverUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); Initialized = true; } try { HttpResponseMessage response = await client.GetAsync(serverUrl + TEST_ROUTE); if (response.IsSuccessStatusCode) { var ret = await response.Content.ReadAsStringAsync(); if (ret.Contains("AyaNova")) { ApiBaseUrl = serverUrl; return "OK"; } } else { return "Failed: " + response.StatusCode.ToString(); } } catch { return "Failed"; } return "failed"; } public async static Task AuthenticateAsync(string login, string password = null) { InitClient(); if (password == null) password = login; dynamic creds = new JObject(); creds.login = login; creds.password = password; ApiResponse a = await PostAsync("Auth", creds.ToString()); if (a.HttpResponse.IsSuccessStatusCode) { JWT=a.ObjectResponse["data"]["token"].Value(); return true; } return false; } public async static Task GetAsync(string route, string authToken = null, string bodyJsonData = null) { var requestMessage = new HttpRequestMessage(HttpMethod.Get, route); if (!string.IsNullOrWhiteSpace(JWT)) requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT); if (!string.IsNullOrWhiteSpace(bodyJsonData)) requestMessage.Content = new StringContent(bodyJsonData, System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.SendAsync(requestMessage); var responseAsString = await response.Content.ReadAsStringAsync(); return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) }; } public async static Task PostAsync(string route, string postJson = null) { var requestMessage = new HttpRequestMessage(HttpMethod.Post, route); if (!string.IsNullOrWhiteSpace(JWT)) requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT); if (!string.IsNullOrWhiteSpace(postJson)) requestMessage.Content = new StringContent(postJson, System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.SendAsync(requestMessage); var responseAsString = await response.Content.ReadAsStringAsync(); return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) }; } //eoc public class ApiResponse { public HttpResponseMessage HttpResponse { get; set; } public JObject ObjectResponse { get; set; } public string CompactResponse { get { return ObjectResponse.ToString(Newtonsoft.Json.Formatting.None); } } } /// /// /// /// /// public static JObject Parse(string jsonString) { if (string.IsNullOrWhiteSpace(jsonString)) { return null; } return JObject.Parse(jsonString); } }//eoc }//ens