Lots of updates
This commit is contained in:
128
PWAPPv2/Source/API/APIConnection.cs
Normal file
128
PWAPPv2/Source/API/APIConnection.cs
Normal 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 "";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user