Lots of updates

This commit is contained in:
Matthew Burke
2023-07-26 22:26:54 -04:00
parent 42794b6284
commit 1b65e9bb83
24 changed files with 1305 additions and 28 deletions

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace PWAPPv2.Source
{
class APIConfig
{
public string PWUserID;
public string PWPassword;
public string PWPracticeID;
public string PWApiID;
public void LoadConfig(string path)
{
XmlDocument cfgDoc = new XmlDocument();
try
{
cfgDoc.Load(path);
}
catch(XmlException)
{
Console.WriteLine("An error has occured parsing the config file.");
return;
}
catch(System.IO.FileNotFoundException)
{
Console.WriteLine("Config file could not be found!");
return;
}
XmlNodeList XUserID = cfgDoc.GetElementsByTagName("pwuserid");
XmlNodeList XPassword = cfgDoc.GetElementsByTagName("pwpassword");
XmlNodeList XPracticeID = cfgDoc.GetElementsByTagName("pwpracticeid");
XmlNodeList XApiID = cfgDoc.GetElementsByTagName("pwapiid");
PWUserID = XUserID[0].InnerText;
PWPassword = XPassword[0].InnerText;
PWPracticeID = XPracticeID[0].InnerText;
PWApiID = XApiID[0].InnerText;
}
}
}

View File

@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.IO;
using Newtonsoft.Json;
namespace PWAPPv2.Source.API
{
class APIConnection
{
private static string BaseURL;
public DataObjects.APICredentials Credentials;
private static bool ResponseReady = false;
private static string Response = "";
public APIConnection(string baseUrl, DataObjects.APICredentials credentials)
{
Credentials = credentials;
BaseURL = baseUrl;
}
public string GetResponse()
{
while (!ResponseReady) ;
return Response;
}
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);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(Call);
}
}
public string APIPost()
{
string text;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://apipatientweb.azurewebsites.net/api/PWReferralTypes");
request.Method = "POST";
request.ContentType = "application/json; charset=\"utf-8\"";
request.Accept = "application/json";
string postData = Source.API.APIRequestBuilder.BuildJsonBodyRequest(Credentials);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
}
catch(WebException wex)
{
text = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
}
return text;
}
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;
if(response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
return "";
}
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;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
return "";
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;
namespace PWAPPv2.Source.API
{
class APIRequestBuilder
{
public static string BuildJsonBodyRequest(Object src)
{
return JsonConvert.SerializeObject(src);
}
}
}