133 lines
4.3 KiB
C#
133 lines
4.3 KiB
C#
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
|
|
namespace PWAPPv2.Source.API
|
|
{
|
|
public class APIConnection
|
|
{
|
|
private static string BaseURL;
|
|
|
|
public DataObjects.APICredentials Credentials;
|
|
|
|
|
|
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"));
|
|
Client.DefaultRequestHeaders.Add("UserID", Credentials.UserID);
|
|
Client.DefaultRequestHeaders.Add("Password", Credentials.Password);
|
|
Client.DefaultRequestHeaders.Add("PracticeID", Credentials.PracticeId);
|
|
Client.DefaultRequestHeaders.Add("ApiID", Credentials.APIid);
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Dont use. Doesn't work, sillyhead.
|
|
/// </summary>
|
|
/// <param name="Call"></param>
|
|
public static async void APIGet(string Call)
|
|
{
|
|
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);
|
|
|
|
}
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
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 "";
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
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 "";
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
|
|
|
|
StringContent content = new StringContent(PostData, Encoding.UTF8, "application/json");
|
|
string conts = content.ToString();
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|