Refactor to api connections. Update Checker.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using PWAPPv2.Source.Config;
|
||||
using System;
|
||||
using System.Xml;
|
||||
|
||||
namespace PWAPPv2.Source
|
||||
@@ -10,6 +11,25 @@ namespace PWAPPv2.Source
|
||||
public string PWPracticeID;
|
||||
public string PWApiID;
|
||||
|
||||
public string PWBaseURI;
|
||||
public string PWUpdateURI;
|
||||
public string PWAppVersion;
|
||||
|
||||
public APIConfig() { }
|
||||
|
||||
public APIConfig(Config.Configuration practiceConfig, Config.Configuration universalConfig)
|
||||
{
|
||||
PWUserID = practiceConfig.Get("pwuserid");
|
||||
PWPassword = practiceConfig.Get("pwpassword");
|
||||
PWPracticeID = practiceConfig.Get("pwpracticeid");
|
||||
PWApiID = practiceConfig.Get("pwapiid");
|
||||
|
||||
PWBaseURI = universalConfig.Get("PWBaseURI");
|
||||
PWUpdateURI = universalConfig.Get("PWUpdateURI");
|
||||
PWAppVersion = universalConfig.Get("PWAppVersion");
|
||||
|
||||
}
|
||||
|
||||
public void LoadConfig(string path)
|
||||
{
|
||||
XmlDocument cfgDoc = new XmlDocument();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
50
PWAPPv2/Source/API/PWApiConnection.cs
Normal file
50
PWAPPv2/Source/API/PWApiConnection.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using PWAPPv2.Source.DataObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PWAPPv2.Source.API
|
||||
{
|
||||
public class PWApiConnection
|
||||
{
|
||||
private APIConnection BaseConnection;
|
||||
private APIConnection UpdateConnection;
|
||||
|
||||
private APICredentials Credentials;
|
||||
private string AppVersion;
|
||||
|
||||
public PWApiConnection(Source.Config.Configuration practiceConfig, Source.Config.Configuration universalConfig)
|
||||
{
|
||||
Credentials = new APICredentials(practiceConfig);
|
||||
BaseConnection = new APIConnection(universalConfig.Get("PWBaseURI"), Credentials);
|
||||
UpdateConnection = new APIConnection(universalConfig.Get("PWUpdateURI"), Credentials);
|
||||
|
||||
AppVersion = universalConfig.Get("PWAppVersion");
|
||||
|
||||
}
|
||||
|
||||
public string PostToApi(string uri, string data)
|
||||
{
|
||||
return BaseConnection.SendPostRequestAsync(uri, data);
|
||||
}
|
||||
|
||||
public string GetFromApi(string uri)
|
||||
{
|
||||
return BaseConnection.SendPostRequestAsync(uri);
|
||||
}
|
||||
|
||||
public bool CheckForUpdate()
|
||||
{
|
||||
string currentVersion = UpdateConnection.SendPostWithCredsInHeader("", "");
|
||||
currentVersion = currentVersion.Replace("\"", "");
|
||||
if(currentVersion != AppVersion)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user