This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TangramClient.cs
183 lines (161 loc) · 5.5 KB
/
TangramClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using TanDotNet.Models;
using TanDotNet.Request;
using TanDotNet.Response;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Method = RestSharp.Method;
using Action = TanDotNet.Request.Action;
namespace TanDotNet
{
/// <summary>
/// Provides methods to call API methods on a Tangram node
/// </summary>
public class TangramClient : IDisposable, ITangramClient
{
private readonly CancellationTokenSource cancellationTokenSource;
private RestClient client;
public TangramClient(string nodeApiUrl)
{
client = new RestClient(nodeApiUrl);
cancellationTokenSource = new CancellationTokenSource();
}
public void Dispose()
{
client = null;
}
private async Task<T> ExecuteAsync<T>(RestRequest request)
{
var response = await client.ExecuteAsync<T>(request, cancellationTokenSource.Token);
if (response.ErrorException != null || response.Data == null)
{
var ex = new ApplicationException("Error retrieving response. Check log for more info.",
response.ErrorException);
throw ex;
}
return response.Data;
}
private async Task<T> GetAsync<T>(RequestEndpoint endpoint, object body = null)
{
try
{
var request = new RestRequest($"{endpoint.Group}/{endpoint.Action}", Method.GET);
if (body != null)
request.AddJsonBody(body);
return await ExecuteAsync<T>(request);
}
catch
{
throw;
}
}
private async Task<T> PostAsync<T>(RequestEndpoint endpoint, object body = null)
{
try
{
var request = new RestRequest($"{endpoint.Group}/{endpoint.Action}", Method.POST);
if (body != null)
request.AddJsonBody(body);
return await ExecuteAsync<T>(request);
}
catch
{
throw;
}
}
/// <inheritdoc />
public async Task<IEnumerable<string>> WalletAddresses(Wallet wallet)
{
return await PostAsync<IEnumerable<string>>(
new RequestEndpoint(Group.Wallet, Action.Addresses),
wallet);
}
/// <inheritdoc />
public async Task<double> WalletBalance(Wallet wallet)
{
return await PostAsync<double>(
new RequestEndpoint(Group.Wallet, Action.Balance),
wallet);
}
/// <inheritdoc />
public async Task<Wallet> WalletCreate(string mnemonic = null, string passphrase = null)
{
dynamic data = null;
if (mnemonic != null)
data = new
{
Mnemonic = mnemonic
};
if (passphrase != null)
data = new
{
Mnemonic = data.Mnemonic ?? null,
Passphrase = passphrase
};
return await GetAsync<Wallet>(
new RequestEndpoint(Group.Wallet, Action.Create),
data);
}
/// <inheritdoc />
public async Task<IEnumerable<WalletTransaction>> WalletHistory(Wallet wallet)
{
return await PostAsync<IEnumerable<WalletTransaction>>(
new RequestEndpoint(Group.Wallet, Action.History),
wallet);
}
/// <inheritdoc />
public async Task<IEnumerable<string>> WalletList()
{
return await GetAsync<IEnumerable<string>>(
new RequestEndpoint(Group.Wallet, Action.List));
}
/// <inheritdoc />
public async Task<WalletMnemonic> WalletMnemonic(int language = 0, int mnemonicWordCount = 24, int passphraseWordCount = 12)
{
return await GetAsync<WalletMnemonic>(
new RequestEndpoint(Group.Wallet, Action.Mnemonic),
new
{
language,
mnemonicWordCount,
passphraseWordCount
});
}
/// <inheritdoc />
public async Task<WalletReceive> WalletReceive(Wallet wallet, string paymentId)
{
return await PostAsync<WalletReceive>(
new RequestEndpoint(Group.Wallet, Action.Receive),
new
{
wallet.Identifier,
wallet.Passphrase,
paymentId
});
}
/// <inheritdoc />
public async Task<WalletSpend> WalletSpend(Wallet wallet, string address, double amount, string memo = null)
{
return await PostAsync<WalletSpend>(
new RequestEndpoint(Group.Wallet, Action.Spend),
new
{
wallet.Identifier,
wallet.Passphrase,
address,
amount,
memo
});
}
/// <inheritdoc />
public async Task<WalletProtobuf> WalletTransaction(byte[] payment)
{
return await PostAsync<WalletProtobuf>(
new RequestEndpoint(Group.Wallet, Action.Transaction),
payment);
}
}
}