more work on api
This commit is contained in:
@@ -50,50 +50,55 @@ public class ApiServer(string apiUrl) : ApiClient, IApiServer
|
||||
protected virtual void Listen()
|
||||
{
|
||||
while (httpListener.IsListening)
|
||||
CheckContext();
|
||||
}
|
||||
|
||||
protected virtual void CheckContext()
|
||||
{
|
||||
var context = httpListener.GetContext();
|
||||
void close() => context.Response.OutputStream.Close();
|
||||
|
||||
if (context.Request.HttpMethod != HttpMethod.Post.Method
|
||||
|| context.Request.ContentType is not string contentType
|
||||
|| !contentType.Contains("application/json"))
|
||||
{
|
||||
var context = httpListener.GetContext();
|
||||
|
||||
if (context.Request.HttpMethod != HttpMethod.Post.Method
|
||||
|| context.Request.ContentType is not string contentType
|
||||
|| contentType.Contains("application/json")
|
||||
|| context.Request.AcceptTypes is null
|
||||
|| context.Request.AcceptTypes.Contains("application/json"))
|
||||
{
|
||||
close();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse url
|
||||
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty);
|
||||
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
|
||||
{
|
||||
close();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read input content
|
||||
using StreamReader input = new(context.Request.InputStream);
|
||||
var contentJson = input.ReadToEnd();
|
||||
|
||||
// Handle message
|
||||
if (HandleMessage(path, contentJson) is not string resultJson)
|
||||
{
|
||||
close();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set response parameters
|
||||
context.Response.StatusCode = (int)args.ResponseStatusCode;
|
||||
context.Response.StatusDescription = args.ResponseStatusDescription;
|
||||
|
||||
// Write response content
|
||||
context.Response.ContentType = ContentTypes.CONTENT_TYPE_JSON;
|
||||
using StreamWriter output = new(context.Response.OutputStream);
|
||||
output.Write(resultJson);
|
||||
|
||||
close();
|
||||
void close() => context.Response.OutputStream.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse url
|
||||
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty);
|
||||
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
|
||||
{
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Read input content
|
||||
using StreamReader input = new(context.Request.InputStream);
|
||||
var contentJson = input.ReadToEnd();
|
||||
|
||||
// Handle message
|
||||
if (HandleMessage(path, contentJson) is not PrivateApiResult result)
|
||||
{
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Set response parameters
|
||||
context.Response.StatusCode = (int)result.Original.StatusCode;
|
||||
if (result.Original.StatusDescription is not null)
|
||||
context.Response.StatusDescription = result.Original.StatusDescription;
|
||||
|
||||
// Write response content
|
||||
if (result.ResultJson is not null)
|
||||
{
|
||||
context.Response.ContentType = "application/json";
|
||||
using StreamWriter output = new(context.Response.OutputStream);
|
||||
output.Write(result);
|
||||
}
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
protected virtual PrivateApiResult? HandleMessage(string url, string json)
|
||||
|
||||
Reference in New Issue
Block a user