83 lines
3.3 KiB
C#
83 lines
3.3 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using RestClient.Models;
|
|
|
|
namespace RestClient
|
|
{
|
|
public class GetQ2PreCorrectionClient : BaseClient
|
|
{
|
|
public GetQ2PreCorrectionClient(string baseUrl)
|
|
: base(baseUrl)
|
|
{ }
|
|
|
|
public async Task GetToken(string name, string password, string urlLogin)
|
|
{
|
|
LoginUser user = new LoginUser { Name = name, Password = password };
|
|
var response = await PostWithReturnDataAsync(urlLogin, false, user).ConfigureAwait(false);
|
|
var tmp = JsonConvert.DeserializeObject<CustomJwt>(response);
|
|
if (tmp != null)
|
|
{
|
|
SetToken(tmp.access_token);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception(string.Format("No token loaded: {0}", ErrorMessage));
|
|
}
|
|
}
|
|
|
|
public async Task<Q2PreCorrection> GetQ2Correction(string relativeUrl)
|
|
{
|
|
ErrorMessage = String.Empty;
|
|
Uri url = GetUrl(relativeUrl);
|
|
if (url == null) return new Q2PreCorrection { AreDataCalculated = false, CorrLR = 0, CorrRL = 0, };
|
|
|
|
using (var client = GetClient())
|
|
{
|
|
try
|
|
{
|
|
var result = await client.GetAsync(url).ConfigureAwait(false);
|
|
string jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
if (result.IsSuccessStatusCode)
|
|
{
|
|
Q2PreCorrection retVal = new Q2PreCorrection();
|
|
|
|
string pattern = "\"areDataCalculated\":";
|
|
int position = jsonResult.IndexOf(pattern) + pattern.Length;
|
|
int length = jsonResult.Substring(position).IndexOfAny(new char[] { ',', '}', '\r', '\n' });
|
|
retVal.AreDataCalculated = bool.Parse(jsonResult.Substring(position, length));
|
|
|
|
pattern = "\"right\":";
|
|
position = jsonResult.IndexOf(pattern) + pattern.Length;
|
|
length = jsonResult.Substring(position).IndexOfAny(new char[] { ',', '}', '\r', '\n' });
|
|
retVal.CorrLR = int.Parse(jsonResult.Substring(position, length));
|
|
|
|
pattern = "\"left\":";
|
|
position = jsonResult.IndexOf(pattern) + pattern.Length;
|
|
length = jsonResult.Substring(position).IndexOfAny(new char[] { ',', '}', '\r', '\n' });
|
|
retVal.CorrRL = int.Parse(jsonResult.Substring(position, length));
|
|
|
|
return retVal;
|
|
}
|
|
else
|
|
{
|
|
return new Q2PreCorrection { AreDataCalculated = false, CorrLR = 0, CorrRL = 0, };
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ex.Message;
|
|
if (ex.InnerException != null)
|
|
{
|
|
Message += ". " + ex.InnerException.Message;
|
|
}
|
|
Trace.TraceError(Message);
|
|
ErrorMessage = Message;
|
|
return new Q2PreCorrection { AreDataCalculated = false, CorrLR = 0, CorrRL = 0, };
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|