Skip to content

Commit

Permalink
service starting to process function sync requests properly
Browse files Browse the repository at this point in the history
  • Loading branch information
EarliestFall988 committed Nov 27, 2023
1 parent 38e51ef commit 3f5a5d1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 31 deletions.
6 changes: 3 additions & 3 deletions DiscoverFunctionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class DiscoverFunctionsHandler
/// <returns>returns the json string of the functions</returns>
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());
}
}
21 changes: 12 additions & 9 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand All @@ -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));
});


Expand Down Expand Up @@ -239,9 +241,10 @@
/// <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, ReferenceTuple[] Parameters);
internal record FunctionListItem(string Name, string Description, ExpectedParameter[] Parameters);

internal record message(string content);
internal record errResult(string error, string reasonPhrase);



Expand Down
36 changes: 36 additions & 0 deletions State Machine/FunctionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ public abstract class FunctionDefinition
/// <returns></returns>
public Dictionary<string, ReferenceTuple> ExpectedParameters { get; set; } = new Dictionary<string, ReferenceTuple>();

/// <summary>
/// Get the list of expected parameters
/// </summary>
public ExpectedParameter[] ExpectedParametersList
{
get
{
List<ExpectedParameter> parameters = new List<ExpectedParameter>();

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

return parameters.ToArray();
}
}

/// <summary>
/// The dictionary of parameters
/// </summary>
Expand Down Expand Up @@ -109,4 +127,22 @@ public bool TryGetVariableType(string name, out StateMachineVariableType result)
return false;
}
}

/// <summary>
/// A struct representing an expected parameter
/// </summary>
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;
}
}
}
13 changes: 6 additions & 7 deletions State Machine/FunctionLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;


Expand All @@ -20,16 +22,13 @@ public class FunctionLibrary
public Dictionary<string, FunctionDefinition> ImportedFunctions = new Dictionary<string, FunctionDefinition>();

/// <summary>
/// Default constructor. Imports all functions.
/// Default constructor. Builds all functions.
/// </summary>
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();
Expand Down
2 changes: 1 addition & 1 deletion State Machine/ReferenceTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 20 additions & 11 deletions ValkyrieServerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// the base URI for the Valkyrie server
/// </summary>
private Uri selectedURI = testURI;
private Uri selectedURI = testGetDataURI;

public ValkyrieServerController()
{

}

/// <summary>
/// The API key for the Valkyrie server
Expand Down Expand Up @@ -79,39 +86,41 @@ public class ValkyrieServerController
/// </summary>
/// <param name="instructionId"> the instruction ID</param>
/// <returns> the result of the update</returns>
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());

string responseString = await reader.ReadToEndAsync();

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 ?? "");
}
}

Expand Down

0 comments on commit 3f5a5d1

Please sign in to comment.