using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Net.Http; using System.Net.Http.Headers; using System.Text.Json.Nodes; public class OnetWebService { public class QueryParams : IEnumerable> { private List> internalParams = new List>(); public IEnumerator> GetEnumerator() => internalParams.GetEnumerator(); System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => internalParams.GetEnumerator(); public void Add(string key, string value) => internalParams.Add(new KeyValuePair(key, value)); } private string baseURL; private HttpClient client; private static string EncodeAuth(string username, string password) { return Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(username + ":" + password)); } public OnetWebService(string username, string password) { HttpClientHandler handler = new HttpClientHandler() { AllowAutoRedirect = false, }; client = new HttpClient(handler); client.Timeout = new TimeSpan(0, 0, 10); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.UserAgent.ParseAdd("dotnet-OnetWebService/1.00 (bot)"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", EncodeAuth(username, password)); SetVersion(); } public void SetVersion(string version = "") { if (version == "") { baseURL = "https://services.onetcenter.org/ws/"; } else { baseURL = "https://services.onetcenter.org/ws/v" + version + "/ws/"; } } public async Task Call(string path, QueryParams query = null) { List encoded_params = new List(); if (query != null) { foreach (KeyValuePair pair in query) { encoded_params.Add(System.Net.WebUtility.UrlEncode(pair.Key) + "=" + System.Net.WebUtility.UrlEncode(pair.Value)); } } string url = baseURL + path; if (encoded_params.Count > 0) { url += "?" + String.Join("&", encoded_params.ToArray()); } JsonNode result = new JsonObject { ["error"] = "Call to " + url + " failed with unknown reason" }; try { HttpResponseMessage response = await client.GetAsync(url); if (response.StatusCode == (System.Net.HttpStatusCode)200 || response.StatusCode == (System.Net.HttpStatusCode)422) { result = JsonNode.Parse(await response.Content.ReadAsStringAsync()); } else { result["error"] = "Call to " + url + " failed with error code " + ((int)response.StatusCode).ToString(); } } catch (HttpRequestException e) { result["error"] = "Call to " + url + " failed with reason: " + e.Message; } return result; } }