From 3f5a5d109af6fba6c9846358340fb1eced084003 Mon Sep 17 00:00:00 2001 From: Taylor Howell Date: Sun, 26 Nov 2023 23:53:22 -0600 Subject: [PATCH] service starting to process function sync requests properly --- DiscoverFunctionsHandler.cs | 6 ++--- Program.cs | 21 +++++++++-------- State Machine/FunctionDefinition.cs | 36 +++++++++++++++++++++++++++++ State Machine/FunctionLibrary.cs | 13 +++++------ State Machine/ReferenceTuple.cs | 2 +- ValkyrieServerController.cs | 31 ++++++++++++++++--------- 6 files changed, 78 insertions(+), 31 deletions(-) diff --git a/DiscoverFunctionsHandler.cs b/DiscoverFunctionsHandler.cs index 87f10c6..a46ec64 100644 --- a/DiscoverFunctionsHandler.cs +++ b/DiscoverFunctionsHandler.cs @@ -14,8 +14,8 @@ public class DiscoverFunctionsHandler /// returns the json string of the functions public static string GetFunctionDefinitionsJSON() => JsonSerializer.Serialize( new FunctionLibrary().ImportedFunctions - .Select(x => new FunctionListItem(x.Key, x.Value.Description, x.Value.ExpectedParameters - .Select(x => x.Value) - .ToArray()))); + .Select(x => + new FunctionListItem(x.Key, x.Value.Description, x.Value.ExpectedParametersList)) + .ToArray()); } } diff --git a/Program.cs b/Program.cs index 020fecd..f8c13b0 100644 --- a/Program.cs +++ b/Program.cs @@ -58,8 +58,6 @@ app.MapGet("/api/v1/sync", async (HttpContext ctx) => { - ctx.RequestAborted.ThrowIfCancellationRequested(); - string instructionId = ctx.Request.Headers["id"]; string key = ctx.Request.Headers["apikey"]; @@ -83,23 +81,27 @@ return JsonSerializer.Serialize(new message("No instruction ID provided")); } - ctx.Response.Headers.Add("Content-Type", "application/json"); - + Debug.WriteLine("test 1"); + ValkyrieServerController valkyrieServerController = new ValkyrieServerController() { ValkyrieAPIKey = valkApiKey }; + + Debug.WriteLine("test 2"); + var res = await valkyrieServerController.UpdateInstructionFunctionDefinitions(instructionId); - if (res.statusCode == "200") + if (res.statusCode.ToLower() == "ok") { ctx.Response.StatusCode = 200; - return JsonSerializer.Serialize(new message("Syncing")); + return JsonSerializer.Serialize(new message("Synced!")); } - ctx.Response.StatusCode = int.Parse(res.statusCode); - return JsonSerializer.Serialize(new message(res.response)); + ctx.Response.StatusCode = 500; + + return JsonSerializer.Serialize(new errResult(res.response, res.statusCode)); }); @@ -239,9 +241,10 @@ /// The name of the function /// The description of the function /// the parameters of the function -internal record FunctionListItem(string Name, string Description, ReferenceTuple[] Parameters); +internal record FunctionListItem(string Name, string Description, ExpectedParameter[] Parameters); internal record message(string content); +internal record errResult(string error, string reasonPhrase); diff --git a/State Machine/FunctionDefinition.cs b/State Machine/FunctionDefinition.cs index b298051..6d3e333 100644 --- a/State Machine/FunctionDefinition.cs +++ b/State Machine/FunctionDefinition.cs @@ -36,6 +36,24 @@ public abstract class FunctionDefinition /// public Dictionary ExpectedParameters { get; set; } = new Dictionary(); + /// + /// Get the list of expected parameters + /// + public ExpectedParameter[] ExpectedParametersList + { + get + { + List parameters = new List(); + + foreach (var x in ExpectedParameters) + { + parameters.Add(new ExpectedParameter(x.Key, x.Value.type.ToString())); + } + + return parameters.ToArray(); + } + } + /// /// The dictionary of parameters /// @@ -109,4 +127,22 @@ public bool TryGetVariableType(string name, out StateMachineVariableType result) return false; } } + + /// + /// A struct representing an expected parameter + /// + public struct ExpectedParameter + { + public string Name { get; init; } + public string Type { get; init; } + + public string Description { get; init; } + + public ExpectedParameter(string name, string type, string description = "") + { + Name = name; + Type = type; + Description = description; + } + } } \ No newline at end of file diff --git a/State Machine/FunctionLibrary.cs b/State Machine/FunctionLibrary.cs index 9659198..638f142 100644 --- a/State Machine/FunctionLibrary.cs +++ b/State Machine/FunctionLibrary.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; +using System.Security.Cryptography; using System.Threading.Tasks; @@ -20,16 +22,13 @@ public class FunctionLibrary public Dictionary ImportedFunctions = new Dictionary(); /// - /// Default constructor. Imports all functions. + /// Default constructor. Builds all functions. /// - public FunctionLibrary() - { - ImportFunctions(); - } + public FunctionLibrary() => BuildFunctionLibrary(); - public void ImportFunctions() + public void BuildFunctionLibrary() { - Console.WriteLine("\t>Gathering Functions...\n"); + //Debug.WriteLine("\t>Gathering Functions...\n"); ContinueFunction continueFunction = new ContinueFunction(); ExitQuestionFunction exitQuestion = new ExitQuestionFunction(); GreetUserFunction greetFunction = new GreetUserFunction(); diff --git a/State Machine/ReferenceTuple.cs b/State Machine/ReferenceTuple.cs index 56df3c3..4424aa1 100644 --- a/State Machine/ReferenceTuple.cs +++ b/State Machine/ReferenceTuple.cs @@ -14,7 +14,7 @@ public class ReferenceTuple public StateMachineVariableType type { get; set; } public bool applied { get; set; } - public ReferenceTuple(StateMachineVariableType type, bool applied) + public ReferenceTuple(StateMachineVariableType type, bool applied, string description = "") { this.type = type; this.applied = applied; diff --git a/ValkyrieServerController.cs b/ValkyrieServerController.cs index 6d1b3a4..7eba034 100644 --- a/ValkyrieServerController.cs +++ b/ValkyrieServerController.cs @@ -12,15 +12,22 @@ namespace Valkyrie_Server public class ValkyrieServerController { - public static readonly Uri productionBaseUri = new Uri("https://valkyrie-nu.vercel.app/api/v1/getdata/", UriKind.Absolute); - public static readonly Uri testBaseUri = new Uri("http://localhost:3000/api/v1/getdata/", UriKind.Absolute); + public static readonly Uri prodGetDataURI = new Uri("https://valkyrie-nu.vercel.app/api/v1/getdata/", UriKind.Absolute); + public static readonly Uri testGetDataURI = new Uri("http://localhost:3000/api/v1/getdata/", UriKind.Absolute); - public static readonly Uri testURI = new Uri("http://localhost:5000/api/sm/guess", UriKind.Absolute); + public static readonly Uri testSyncFunctionsURI = new Uri("http://localhost:3000/api/v1/sync-functions", UriKind.Absolute); + + public static readonly Uri testAPIURIThatICameBackFromThanksgivingAndCantRememberWhatItIsFor = new Uri("http://localhost:3000/api/sm/guess", UriKind.Absolute); /// /// the base URI for the Valkyrie server /// - private Uri selectedURI = testURI; + private Uri selectedURI = testGetDataURI; + + public ValkyrieServerController() + { + + } /// /// The API key for the Valkyrie server @@ -79,23 +86,25 @@ public class ValkyrieServerController /// /// the instruction ID /// the result of the update - public async Task<(string response, string statusCode)> UpdateInstructionFunctionDefinitions(string instructionId) + public async Task<(string response, bool success, string statusCode)> UpdateInstructionFunctionDefinitions(string instructionId) { if (string.IsNullOrEmpty(instructionId)) { - return ("Error: No instruction ID provided", "0"); + return ("Error: No instruction ID provided", false, "0"); } - using HttpClient client = new(); var functionJSON = DiscoverFunctionsHandler.GetFunctionDefinitionsJSON(); + Debug.WriteLine(functionJSON); + + using HttpClient client = new(); HttpContent content = new StringContent(functionJSON); - var response = await client.PostAsync(selectedURI, content); client.DefaultRequestHeaders.Add("x-api-key", ValkyrieAPIKey); client.DefaultRequestHeaders.Add("x-instruction-id", instructionId); + var response = await client.PostAsync(testSyncFunctionsURI, content); using StreamReader reader = new StreamReader(response.Content.ReadAsStream()); @@ -103,15 +112,15 @@ public class ValkyrieServerController if (!response.IsSuccessStatusCode) { - return ("Error: " + response.StatusCode + "\n" + responseString, response.StatusCode.ToString()); + return ("Error: " + response.StatusCode + "\n" + responseString, false, response.StatusCode.ToString()); } if (string.IsNullOrEmpty(responseString)) { - return ("Error: Empty response from server", "0"); + return ("Error: Empty response from server", false, "0"); } - return (responseString, response.StatusCode.ToString()); + return (responseString, response.IsSuccessStatusCode, response.ReasonPhrase ?? ""); } }