This commit is contained in:
77
source/Plugins/AyaNova.Plugin.QBOI/GetToken.cs
Normal file
77
source/Plugins/AyaNova.Plugin.QBOI/GetToken.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace AyaNova.PlugIn.QBOI
|
||||
{
|
||||
public partial class GetToken : Form
|
||||
{
|
||||
static readonly HttpClient client = new HttpClient();
|
||||
string QBOI2_SESSION_TOKEN = "";
|
||||
|
||||
public string LastError { get; set; }
|
||||
public string FetchedToken { get; set; }
|
||||
|
||||
public GetToken(string sessionID)
|
||||
{
|
||||
InitializeComponent();
|
||||
QBOI2_SESSION_TOKEN = sessionID;
|
||||
LastError = string.Empty;
|
||||
FetchedToken = string.Empty;
|
||||
}
|
||||
|
||||
private async void GetToken_Load(object sender, EventArgs e)
|
||||
{
|
||||
while (string.IsNullOrEmpty(FetchedToken) && string.IsNullOrEmpty(LastError))
|
||||
{
|
||||
await TryFetchAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TryFetchAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await client.GetAsync("https://qboauth.ayanova.com/fetch/" + QBOI2_SESSION_TOKEN);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
//set the token and return
|
||||
FetchedToken = await response.Content.ReadAsStringAsync();
|
||||
this.DialogResult = DialogResult.OK;
|
||||
this.Close();
|
||||
}
|
||||
|
||||
if (response.StatusCode != System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
response.EnsureSuccessStatusCode();//throw exception, is unexpected result
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException err)
|
||||
{
|
||||
LastError = err.Message;
|
||||
this.DialogResult = DialogResult.Cancel;
|
||||
this.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void btnCancel_Click(object sender, EventArgs e)
|
||||
{
|
||||
LastError = "User selected CANCEL when attepting to fetch token";
|
||||
this.DialogResult = DialogResult.Cancel;
|
||||
this.Close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user