193 lines
5.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
class Program
{
static SignLanguageReportApiClient api;
static void Main(string[] args)
{
Console.Title = "SL REPORT API TEST HARNESS";
try
{
api = new SignLanguageReportApiClient("https://localhost:44367/");
LogHeader("SL REPORT TEST STARTED");
var userId = RegisterUser();
if (userId == Guid.Empty)
return;
RegisterClient();
MapUser(userId);
CaptureSessions();
ReportAllHistory();
ReportFilteredHistory();
ListAllClients();
LogHeader("SL REPORT TEST COMPLETED");
}
catch (Exception ex)
{
LogFatal("UNHANDLED FATAL ERROR", ex);
}
Console.WriteLine("\nPress ENTER to exit...");
Console.ReadLine();
}
static Guid RegisterUser()
{
return SafeCall(
() => api.RegisterUser("test_user3", "123456"),
"RegisterUser"
);
}
static void RegisterClient()
{
SafeCall(
() => api.RegisterClient("13351", "MORO"),
"RegisterClient"
);
}
static void MapUser(Guid userId)
{
SafeCall(
() => api.MapUser(userId, "13351", "MORO"),
"MapUser"
);
}
static void CaptureSessions()
{
SafeCall(() => api.Capture("13351", "MORO"), "Capture #1");
SafeCall(() => api.Capture("13351", "MORO"), "Capture #2");
}
static void ReportAllHistory()
{
var history = SafeCall(
() => api.ReportAllClientHistory("13351", "MORO"),
"ReportAllClientHistory"
);
if (history == null) return;
LogInfo($"History records: {history.Count}");
foreach (var h in history)
LogInfo($"{h.captDate:yyyy-MM-dd HH:mm:ss} , Sessions: {h.NoOfSessions}");
}
static void ReportFilteredHistory()
{
var from = DateTime.Today.AddDays(-7);
var to = DateTime.Today;
var history = SafeCall(
() => api.ReportClientHistory("13351", "MORO", from, to),
"ReportClientHistory"
);
if (history != null)
LogInfo($"Filtered records: {history.Count}");
foreach (var h in history)
LogInfo($"{h.captDate:yyyy-MM-dd HH:mm:ss} , Sessions: {h.NoOfSessions}");
}
static void ListAllClients()
{
var clients = SafeCall(
() => api.ListAllClients(),
"ListAllClients"
);
if (clients != null)
LogInfo($"Total clients: {clients.Count}");
foreach (var c in clients)
LogInfo($"cid: {c.cid}, srv: {c.srv}, addingDate: {c.addingDate}, lastCaptDate: {c.lastCaptDate}, NoOfCapt: {c.NoOfCapt}");
}
static T SafeCall<T>(Func<T> action, string stepName)
{
var sw = Stopwatch.StartNew();
LogStep(stepName);
try
{
var result = action();
sw.Stop();
LogSuccess($"{stepName} completed in {sw.ElapsedMilliseconds} ms");
return result;
}
catch (WebException wex)
{
sw.Stop();
LogError(stepName, "NETWORK / HTTP ERROR", wex);
}
catch (Exception ex)
{
sw.Stop();
LogError(stepName, "APPLICATION ERROR", ex);
}
return default;
}
static void LogHeader(string msg)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\n======================================");
Console.WriteLine(msg);
Console.WriteLine("======================================\n");
Console.ResetColor();
}
static void LogStep(string msg)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"> {msg}");
Console.ResetColor();
}
static void LogSuccess(string msg)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"✔ {msg}\n");
Console.ResetColor();
}
static void LogInfo(string msg)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($" {msg}");
Console.ResetColor();
}
static void LogError(string step, string type, Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"✖ {step} FAILED [{type}]");
Console.WriteLine($" Message: {ex.Message}");
if (ex.InnerException != null)
Console.WriteLine($" Inner: {ex.InnerException.Message}");
Console.ResetColor();
Console.WriteLine();
}
static void LogFatal(string msg, Exception ex)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("!!! FATAL ERROR !!!");
Console.WriteLine(msg);
if (ex != null)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
Console.ResetColor();
}
}