This commit is contained in:
@@ -28,14 +28,24 @@ namespace AyaNova.PlugIn.V8
|
|||||||
MessageBox.Show("Server could not be reached at that URL\n" + result);
|
MessageBox.Show("Server could not be reached at that URL\n" + result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void btnOk_Click(object sender, EventArgs e)
|
private async void btnOk_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
var res = await util.AuthenticateAsync(edUserName.Text,edPassword.Text);
|
||||||
|
if (!res)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Login failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.DialogResult = DialogResult.OK;
|
||||||
|
this.Close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void btnCancel_Click(object sender, EventArgs e)
|
private void btnCancel_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
|
||||||
|
this.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Auth_Load(object sender, EventArgs e)
|
private void Auth_Load(object sender, EventArgs e)
|
||||||
|
|||||||
@@ -132,7 +132,14 @@ namespace AyaNova.PlugIn.V8
|
|||||||
}
|
}
|
||||||
|
|
||||||
Auth d = new Auth();
|
Auth d = new Auth();
|
||||||
d.ShowDialog();
|
var res = d.ShowDialog(); ;
|
||||||
|
if (res == DialogResult.Cancel)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//here because we logged in fine and can proceed
|
||||||
|
|
||||||
|
MessageBox.Show("Login successful! JWT is " + util.JWT);
|
||||||
//Only one command
|
//Only one command
|
||||||
// DumpIt();
|
// DumpIt();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System.Net;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
|
||||||
namespace AyaNova.PlugIn.V8
|
namespace AyaNova.PlugIn.V8
|
||||||
@@ -15,9 +16,28 @@ namespace AyaNova.PlugIn.V8
|
|||||||
const string TEST_ROUTE = "ServerInfo";
|
const string TEST_ROUTE = "ServerInfo";
|
||||||
const string API_BASE_ROUTE = "api/v8/";
|
const string API_BASE_ROUTE = "api/v8/";
|
||||||
static HttpClient client = new HttpClient();
|
static HttpClient client = new HttpClient();
|
||||||
|
//url once known to be good
|
||||||
static string ApiBaseUrl { get; set; }
|
static string ApiBaseUrl { get; set; }
|
||||||
|
internal static string JWT { get; set; }
|
||||||
|
static bool Initialized { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public util()
|
||||||
|
{
|
||||||
|
Initialized = false;
|
||||||
|
JWT = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void InitClient()
|
||||||
|
{
|
||||||
|
if (Initialized) return;
|
||||||
|
client.BaseAddress = new Uri(ApiBaseUrl);
|
||||||
|
client.DefaultRequestHeaders.Accept.Clear();
|
||||||
|
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
|
Initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Only a return value of "OK" is ok
|
/// Only a return value of "OK" is ok
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -27,21 +47,22 @@ namespace AyaNova.PlugIn.V8
|
|||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(serverUrl)) return "Server url required";
|
if (string.IsNullOrEmpty(serverUrl)) return "Server url required";
|
||||||
|
|
||||||
if (!serverUrl.Contains("/api/")) {
|
if (!serverUrl.Contains("/api/"))
|
||||||
if (!serverUrl.EndsWith("/")) serverUrl+="/";
|
{
|
||||||
|
if (!serverUrl.EndsWith("/")) serverUrl += "/";
|
||||||
serverUrl += API_BASE_ROUTE;
|
serverUrl += API_BASE_ROUTE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!serverUrl.StartsWith("http")) serverUrl = "http://" + serverUrl;
|
if (!serverUrl.StartsWith("http")) serverUrl = "http://" + serverUrl;
|
||||||
//client.BaseAddress = new Uri("http://localhost:64195/");
|
|
||||||
//client.DefaultRequestHeaders.Accept.Clear();
|
|
||||||
//client.DefaultRequestHeaders.Accept.Add(
|
|
||||||
// new MediaTypeWithQualityHeaderValue("application/json"));
|
|
||||||
|
|
||||||
//try to connect, ping the server api
|
//try to connect, ping the server api
|
||||||
client.BaseAddress = new Uri(serverUrl);
|
if (!Initialized)
|
||||||
client.DefaultRequestHeaders.Accept.Clear();
|
{
|
||||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
client.BaseAddress = new Uri(serverUrl);
|
||||||
|
client.DefaultRequestHeaders.Accept.Clear();
|
||||||
|
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
|
Initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -49,7 +70,12 @@ namespace AyaNova.PlugIn.V8
|
|||||||
if (response.IsSuccessStatusCode)
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
var ret = await response.Content.ReadAsStringAsync();
|
var ret = await response.Content.ReadAsStringAsync();
|
||||||
if (ret.Contains("AyaNova")) return "OK";
|
if (ret.Contains("AyaNova"))
|
||||||
|
{
|
||||||
|
ApiBaseUrl = serverUrl;
|
||||||
|
return "OK";
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -62,7 +88,100 @@ namespace AyaNova.PlugIn.V8
|
|||||||
return "failed";
|
return "failed";
|
||||||
}
|
}
|
||||||
|
|
||||||
//eoc
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public async static Task<bool> AuthenticateAsync(string login, string password = null)
|
||||||
|
{
|
||||||
|
InitClient();
|
||||||
|
|
||||||
|
if (password == null)
|
||||||
|
password = login;
|
||||||
|
|
||||||
|
dynamic creds = new JObject();
|
||||||
|
creds.login = login;
|
||||||
|
creds.password = password;
|
||||||
|
|
||||||
|
ApiResponse a = await PostAsync("Auth", creds.ToString());
|
||||||
|
|
||||||
|
if (a.HttpResponse.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
JWT=a.ObjectResponse["data"]["token"].Value<string>();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public async static Task<ApiResponse> GetAsync(string route, string authToken = null, string bodyJsonData = null)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
var requestMessage = new HttpRequestMessage(HttpMethod.Get, route);
|
||||||
|
if (!string.IsNullOrWhiteSpace(JWT))
|
||||||
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(bodyJsonData))
|
||||||
|
requestMessage.Content = new StringContent(bodyJsonData, System.Text.Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
HttpResponseMessage response = await client.SendAsync(requestMessage);
|
||||||
|
var responseAsString = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
|
||||||
|
}
|
||||||
|
|
||||||
|
public async static Task<ApiResponse> PostAsync(string route, string postJson = null)
|
||||||
|
{
|
||||||
|
|
||||||
|
var requestMessage = new HttpRequestMessage(HttpMethod.Post, route);
|
||||||
|
if (!string.IsNullOrWhiteSpace(JWT))
|
||||||
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(postJson))
|
||||||
|
requestMessage.Content = new StringContent(postJson, System.Text.Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
HttpResponseMessage response = await client.SendAsync(requestMessage);
|
||||||
|
var responseAsString = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//eoc
|
||||||
|
public class ApiResponse
|
||||||
|
{
|
||||||
|
public HttpResponseMessage HttpResponse { get; set; }
|
||||||
|
public JObject ObjectResponse { get; set; }
|
||||||
|
public string CompactResponse
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return ObjectResponse.ToString(Newtonsoft.Json.Formatting.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="jsonString"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static JObject Parse(string jsonString)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(jsonString))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return JObject.Parse(jsonString);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}//eoc
|
||||||
|
|
||||||
|
|
||||||
}//ens
|
}//ens
|
||||||
|
|||||||
Reference in New Issue
Block a user