using CodexContractsPlugin.Marketplace; using DiscordRewards; using Logging; using Newtonsoft.Json; using System.Net.Http.Json; namespace TestNetRewarder { public class BotClient { private readonly Configuration configuration; private readonly ILog log; public BotClient(Configuration configuration, ILog log) { this.configuration = configuration; this.log = log; } public async Task IsOnline() { var result = await HttpGet(); log.Log("Is DiscordBot online: " + result); return result == "Pong"; } public async Task SendRewards(GiveRewardsCommand command) { if (command == null || command.Rewards == null || !command.Rewards.Any()) return false; var result = await HttpPostJson(command); log.Log("Reward response: " + result); return result == "OK"; } private async Task HttpGet() { try { var client = new HttpClient(); var response = await client.GetAsync(GetUrl()); return await response.Content.ReadAsStringAsync(); } catch (Exception ex) { log.Error(ex.ToString()); return string.Empty; } } private async Task HttpPostJson(T body) { try { using var client = new HttpClient(); using var content = JsonContent.Create(body); using var response = await client.PostAsync(GetUrl(), content); return await response.Content.ReadAsStringAsync(); } catch (Exception ex) { log.Error(ex.ToString()); return string.Empty; } } private string GetUrl() { return $"{configuration.DiscordHost}:{configuration.DiscordPort}/api/reward"; } } }