-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValkyrieServerController.cs
157 lines (110 loc) · 5.2 KB
/
ValkyrieServerController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using ValkyrieFSMCore;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Valkyrie_Server
{
/// <summary>
/// Handles sending and receiving data from the Valkyrie server
/// </summary>
public class ValkyrieServerController
{
public static readonly Uri prodGetDataURI = new Uri("https://valkyrie-git-get-functions-from-server-earliestfall988.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 prodSyncFunctionsURITestBranch = new Uri("https://valkyrie-git-get-functions-from-server-earliestfall988.vercel.app/api/v1/sync-functions", 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 = prodGetDataURI;
public ValkyrieServerController()
{
}
/// <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)
{
//Debug.WriteLine("\n\n\t" + instructionId + "\n");
using HttpClient client = new();
var data = JsonSerializer.Serialize(new Content()
{
Key = ValkyrieAPIKey,
InstructionId = instructionId
});
HttpContent content = new StringContent(data);
//Debug.WriteLine("\n\n\t" + selectedURI + "\n");
//Debug.WriteLine("\n\n\t" + data + "\n");
Debug.WriteLine(ValkyrieAPIKey);
client.DefaultRequestHeaders.Add("x-api-key", ValkyrieAPIKey);
var response = await client.PostAsync(selectedURI, content);
using StreamReader reader = new StreamReader(response.Content.ReadAsStream());
reader.BaseStream.Seek(0, SeekOrigin.Begin);
string responseString = await reader.ReadToEndAsync();
//Debug.WriteLine("\n\n\n\t" + response.StatusCode + "\n");
if (!response.IsSuccessStatusCode)
{
return (false, "Error: " + response.StatusCode + "\n" + responseString);
}
if (string.IsNullOrEmpty(responseString))
{
return (false, "Error: Empty response from server");
}
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, bool success, string statusCode)> UpdateInstructionFunctionDefinitions(string instructionId)
{
if (string.IsNullOrEmpty(instructionId))
{
return ("Error: No instruction ID provided", false, "0");
}
var functionJSON = GetSyncDataHandler.GetSyncData();
Debug.WriteLine(functionJSON);
try
{
using HttpClient client = new();
HttpContent content = new StringContent(functionJSON);
client.DefaultRequestHeaders.Add("x-api-key", ValkyrieAPIKey);
client.DefaultRequestHeaders.Add("x-instruction-id", instructionId);
var response = await client.PostAsync(prodSyncFunctionsURITestBranch, content);
using StreamReader reader = new StreamReader(response.Content.ReadAsStream());
string responseString = await reader.ReadToEndAsync();
if (!response.IsSuccessStatusCode)
{
return ("Server Response Error: " + response.StatusCode + "\n" + responseString, false, response.StatusCode.ToString());
}
if (string.IsNullOrEmpty(responseString))
{
return ("Error: Empty response from server", false, "0");
}
return (responseString, response.IsSuccessStatusCode, response.ReasonPhrase ?? "");
}
catch (Exception ex)
{
return (ex.Message, false, "0");
}
}
}
/// <summary>
/// Typical content requested by the valkyrie server
/// </summary>
public struct Content
{
[JsonPropertyName("key")]
public string Key { get; set; }
[JsonPropertyName("Id")]
public string InstructionId { get; set; }
}
}