Skip to content

Commit 13a1388

Browse files
Replace Api with an interface
1 parent a261a3d commit 13a1388

File tree

7 files changed

+66
-95
lines changed

7 files changed

+66
-95
lines changed

SharpPusher/Services/Api.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.

SharpPusher/Services/IApi.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SharpPusher
2+
// Copyright (c) 2017 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using SharpPusher.Models;
7+
using System.Threading.Tasks;
8+
9+
namespace SharpPusher.Services
10+
{
11+
public interface IApi
12+
{
13+
string ApiName { get; }
14+
Task<Response> PushTx(string txHex);
15+
}
16+
}

SharpPusher/Services/P2P.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace SharpPusher.Services
1515
{
16-
public class P2P : Api
16+
public class P2P : IApi
1717
{
1818
public P2P(bool isMainNet)
1919
{
@@ -45,9 +45,9 @@ public P2P(bool isMainNet)
4545
};
4646

4747

48-
public override string ApiName => "P2P";
48+
public string ApiName => "P2P";
4949

50-
public override async Task<Response> PushTx(string txHex)
50+
public async Task<Response> PushTx(string txHex)
5151
{
5252
Response resp = new();
5353

SharpPusher/Services/PushServices/BlockCypher.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,52 @@
55

66
using Newtonsoft.Json.Linq;
77
using SharpPusher.Models;
8+
using System.Net.Http;
9+
using System;
810
using System.Threading.Tasks;
911

1012
namespace SharpPusher.Services.PushServices
1113
{
12-
public sealed class BlockCypher : Api
14+
public sealed class BlockCypher : IApi
1315
{
14-
public override string ApiName => "BlockCypher";
16+
public string ApiName => "BlockCypher";
1517

1618

17-
public override async Task<Response> PushTx(string txHex)
19+
public async Task<Response> PushTx(string txHex)
1820
{
19-
Response resp = await PushTx(txHex, "tx", "https://api.blockcypher.com/v1/bcy/test/txs/push");
20-
if (!resp.IsSuccess)
21-
{
22-
return resp;
23-
}
21+
Response resp = new();
22+
using HttpClient client = new();
2423

25-
JObject jResult = JObject.Parse(resp.Message);
26-
if (jResult["error"] != null)
24+
try
2725
{
28-
resp.SetError(jResult["error"].ToString());
26+
JObject tx = new()
27+
{
28+
{"tx", txHex}
29+
};
30+
31+
string url = "https://api.blockcypher.com/v1/bcy/test/txs/push";
32+
HttpResponseMessage httpResp = await client.PostAsync(url, new StringContent(tx.ToString()));
33+
if (!httpResp.IsSuccessStatusCode)
34+
{
35+
resp.SetError("API response doesn't indicate success.");
36+
return resp;
37+
}
38+
39+
string t = await httpResp.Content.ReadAsStringAsync();
40+
JObject jResult = JObject.Parse(t);
41+
if (jResult["error"] != null)
42+
{
43+
resp.SetError(jResult["error"]?.ToString() ?? "");
44+
}
45+
else
46+
{
47+
resp.SetMessage($"Successfully done. Tx ID: {jResult["hash"]}");
48+
}
2949
}
30-
else
50+
catch (Exception ex)
3151
{
32-
resp.SetMessage($"Successfully done. Tx ID: {jResult["hash"]}");
52+
string errMsg = (ex.InnerException == null) ? ex.Message : ex.Message + " " + ex.InnerException;
53+
resp.SetError(errMsg);
3354
}
3455

3556
return resp;

SharpPusher/Services/PushServices/BlockchainInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
namespace SharpPusher.Services.PushServices
1515
{
16-
public sealed class BlockchainInfo : Api
16+
public sealed class BlockchainInfo : IApi
1717
{
18-
public override string ApiName => "Blockchain.Info";
18+
public string ApiName => "Blockchain.Info";
1919

20-
public async override Task<Response> PushTx(string txHex)
20+
public async Task<Response> PushTx(string txHex)
2121
{
2222
using HttpClient client = new();
2323
Response resp = new();
@@ -40,11 +40,11 @@ public async override Task<Response> PushTx(string txHex)
4040
if (sResult != null && sResult.StartsWith("{\"error\":"))
4141
{
4242
JObject jObject = JObject.Parse(sResult);
43-
resp.SetError(jObject["error"].ToString());
43+
resp.SetError(jObject["error"]?.ToString() ?? "");
4444
}
4545
else
4646
{
47-
resp.SetMessage(sResult);
47+
resp.SetMessage(sResult ?? "");
4848
}
4949
}
5050
else

SharpPusher/Services/PushServices/Blockchair.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace SharpPusher.Services.PushServices
1515
{
16-
public sealed class Blockchair : Api
16+
public sealed class Blockchair : IApi
1717
{
1818
public Blockchair(Chain chain)
1919
{
@@ -45,9 +45,9 @@ public enum Chain
4545

4646
private readonly Chain chain;
4747

48-
public override string ApiName => "Blockchair";
48+
public string ApiName => "Blockchair";
4949

50-
public override async Task<Response> PushTx(string txHex)
50+
public async Task<Response> PushTx(string txHex)
5151
{
5252
using HttpClient client = new();
5353
Response resp = new();
@@ -83,7 +83,7 @@ public override async Task<Response> PushTx(string txHex)
8383

8484
var content = new FormUrlEncodedContent(
8585
[
86-
new KeyValuePair<string, string>("data", txHex)
86+
new KeyValuePair<string, string>("data", txHex)
8787
]);
8888

8989
HttpResponseMessage httpResp = await client.PostAsync(url, content);

SharpPusher/ViewModels/MainWindowViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ private void SetApiList()
189189
}
190190

191191

192-
private ObservableCollection<Api> _apiList = new();
193-
public ObservableCollection<Api> ApiList
192+
private ObservableCollection<IApi> _apiList = new();
193+
public ObservableCollection<IApi> ApiList
194194
{
195195
get => _apiList;
196196
set => SetField(ref _apiList, value);
197197
}
198198

199199

200-
private Api _selApi;
201-
public Api SelectedApi
200+
private IApi _selApi;
201+
public IApi SelectedApi
202202
{
203203
get => _selApi;
204204
set

0 commit comments

Comments
 (0)