Skip to content

Commit

Permalink
Replace Api with an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Jan 1, 2025
1 parent a261a3d commit 13a1388
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 95 deletions.
66 changes: 0 additions & 66 deletions SharpPusher/Services/Api.cs

This file was deleted.

16 changes: 16 additions & 0 deletions SharpPusher/Services/IApi.cs
Original file line number Diff line number Diff line change
@@ -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<Response> PushTx(string txHex);
}
}
6 changes: 3 additions & 3 deletions SharpPusher/Services/P2P.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace SharpPusher.Services
{
public class P2P : Api
public class P2P : IApi
{
public P2P(bool isMainNet)
{
Expand Down Expand Up @@ -45,9 +45,9 @@ public P2P(bool isMainNet)
};


public override string ApiName => "P2P";
public string ApiName => "P2P";

public override async Task<Response> PushTx(string txHex)
public async Task<Response> PushTx(string txHex)
{
Response resp = new();

Expand Down
47 changes: 34 additions & 13 deletions SharpPusher/Services/PushServices/BlockCypher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> PushTx(string txHex)
public async Task<Response> 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;
Expand Down
10 changes: 5 additions & 5 deletions SharpPusher/Services/PushServices/BlockchainInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> PushTx(string txHex)
public async Task<Response> PushTx(string txHex)
{
using HttpClient client = new();
Response resp = new();
Expand All @@ -40,11 +40,11 @@ public async override Task<Response> 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
Expand Down
8 changes: 4 additions & 4 deletions SharpPusher/Services/PushServices/Blockchair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace SharpPusher.Services.PushServices
{
public sealed class Blockchair : Api
public sealed class Blockchair : IApi
{
public Blockchair(Chain chain)
{
Expand Down Expand Up @@ -45,9 +45,9 @@ public enum Chain

private readonly Chain chain;

public override string ApiName => "Blockchair";
public string ApiName => "Blockchair";

public override async Task<Response> PushTx(string txHex)
public async Task<Response> PushTx(string txHex)
{
using HttpClient client = new();
Response resp = new();
Expand Down Expand Up @@ -83,7 +83,7 @@ public override async Task<Response> PushTx(string txHex)

var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("data", txHex)
new KeyValuePair<string, string>("data", txHex)
]);

HttpResponseMessage httpResp = await client.PostAsync(url, content);
Expand Down
8 changes: 4 additions & 4 deletions SharpPusher/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,16 @@ private void SetApiList()
}


private ObservableCollection<Api> _apiList = new();
public ObservableCollection<Api> ApiList
private ObservableCollection<IApi> _apiList = new();
public ObservableCollection<IApi> ApiList
{
get => _apiList;
set => SetField(ref _apiList, value);
}


private Api _selApi;
public Api SelectedApi
private IApi _selApi;
public IApi SelectedApi
{
get => _selApi;
set
Expand Down

0 comments on commit 13a1388

Please sign in to comment.