This commit is contained in:
@@ -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<string>();
|
||||
//Must be *the* SuperUser to continue:
|
||||
a = await GetAsync("user/amsu");
|
||||
var IsSuperUser = a.ObjectResponse["data"].Value<bool>();
|
||||
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<string>();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -207,7 +212,7 @@ namespace AyaNovaQBI
|
||||
}
|
||||
|
||||
|
||||
private async static Task<ApiResponse> TryPostAsync(string route, string postJson = null)
|
||||
internal async static Task<ApiResponse> TryPostAsync(string route, string postJson = null)
|
||||
{
|
||||
|
||||
var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + route);
|
||||
@@ -244,9 +249,30 @@ namespace AyaNovaQBI
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="jsonString"></param>
|
||||
/// <returns></returns>
|
||||
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<long>();
|
||||
}
|
||||
|
||||
public static uint CTokenFromResponse(ApiResponse a)
|
||||
{
|
||||
return a.ObjectResponse["data"]["concurrency"].Value<uint>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user