diff --git a/SharpPusher/Services/Api.cs b/SharpPusher/Services/Api.cs deleted file mode 100644 index c22aca0..0000000 --- a/SharpPusher/Services/Api.cs +++ /dev/null @@ -1,66 +0,0 @@ -// SharpPusher -// Copyright (c) 2017 Coding Enthusiast -// Distributed under the MIT software license, see the accompanying -// file LICENCE or http://www.opensource.org/licenses/mit-license.php. - -using Newtonsoft.Json.Linq; -using SharpPusher.Models; -using System; -using System.Net.Http; -using System.Threading.Tasks; - -namespace SharpPusher.Services -{ - public abstract class Api - { - /// - /// Name of the Api service. Used for showing the name in GUI. - /// - public abstract string ApiName { get; } - - - /// - /// Broadcasts a signed raw transactions in hex format. - /// - /// Signed raw transaction in hex format - /// Result of broadcasting. - public abstract Task PushTx(string txHex); - - - /// - /// Broadcasts a signed raw transactions in hex format. - /// - /// Signed raw transaction in hex format - /// The JSON key used for making the HttpContent in JSON format. - /// Api url to use. - /// Result of broadcasting. - protected static async Task PushTx(string txHex, string jKey, string url) - { - Response resp = new(); - - using HttpClient client = new(); - try - { - JObject tx = new() - { - {jKey, txHex} - }; - - HttpResponseMessage httpResp = await client.PostAsync(url, new StringContent(tx.ToString())); - if (!httpResp.IsSuccessStatusCode) - { - resp.SetError("API response doesn't indicate success."); - } - resp.SetMessage(await httpResp.Content.ReadAsStringAsync()); - } - catch (Exception ex) - { - string errMsg = (ex.InnerException == null) ? ex.Message : ex.Message + " " + ex.InnerException; - resp.SetError(errMsg); - } - - return resp; - } - - } -} diff --git a/SharpPusher/Services/IApi.cs b/SharpPusher/Services/IApi.cs new file mode 100644 index 0000000..4f11756 --- /dev/null +++ b/SharpPusher/Services/IApi.cs @@ -0,0 +1,16 @@ +// SharpPusher +// Copyright (c) 2017 Coding Enthusiast +// Distributed under the MIT software license, see the accompanying +// file LICENCE or http://www.opensource.org/licenses/mit-license.php. + +using SharpPusher.Models; +using System.Threading.Tasks; + +namespace SharpPusher.Services +{ + public interface IApi + { + string ApiName { get; } + Task PushTx(string txHex); + } +} diff --git a/SharpPusher/Services/P2P.cs b/SharpPusher/Services/P2P.cs index e1b25a8..9dd465e 100644 --- a/SharpPusher/Services/P2P.cs +++ b/SharpPusher/Services/P2P.cs @@ -13,7 +13,7 @@ namespace SharpPusher.Services { - public class P2P : Api + public class P2P : IApi { public P2P(bool isMainNet) { @@ -45,9 +45,9 @@ public P2P(bool isMainNet) }; - public override string ApiName => "P2P"; + public string ApiName => "P2P"; - public override async Task PushTx(string txHex) + public async Task PushTx(string txHex) { Response resp = new(); diff --git a/SharpPusher/Services/PushServices/BlockCypher.cs b/SharpPusher/Services/PushServices/BlockCypher.cs index 8fe66c1..c714850 100644 --- a/SharpPusher/Services/PushServices/BlockCypher.cs +++ b/SharpPusher/Services/PushServices/BlockCypher.cs @@ -5,31 +5,52 @@ using Newtonsoft.Json.Linq; using SharpPusher.Models; +using System.Net.Http; +using System; using System.Threading.Tasks; namespace SharpPusher.Services.PushServices { - public sealed class BlockCypher : Api + public sealed class BlockCypher : IApi { - public override string ApiName => "BlockCypher"; + public string ApiName => "BlockCypher"; - public override async Task PushTx(string txHex) + public async Task PushTx(string txHex) { - Response resp = await PushTx(txHex, "tx", "https://api.blockcypher.com/v1/bcy/test/txs/push"); - if (!resp.IsSuccess) - { - return resp; - } + Response resp = new(); + using HttpClient client = new(); - JObject jResult = JObject.Parse(resp.Message); - if (jResult["error"] != null) + try { - resp.SetError(jResult["error"].ToString()); + JObject tx = new() + { + {"tx", txHex} + }; + + string url = "https://api.blockcypher.com/v1/bcy/test/txs/push"; + HttpResponseMessage httpResp = await client.PostAsync(url, new StringContent(tx.ToString())); + if (!httpResp.IsSuccessStatusCode) + { + resp.SetError("API response doesn't indicate success."); + return resp; + } + + string t = await httpResp.Content.ReadAsStringAsync(); + JObject jResult = JObject.Parse(t); + if (jResult["error"] != null) + { + resp.SetError(jResult["error"]?.ToString() ?? ""); + } + else + { + resp.SetMessage($"Successfully done. Tx ID: {jResult["hash"]}"); + } } - else + catch (Exception ex) { - resp.SetMessage($"Successfully done. Tx ID: {jResult["hash"]}"); + string errMsg = (ex.InnerException == null) ? ex.Message : ex.Message + " " + ex.InnerException; + resp.SetError(errMsg); } return resp; diff --git a/SharpPusher/Services/PushServices/BlockchainInfo.cs b/SharpPusher/Services/PushServices/BlockchainInfo.cs index 73ac71c..35bf797 100644 --- a/SharpPusher/Services/PushServices/BlockchainInfo.cs +++ b/SharpPusher/Services/PushServices/BlockchainInfo.cs @@ -13,11 +13,11 @@ namespace SharpPusher.Services.PushServices { - public sealed class BlockchainInfo : Api + public sealed class BlockchainInfo : IApi { - public override string ApiName => "Blockchain.Info"; + public string ApiName => "Blockchain.Info"; - public async override Task PushTx(string txHex) + public async Task PushTx(string txHex) { using HttpClient client = new(); Response resp = new(); @@ -40,11 +40,11 @@ public async override Task PushTx(string txHex) if (sResult != null && sResult.StartsWith("{\"error\":")) { JObject jObject = JObject.Parse(sResult); - resp.SetError(jObject["error"].ToString()); + resp.SetError(jObject["error"]?.ToString() ?? ""); } else { - resp.SetMessage(sResult); + resp.SetMessage(sResult ?? ""); } } else diff --git a/SharpPusher/Services/PushServices/Blockchair.cs b/SharpPusher/Services/PushServices/Blockchair.cs index 4e22475..ec22410 100644 --- a/SharpPusher/Services/PushServices/Blockchair.cs +++ b/SharpPusher/Services/PushServices/Blockchair.cs @@ -13,7 +13,7 @@ namespace SharpPusher.Services.PushServices { - public sealed class Blockchair : Api + public sealed class Blockchair : IApi { public Blockchair(Chain chain) { @@ -45,9 +45,9 @@ public enum Chain private readonly Chain chain; - public override string ApiName => "Blockchair"; + public string ApiName => "Blockchair"; - public override async Task PushTx(string txHex) + public async Task PushTx(string txHex) { using HttpClient client = new(); Response resp = new(); @@ -83,7 +83,7 @@ public override async Task PushTx(string txHex) var content = new FormUrlEncodedContent( [ - new KeyValuePair("data", txHex) + new KeyValuePair("data", txHex) ]); HttpResponseMessage httpResp = await client.PostAsync(url, content); diff --git a/SharpPusher/ViewModels/MainWindowViewModel.cs b/SharpPusher/ViewModels/MainWindowViewModel.cs index b0ed8a7..7d9ebec 100644 --- a/SharpPusher/ViewModels/MainWindowViewModel.cs +++ b/SharpPusher/ViewModels/MainWindowViewModel.cs @@ -189,16 +189,16 @@ private void SetApiList() } - private ObservableCollection _apiList = new(); - public ObservableCollection ApiList + private ObservableCollection _apiList = new(); + public ObservableCollection ApiList { get => _apiList; set => SetField(ref _apiList, value); } - private Api _selApi; - public Api SelectedApi + private IApi _selApi; + public IApi SelectedApi { get => _selApi; set