Skip to content

Commit

Permalink
sharing custom variables system, and working on various functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EarliestFall988 committed Dec 9, 2023
1 parent 8257e88 commit 667a953
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 91 deletions.
21 changes: 0 additions & 21 deletions DiscoverFunctionsHandler.cs

This file was deleted.

64 changes: 64 additions & 0 deletions GetSyncDataHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using ValkyrieFSMCore;

using System.Text.Json;
using System.Collections;
using System.Diagnostics;

namespace Valkyrie_Server
{
public class GetSyncDataHandler
{

/// <summary>
/// Get the functions from the libary as a JSON string
/// </summary>
/// <returns>returns the json string of the functions</returns>
public static IEnumerable<FunctionListItem> GetFunctionDefinitions() =>
new FunctionLibrary().ImportedFunctions.Values.Select(x => new FunctionListItem()
{
Name = x.Name,
Description = x.Description,
Parameters = x.ExpectedParametersList
});

public static IEnumerable<VariableTypeDef> GetVariableTypes() => new VariableFactory().GetAllPossibleVarableTypes.Select(x => new VariableTypeDef()
{
Description = x.description,
Name = x.key
});

public static string GetSyncData()
{
var functions = GetFunctionDefinitions();
var variableTypes = GetVariableTypes();

foreach (var x in functions)
{
foreach (var y in x.Parameters)
{
Debug.WriteLine(y.Name + " " + y.Type + " " + y.IO + " " + y.Description);
}
}

var syncData = new SyncRequestBody(functions.ToArray(), variableTypes.ToArray());

return JsonSerializer.Serialize(syncData);
}
}

public class VariableTypeDef
{
public string Name { get; set; } = "";
public string Description { get; set; } = "";
}

public class FunctionListItem
{
public string Name { get; set; } = "";
public string Description { get; set; } = "";

public ExpectedParameter[] Parameters { get; set; } = new ExpectedParameter[0];
}

internal record SyncRequestBody(FunctionListItem[] Functions, VariableTypeDef[] CustomTypes);
}
88 changes: 30 additions & 58 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
.Build();

string linkURI = env["VALK_DASHBOARD_LINK"];

string version = env["VERSION"];

var splash = "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n <meta charset=\"utf-8\" />\r\n <title>It Works!!</title>\r\n <link rel=\"preconnect\" href=\"https://fonts.googleapis.com\">\r\n <link rel=\"preconnect\" href=\"https://fonts.gstatic.com\" crossorigin>\r\n <link href=\"https://fonts.googleapis.com/css2?family=Roboto&display=swap\" rel=\"stylesheet\">\r\n</head>\r\n<script>\r\n\r\n getURI = () => {\r\n document.getElementById(\"link-to-paste\").innerHTML = window.location.href + \"api/v1/sync\";\r\n }\r\n\r\n</script>\r\n<style>\r\n .body {\r\n background-color: #171717;\r\n width: 90vw;\r\n height: 90vh;\r\n color: #ffffff;\r\n padding: 3rem;\r\n font-family: 'Roboto', sans-serif;\r\n display: block;\r\n box-sizing: border-box;\r\n }\r\n\r\n h1 {\r\n font-size: 3rem;\r\n }\r\n\r\n h1, h2 {\r\n user-select: none;\r\n }\r\n\r\n h2 {\r\n font-size: 1.5rem;\r\n font-weight: 200;\r\n }\r\n\r\n p {\r\n font-size: 1.25rem;\r\n }\r\n\r\n a {\r\n font-size: 1.25rem;\r\n color: #ffff;\r\n text-decoration: none;\r\n background-color: #1d4ed8;\r\n padding: 0.25rem;\r\n border-radius: 0.25rem;\r\n }\r\n\r\n .sync-link {\r\n color: lightblue;\r\n text-decoration: underline;\r\n text-underline-offset: 0.25rem;\r\n font-size: 1.5rem;\r\n }\r\n\r\n .no-select {\r\n user-select: none;\r\n }\r\n</style>\r\n<body onload=\"getURI()\" class=\"body\">\r\n " +
Expand All @@ -31,33 +30,6 @@
var apiKey = env["API_KEY"];
var valkApiKey = "some key";

var projectTest = new GetProjects();
projectTest.Parameters.Add("out", VariableDefinition<List<Project>>.CreateCustom("out", "projects", new()));
var result = projectTest.Function();

Debug.WriteLine("projects ");
foreach (var x in projectTest.Get<List<Project>>("out"))
{
Debug.WriteLine(x.Name);
}

Debug.WriteLine("\n\n\n\n");

var project = projectTest.Get<List<Project>>("out")[0];

var split = new SplitProject();
split.Parameters.Add("project", new VariableDefinition<Project>("project", project, "project"));
split.Parameters.Add("name", VariableDefinition<string>.CreateString("name", ""));
split.Parameters.Add("id", VariableDefinition<string>.CreateString("id", ""));


var splitResult = split.Function();

Debug.WriteLine("split result: " + splitResult);

Debug.WriteLine("name: " + split.Get<string>("name"));


long minuteCountWaitTime = 5;
StateMachinesController? stateMachinesController = new StateMachinesController(minuteCountWaitTime);

Expand Down Expand Up @@ -230,36 +202,36 @@
});


app.MapGet("/api/v1/functions", (HttpContext context) =>
{

string key = context.Request.Headers["apikey"];

if (key == null)
{
context.Response.StatusCode = 401;
return "No API key provided";
}

if (apiKey != key)
{
context.Response.StatusCode = 401;
return "incorrect api key";
}

//app.MapGet("/api/v1/functions", (HttpContext context) =>
//{

try
{
context.Response.Headers.Add("Content-Type", "application/json");
context.Response.StatusCode = 200;
return DiscoverFunctionsHandler.GetFunctionDefinitionsJSON();
}
catch (Exception e)
{
context.Response.StatusCode = 500;
return e.Message;
}
});
// string key = context.Request.Headers["apikey"];

