Refactor to api connections. Update Checker.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
@@ -13,34 +15,28 @@ namespace PWAPPv2.Source.API
|
||||
|
||||
public DataObjects.APICredentials Credentials;
|
||||
|
||||
private static bool ResponseReady = false;
|
||||
private static string Response = "";
|
||||
|
||||
HttpClient Client;
|
||||
|
||||
public APIConnection(string baseUrl, DataObjects.APICredentials credentials)
|
||||
{
|
||||
Credentials = credentials;
|
||||
BaseURL = baseUrl;
|
||||
|
||||
Client = new HttpClient();
|
||||
Client.BaseAddress = new Uri(BaseURL);
|
||||
Client.DefaultRequestHeaders.Accept.Add(
|
||||
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
}
|
||||
|
||||
public string GetResponse()
|
||||
{
|
||||
while (!ResponseReady) ;
|
||||
return Response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dont use. Doesn't work, sillyhead.
|
||||
/// </summary>
|
||||
/// <param name="Call"></param>
|
||||
public static async void APIGet(string Call)
|
||||
{
|
||||
//WebRequest request = WebRequest.Create(BaseURL + Call);
|
||||
//request.Method = "GET";
|
||||
//request.ContentType = "application/json; charset=utf-8";
|
||||
//var response = (HttpWebResponse)request.GetResponse();
|
||||
//string text;
|
||||
//using (var sr = new StreamReader(response.GetResponseStream()))
|
||||
//{
|
||||
// text = sr.ReadToEnd();
|
||||
//}
|
||||
//return text;
|
||||
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
client.BaseAddress = new Uri(BaseURL);
|
||||
@@ -52,16 +48,16 @@ namespace PWAPPv2.Source.API
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a post requst without data. Essentially an advanced get since I was a retard when I wrote the original API.
|
||||
/// </summary>
|
||||
/// <param name="apiUri"></param>
|
||||
/// <returns></returns>
|
||||
public string SendPostRequestAsync(string apiUri)
|
||||
{
|
||||
HttpClient client = new HttpClient();
|
||||
client.BaseAddress = new Uri(BaseURL);
|
||||
client.DefaultRequestHeaders.Accept.Add(
|
||||
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
StringContent contetnt = new StringContent(Credentials.ToJsonString(), Encoding.UTF8, "application/json");
|
||||
|
||||
var response = client.PostAsync(apiUri, contetnt).Result;
|
||||
var response = Client.PostAsync(apiUri, contetnt).Result;
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
@@ -69,16 +65,18 @@ namespace PWAPPv2.Source.API
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a post request with data
|
||||
/// </summary>
|
||||
/// <param name="apiUri"></param>
|
||||
/// <param name="PostData"></param>
|
||||
/// <returns></returns>
|
||||
public string SendPostRequestAsync(string apiUri, string PostData)
|
||||
{
|
||||
HttpClient client = new HttpClient();
|
||||
client.BaseAddress = new Uri(BaseURL);
|
||||
client.DefaultRequestHeaders.Accept.Add(
|
||||
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
StringContent contetnt = new StringContent(PostData, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = client.PostAsync(apiUri, contetnt).Result;
|
||||
var response = Client.PostAsync(apiUri, contetnt).Result;
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
@@ -87,5 +85,46 @@ namespace PWAPPv2.Source.API
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a post request with the API credengials in the header of the request.
|
||||
/// </summary>
|
||||
/// <param name="apiUri"></param>
|
||||
/// <param name="PostData"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="RequestFailedExcpetion"></exception>
|
||||
public string SendPostWithCredsInHeader(string apiUri, string PostData)
|
||||
{
|
||||
Client.DefaultRequestHeaders.Add("UserID", Credentials.UserID);
|
||||
Client.DefaultRequestHeaders.Add("Password", Credentials.Password);
|
||||
Client.DefaultRequestHeaders.Add("PracticeID", Credentials.PracticeId);
|
||||
Client.DefaultRequestHeaders.Add("ApiID", Credentials.APIid);
|
||||
|
||||
StringContent content = new StringContent(PostData, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = Client.PostAsync(apiUri, content).Result;
|
||||
|
||||
Client.DefaultRequestHeaders.Remove("UserID");
|
||||
Client.DefaultRequestHeaders.Remove("Password");
|
||||
Client.DefaultRequestHeaders.Remove("PraticeID");
|
||||
Client.DefaultRequestHeaders.Remove("ApiID");
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
|
||||
return response.Content.ReadAsStringAsync().Result;
|
||||
}
|
||||
throw new RequestFailedExcpetion(response.StatusCode.ToString());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class RequestFailedExcpetion : Exception
|
||||
{
|
||||
public string RequestError;
|
||||
public RequestFailedExcpetion(string errorCode)
|
||||
{
|
||||
RequestError = "Server responded with error code: " + errorCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user