Added Poll response function

JSON is not properly serialised... still in progress
This commit is contained in:
2021-12-28 21:53:31 +00:00
parent e134751745
commit 5a2f109e65
5 changed files with 107 additions and 25 deletions

View File

@ -12,7 +12,7 @@ namespace EFB.Controllers.API
{
private HttpClient HttpClient { get; set; }
public async Task<ResponseModel> Get<T>(string Endpoint, Dictionary<string, string> Headers){
public async Task<ResponseModel<T>> Get<T>(string Endpoint, Dictionary<string, string> Headers){
this.HttpClient = new HttpClient();
@ -36,32 +36,31 @@ namespace EFB.Controllers.API
string resultString = result.Content.ReadAsStringAsync().Result;
object response;
//Assess return value (object or raw string)
try{
object response = resultString;
if (typeof(T) != typeof(string))
{//If the user requests string for return type
response = JsonConvert.DeserializeObject<T>(resultString);
}catch {
response = resultString;
}
return new ResponseModel{
//Sender should be aware of type T becuase of Generic function
Result = response
return new ResponseModel<T>(){
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
}
catch (System.Exception e)
{
return new ResponseModel{Error = e.Message};
return new ResponseModel<T>{Error = e.Message};
}
}
//Returned in the event No other response has been returned
return new ResponseModel{Error = "Invalid Endpoint - Please try again later"};
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
}
public async Task<ResponseModel> Post<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
public async Task<ResponseModel<T>> Post<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
@ -88,16 +87,58 @@ namespace EFB.Controllers.API
response = JsonConvert.DeserializeObject<T>(resultString);
}
return new ResponseModel(){
//Sender should be aware of type T becuase of Generic function
Result = response
return new ResponseModel<T>(){
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
}catch(System.Exception e){
return new ResponseModel{Error = e.Message};
return new ResponseModel<T>{Error = e.Message};
}
}
//Returned in the event No other response has been returned
return new ResponseModel{Error = "Invalid Endpoint - Please try again later"};
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
}
public async Task<ResponseModel<T>> Put<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
if (Headers != null)
{
foreach (var Header in Headers)
{
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
}
}
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
{
try{//Try statement to catch errors in the process of making the request
var pendingResult = this.HttpClient.PutAsync(Endpoint, Body);
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
object response = resultString;
if (typeof(T) != typeof(string))
{//If the user requests string for return type
response = JsonConvert.DeserializeObject<T>(resultString);
}
return new ResponseModel<T>(){
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
}catch(System.Exception e){
return new ResponseModel<T>{Error = e.Message};
}
}
//Returned in the event No other response has been returned
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
}