This commit is contained in:
62
source/Plugins/AyaNova.Plugin.V8/util.cs
Normal file
62
source/Plugins/AyaNova.Plugin.V8/util.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace AyaNova.PlugIn.V8
|
||||
{
|
||||
class util
|
||||
{
|
||||
const string TEST_ROUTE = "ServerInfo";
|
||||
const string API_BASE_ROUTE = "api/v8/";
|
||||
static HttpClient client = new HttpClient();
|
||||
static string ApiBaseUrl { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Only a return value of "OK" is ok
|
||||
/// </summary>
|
||||
/// <param name="serverUrl"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> TestUrlAsync(string serverUrl)
|
||||
{
|
||||
if (string.IsNullOrEmpty(serverUrl)) return "Server url required";
|
||||
|
||||
if (!serverUrl.Contains("/api/")) {
|
||||
if (!serverUrl.EndsWith("/")) serverUrl+="/";
|
||||
serverUrl += API_BASE_ROUTE;
|
||||
}
|
||||
|
||||
//try to connect, ping the server api
|
||||
client.BaseAddress = new Uri(serverUrl);
|
||||
client.DefaultRequestHeaders.Accept.Clear();
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await client.GetAsync(serverUrl + TEST_ROUTE);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var ret = await response.Content.ReadAsStringAsync();
|
||||
if (ret.Contains("AyaNova")) return "OK";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "Failed: " + response.StatusCode.ToString();
|
||||
}
|
||||
}
|
||||
catch { return "Failed"; }
|
||||
|
||||
|
||||
return "failed";
|
||||
}
|
||||
|
||||
//eoc
|
||||
|
||||
}
|
||||
}//ens
|
||||
Reference in New Issue
Block a user