Skip to content

Commit

Permalink
retooling discover functions api
Browse files Browse the repository at this point in the history
  • Loading branch information
EarliestFall988 committed Nov 19, 2023
1 parent dc4d29a commit b395aa9
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 13 deletions.
21 changes: 21 additions & 0 deletions DiscoverFunctionsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Avalon;

using System.Text.Json;


namespace Valkyrie_Server
{
public class DiscoverFunctionsHandler
{

/// <summary>
/// Get the functions from the libary as a JSON string
/// </summary>
/// <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())));
}
}
55 changes: 43 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,49 @@
return splash;
});

app.MapGet("/api/v1/sync", (HttpContext ctx) =>
app.MapGet("/api/v1/sync", async (HttpContext ctx) =>
{

ctx.RequestAborted.ThrowIfCancellationRequested();

var instructionId = ctx.Request.RouteValues["id"] as string;
string key = ctx.Request.Headers["apikey"];

if (string.IsNullOrEmpty(key))
{
ctx.Response.StatusCode = 401;
return "No API key provided";
}

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

if (string.IsNullOrEmpty(instructionId))
{
ctx.Response.StatusCode = 400;
return "No instruction ID provided";
}

ctx.Response.Headers.Add("Content-Type", "application/json");
ctx.Response.StatusCode = 200;
return JsonSerializer.Serialize(new message("Syncing"));

ValkyrieServerController valkyrieServerController = new ValkyrieServerController()
{
ValkyrieAPIKey = valkApiKey
};

var res = await valkyrieServerController.UpdateInstructionFunctionDefinitions(instructionId);

if (res.statusCode == "200")
{
ctx.Response.StatusCode = 200;
return JsonSerializer.Serialize(new message("Syncing"));
}

ctx.Response.StatusCode = int.Parse(res.statusCode);
return JsonSerializer.Serialize(new message(res.response));
});


Expand Down Expand Up @@ -174,20 +212,13 @@
return "incorrect api key";
}

var lib = new FunctionLibrary();


try
{

var result = JsonSerializer.Serialize(lib.ImportedFunctions.Select(x =>
{
return new FunctionListItem(x.Key, x.Value.Description, x.Value.ExpectedParameters.Select(x => x.Value).ToArray());
}));


context.Response.Headers.Add("Content-Type", "application/json");
context.Response.StatusCode = 200;
return result;
return DiscoverFunctionsHandler.GetFunctionDefinitionsJSON();
}
catch (Exception e)
{
Expand Down
61 changes: 60 additions & 1 deletion ValkyrieServerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Valkyrie_Server
{
/// <summary>
/// Handles sending and receiving data from the Valkyrie server
/// </summary>
public class ValkyrieServerController
{

Expand All @@ -14,8 +17,22 @@ public class ValkyrieServerController

public static readonly Uri testURI = new Uri("http://localhost:5000/api/sm/guess", UriKind.Absolute);

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

/// <summary>
/// The API key for the Valkyrie server
/// </summary>
public string ValkyrieAPIKey { get; init; } = "";


/// <summary>
/// Try get the instructions from the Valkyrie server
/// </summary>
/// <param name="instructionId">the instruction ID</param>
/// <returns>the result of the operation and the content of the response</returns>
public async Task<(bool result, string content)> TryGetInstructions(string instructionId)
{

Expand All @@ -31,7 +48,6 @@ public class ValkyrieServerController

HttpContent content = new StringContent(data);

var selectedURI = testURI;

//Debug.WriteLine("\n\n\t" + selectedURI + "\n");
//Debug.WriteLine("\n\n\t" + data + "\n");
Expand All @@ -57,8 +73,51 @@ public class ValkyrieServerController
}
return (true, responseString);
}

/// <summary>
/// Update the function definitions for the given instruction ID
/// </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)
{

if (string.IsNullOrEmpty(instructionId))
{
return ("Error: No instruction ID provided", "0");
}

using HttpClient client = new();

var functionJSON = DiscoverFunctionsHandler.GetFunctionDefinitionsJSON();

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);

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());
}

if (string.IsNullOrEmpty(responseString))
{
return ("Error: Empty response from server", "0");
}

return (responseString, response.StatusCode.ToString());
}
}

/// <summary>
/// Typical content requested by the valkyrie server
/// </summary>
public struct Content
{
[JsonPropertyName("key")]
Expand Down

0 comments on commit b395aa9

Please sign in to comment.