// if (key == null)
// {
// context.Response.StatusCode = 401;
// return "No API key provided";
// }

// if (apiKey != key)
// {
// context.Response.StatusCode = 401;
// return "incorrect api key";
// }


// try
// {
// context.Response.Headers.Add("Content-Type", "application/json");
// context.Response.StatusCode = 200;
// return GetSyncDataHandler.GetFunctionDefinitions();
// }
// catch (Exception e)
// {
// context.Response.StatusCode = 500;
// return e.Message;
// }
//});

app.Run();

Expand All @@ -270,7 +242,7 @@
/// <param name="Name">The name of the function</param>
/// <param name="Description">The description of the function</param>
/// <param name="Parameters">the parameters of the function</param>
internal record FunctionListItem(string Name, string Description, ExpectedParameter[] Parameters);


internal record message(string content);
internal record errResult(string error, string reasonPhrase);
Expand Down
7 changes: 4 additions & 3 deletions State Machine/Function Definitions/WM/GetProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace ValkyrieFSMCore.WM
{
/// <summary>
/// Get the list of projects function
/// </summary>
public class GetProjects : FunctionDefinition
{
private static readonly string BASE_URL = "https://valkyrie-git-get-functions-from-server-earliestfall988.vercel.app/";
Expand All @@ -25,7 +28,7 @@ void Setup()
Name = "GetProjects";
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "out", new Parameter("Projects", VariableIO.Out) }
{ "result", new Parameter("projects", VariableIO.Out) }
};
}

Expand Down Expand Up @@ -71,8 +74,6 @@ protected override void DefineFunction()

Debug.WriteLine(json);



var projects = JsonSerializer.Deserialize<ProjectsListResult>(json);

if (projects == null)
Expand Down
3 changes: 2 additions & 1 deletion State Machine/FunctionDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -50,7 +51,7 @@ public ExpectedParameter[] ExpectedParametersList

foreach (var x in ExpectedParameters)
{
parameters.Add(new ExpectedParameter(x.Key, x.Value.IO.ToString(), x.Value.Type.ToString()));
parameters.Add(new ExpectedParameter(x.Key, x.Value.Type.ToString(), x.Value.IO.ToString(), x.Value.Description));
}

return parameters.ToArray();
Expand Down
2 changes: 1 addition & 1 deletion State Machine/FunctionLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void BuildFunctionLibrary()

ImportedFunctions.Add(eachProject.Name, eachProject);
ImportedFunctions.Add(getProjects.Name, getProjects);
ImportedFunctions.Add(splitProject.Name, getProjects);
ImportedFunctions.Add(splitProject.Name, splitProject);

#endregion
}
Expand Down
16 changes: 12 additions & 4 deletions State Machine/VariableTypes/VariableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ public class VariableFactory
/// <summary>
/// The variable constructors
/// </summary>
private Dictionary<string, Func<string, VariableIO, IVariableSignature>> VariableConstructors { get; set; } = new();
private Dictionary<string, (string description, Func<string, VariableIO, IVariableSignature> function)> VariableConstructors { get; set; } = new();

/// <summary>
/// Get the possible variable types
/// </summary>
public IEnumerable<(string key, string description)> GetAllPossibleVarableTypes =>
VariableConstructors.Select(x => (x.Key, x.Value.description));


/// <summary>
/// Default constructor
Expand All @@ -20,11 +27,12 @@ public class VariableFactory
/// </summary>
void Register()
{
VariableConstructors.Add("projects", (key, io) =>
VariableConstructors.Add("projects", ("this variable maps to a list of projects", (key, io) =>
{
var x = VariableDefinition<List<Project>>.CreateCustom(key, "projects", new List<Project>(), io);
return x;
});
}
));
}

/// <summary>
Expand All @@ -44,7 +52,7 @@ public bool TryConstructVariable<T>(string key, string type, out T? result, Vari
return false;
}

var func = VariableConstructors[type];
var func = VariableConstructors[type].function;

result = func(key, io) as T;
return true;
Expand Down
12 changes: 9 additions & 3 deletions ValkyrieServerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public ValkyrieServerController()
}


var functionJSON = DiscoverFunctionsHandler.GetFunctionDefinitionsJSON();
var functionJSON = GetSyncDataHandler.GetSyncData();
Debug.WriteLine(functionJSON);

using HttpClient client = new();
Expand All @@ -105,15 +105,15 @@ public ValkyrieServerController()

client.DefaultRequestHeaders.Add("x-api-key", ValkyrieAPIKey);
client.DefaultRequestHeaders.Add("x-instruction-id", instructionId);
var response = await client.PostAsync(prodSyncFunctionsURITestBranch, content);
var response = await client.PostAsync(testSyncFunctionsURI, content);

using StreamReader reader = new StreamReader(response.Content.ReadAsStream());

string responseString = await reader.ReadToEndAsync();

if (!response.IsSuccessStatusCode)
{
return ("Error: " + response.StatusCode + "\n" + responseString, false, response.StatusCode.ToString());
return ("Server Response Error: " + response.StatusCode + "\n" + responseString, false, response.StatusCode.ToString());
}

if (string.IsNullOrEmpty(responseString))
Expand All @@ -125,6 +125,9 @@ public ValkyrieServerController()
}
}




/// <summary>
/// Typical content requested by the valkyrie server
/// </summary>
Expand All @@ -136,4 +139,7 @@ public struct Content
[JsonPropertyName("Id")]
public string InstructionId { get; set; }
}



}

0 comments on commit 667a953

Please sign in to comment.