From 4fb17a37786a83a7c23283d917075b5cca602952 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 20 Jun 2022 01:01:38 +0000 Subject: [PATCH] --- AyaNovaQBI/auth.cs | 2 +- AyaNovaQBI/util.cs | 62 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/AyaNovaQBI/auth.cs b/AyaNovaQBI/auth.cs index 65a4bfd..e035ea6 100644 --- a/AyaNovaQBI/auth.cs +++ b/AyaNovaQBI/auth.cs @@ -55,7 +55,7 @@ namespace AyaNovaQBI var res = await util.AuthenticateAsync(edUserName.Text, edPassword.Text); if (!res) { - MessageBox.Show("AyaNova 8 SuperUser account login failed"); + MessageBox.Show("Login failed"); btnLogin.Enabled = btnTest.Enabled = true; return; } diff --git a/AyaNovaQBI/util.cs b/AyaNovaQBI/util.cs index c50cfbc..47d5c9e 100644 --- a/AyaNovaQBI/util.cs +++ b/AyaNovaQBI/util.cs @@ -79,26 +79,31 @@ namespace AyaNovaQBI dynamic creds = new JObject(); creds.login = login; creds.password = password; - - ApiResponse a = await PostAsync("auth", creds.ToString()); - - if (a.HttpResponse.IsSuccessStatusCode) + var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + "auth"); + requestMessage.Content = new StringContent(creds.ToString(), System.Text.Encoding.UTF8, "application/json"); + HttpResponseMessage response = null; + try { - JWT = a.ObjectResponse["data"]["token"].Value(); - //Must be *the* SuperUser to continue: - a = await GetAsync("user/amsu"); - var IsSuperUser = a.ObjectResponse["data"].Value(); - if (!IsSuperUser) - { - JWT = string.Empty; - return false; - } - - - - return true; + response = await client.SendAsync(requestMessage); + } + catch (HttpRequestException ex) + { + var Err = ex.Message; + var InnerErr = ""; + if (ex.InnerException != null) + InnerErr = ex.InnerException.Message; + return false; + // throw new Exception("POST error, route: " + route + "\r\nError:" + Err + "\r\nInner error:" + InnerErr + "\r\nStack:" + ex.StackTrace + "\r\nPOSTED OBJECT:\r\n" + postJson); } + //ApiResponse a = await PostAsync("auth", creds.ToString()); + + if (response.IsSuccessStatusCode) + { + var a= new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(await response.Content.ReadAsStringAsync()) }; + JWT = a.ObjectResponse["data"]["token"].Value(); + return true; + } return false; } @@ -207,7 +212,7 @@ namespace AyaNovaQBI } - private async static Task TryPostAsync(string route, string postJson = null) + internal async static Task TryPostAsync(string route, string postJson = null) { var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + route); @@ -244,9 +249,30 @@ namespace AyaNovaQBI + /// + /// + /// + /// + /// + public static JObject Parse(string jsonString) + { + if (string.IsNullOrWhiteSpace(jsonString)) + { + return null; + } + return JObject.Parse(jsonString); + } + public static long IdFromResponse(ApiResponse a) + { + return a.ObjectResponse["data"]["id"].Value(); + } + public static uint CTokenFromResponse(ApiResponse a) + { + return a.ObjectResponse["data"]["concurrency"].Value(); + }