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 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(keys.Select(GetValue)) ; var json = JsonHelper.Get(value); BIT4Log.Log(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 GetValue(StringResource stringResource) { return new KeyValuePair(stringResource.Value, Data.Get(stringResource.Value)); } } }