|
5 | 5 |
|
6 | 6 | using Newtonsoft.Json.Linq; |
7 | 7 | using SharpPusher.Models; |
| 8 | +using System.Net.Http; |
| 9 | +using System; |
8 | 10 | using System.Threading.Tasks; |
9 | 11 |
|
10 | 12 | namespace SharpPusher.Services.PushServices |
11 | 13 | { |
12 | | - public sealed class BlockCypher : Api |
| 14 | + public sealed class BlockCypher : IApi |
13 | 15 | { |
14 | | - public override string ApiName => "BlockCypher"; |
| 16 | + public string ApiName => "BlockCypher"; |
15 | 17 |
|
16 | 18 |
|
17 | | - public override async Task<Response> PushTx(string txHex) |
| 19 | + public async Task<Response> PushTx(string txHex) |
18 | 20 | { |
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(); |
24 | 23 |
|
25 | | - JObject jResult = JObject.Parse(resp.Message); |
26 | | - if (jResult["error"] != null) |
| 24 | + try |
27 | 25 | { |
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 | + } |
29 | 49 | } |
30 | | - else |
| 50 | + catch (Exception ex) |
31 | 51 | { |
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); |
33 | 54 | } |
34 | 55 |
|
35 | 56 | return resp; |
|
0 commit comments