iFactory.Godot/Artists/Scripts/Business/PlaceOrder.cs

63 lines
1.6 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using Cysharp.Threading.Tasks;
using Godot.Collections;
using Newtonsoft.Json;
using HttpClient = System.Net.Http.HttpClient;
namespace BITFactory.Business
{
public partial class PlaceOrder : Node,IAction
{
private static readonly HttpClient _httpClient = new();
[Export] private StringResource postUrl;
[Export] private Array<StringResource> keys;
[Signal]
public delegate void OnPostEventHandler(string httpContext);
[Signal]
public delegate void OnExceptionEventHandler(string exception);
[Signal]
public delegate void OnPostSuccessEventHandler(string httpContext);
public async void Execute()
{
try
{
var value =new System.Collections.Generic.Dictionary<string,string>(keys.Select(GetValue)) ;
var json = JsonHelper.Get(value);
BIT4Log.Log<PlaceOrder>(json);
if (postUrl is not null && string.IsNullOrEmpty(postUrl) is false)
{
try
{
EmitSignal(nameof(OnPostEventHandler), json);
await _httpClient.PostJsonAsync(postUrl, value);
EmitSignal(nameof(OnPostSuccess));
}
catch (Exception e)
{
await BITApp.SwitchToMainThread();
BIT4Log.LogException(e);
EmitSignal(nameof(OnExceptionEventHandler), e.Message);
}
await BITApp.SwitchToMainThread();
}
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
}
private static KeyValuePair<string,string> GetValue(StringResource stringResource)
{
return new KeyValuePair<string, string>(stringResource.Value, Data.Get<string>(stringResource.Value));
}
}
}