-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
314 lines (260 loc) · 11.2 KB
/
Copy pathProgram.cs
File metadata and controls
314 lines (260 loc) · 11.2 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bondora.Api.Client.Sample.DotNet.Models;
namespace Bondora.Api.Client.Sample.DotNet
{
class Program
{
private static ApiClient apiClient;
static void Main(string[] args)
{
apiClient = new ApiClient(ApiConfig.ApiBaseUri);
RunAsync().Wait();
}
static async Task<bool> Authorize()
{
Logger.LogInfo("Do you want to use OAuth authorization?");
Logger.LogInfo("Insert [Y] to use and just press [Enter] for none.");
string answer = Console.ReadLine();
if (string.IsNullOrWhiteSpace(answer) ||
!string.Equals(answer, "y", StringComparison.InvariantCultureIgnoreCase))
{
Logger.LogWarning("Skipping Authorization Flow.");
return true;
}
var scopes = string.Join("%20", ApiConfig.OAuthScopes.Split(','));
string authUri = string.Format("{0}?client_id={1}&scope={2}&response_type=code&redirect_uri={3}",
ApiConfig.OAuthAuthorizeUri, ApiConfig.ClientId, scopes, ApiConfig.RedirectUri);
// Open the default Brower with Uri
System.Diagnostics.Process.Start(authUri);
Logger.LogInfo("Copy the received Uri from the Authorization Flow.");
Logger.LogInfo("Insert the received {{code}} value and press [Enter]:");
string code = Console.ReadLine();
if (string.IsNullOrEmpty(code))
{
Logger.LogWarning("No input was given. Cannot continue with OAuth authorization.");
return false;
}
var accessTokenResult = await apiClient.GetAccessTokenByCode(code,
ApiConfig.ClientId, ApiConfig.ClientSecret, ApiConfig.RedirectUri);
if (accessTokenResult == null)
{
Logger.LogError("Getting the Access Token failed.");
return false;
}
else
{
Logger.LogSuccess("Received Access Token: {0}", accessTokenResult.access_token);
}
apiClient.AccessToken = accessTokenResult.access_token;
apiClient.RefreshToken = accessTokenResult.refresh_token;
apiClient.TokenValidUntilUtc = accessTokenResult.valid_until > 0
? (DateTime?) GetDateTimeFromUnixTime(accessTokenResult.valid_until)
: null;
return true;
}
private static DateTime GetDateTimeFromUnixTime(long unixTime)
{
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) + TimeSpan.FromSeconds(unixTime);
}
static async Task RunAsync()
{
try
{
bool success = await Authorize();
if (success)
await ExecuteApiCalls();
}
catch (Exception ex)
{
Logger.LogError("Error occured: " + ex.Message);
}
// Read line, so that the process is not closed
Logger.LogInfo("Press [Enter] to exit");
Console.ReadLine();
}
static async Task ExecuteApiCalls()
{
// If no accessToken is received, cannot continue
if (string.IsNullOrEmpty(apiClient.AccessToken))
{
Logger.LogWarning("No Access Token set. Cannot continue!");
return;
}
if (apiClient.RefreshToken != null)
{
Logger.LogInfo("Refresh token is set. Trying to get new access token for the refresh token.");
if (!await GetNewAccessTokenFromRefreshToken())
Logger.LogError("Could not get new access token for refresh token! \n");
}
await ShowAuctions();
await ShowBids();
await ShowInvestements();
await ShowSecondMarketItems();
Logger.LogInfo("Trying to Revoke the access token.");
if (!await RevokeAccessToken())
{
Logger.LogError("Revoking the access token failed!");
}
else
{
Logger.LogSuccess("Successfully Revoke the access token.");
}
}
private static async Task<bool> RevokeAccessToken()
{
return await apiClient.RevokeAccessToken();
}
private static async Task<bool> GetNewAccessTokenFromRefreshToken()
{
var result = await apiClient.GetAccessTokenByRefreshToken(apiClient.RefreshToken,
ApiConfig.ClientId, ApiConfig.ClientSecret, ApiConfig.RedirectUri);
if (result == null)
{
Logger.LogError("Getting the Access Token failed.");
return false;
}
else
{
Logger.LogSuccess("Received New Access Token: {0}", result.access_token);
}
apiClient.AccessToken = result.access_token;
apiClient.TokenValidUntilUtc = result.valid_until > 0
? (DateTime?)GetDateTimeFromUnixTime(result.valid_until)
: null;
return true;
}
private const bool FilterSecondaryMarketItems = false;
private static async Task ShowSecondMarketItems()
{
List<SecondMarketItem> secondMarketItems = null;
if (FilterSecondaryMarketItems)
{
int pageNr = 1;
int pageSize = 1000;
int totalCount = -1;
var ratings = new List<string> {"EE", "FI"};
secondMarketItems = new List<SecondMarketItem>();
while (totalCount < 0 || pageNr < (totalCount/pageSize))
{
var result = await apiClient.GetSecondMarketItems(pageNr, pageSize, -80, ratings);
if (result == null)
break;
if (pageNr == 1)
Logger.LogSuccess("\n\nFound Secondary Market items: \n");
var items = result != null ? result.Payload : null;
totalCount = result.TotalCount;
items
.Where(i => i.Price <= 3)
.ToList()
.ForEach(item =>
{
//Logger.LogInfo(Newtonsoft.Json.JsonConvert.SerializeObject(item));
Logger.LogInfo("Id={4}, Amount={0}, Price={1}, XIRR={2}, LateAmount={3}, Discount={5}",
item.Amount, item.Price, item.Xirr, item.LateAmountTotal, item.Id,
item.DesiredDiscountRate);
});
pageNr++;
}
}
else
{
var result = await apiClient.GetSecondMarketItems(1, 10);
if (result != null && result.Payload != null)
secondMarketItems = result.Payload.ToList();
if (secondMarketItems != null && secondMarketItems.Count > 0)
{
Logger.LogSuccess("\n\nFound secondary market items: \n");
foreach (SecondMarketItem item in secondMarketItems)
{
Logger.LogInfo("Id={4}\nAmount={0}, Price={1}, XIRR={2}, LateAmount={3}, Discount={5}",
item.Amount, item.Price, item.Xirr, item.LateAmountTotal, item.Id, item.DesiredDiscountRate);
}
}
else
{
Logger.LogWarning("\nNo secondary market items found.\n");
}
}
}
private static async Task ShowInvestements()
{
// Get Investments
var investmentsResult = await apiClient.GetInvestments();
var investments = investmentsResult != null ? investmentsResult.Payload : null;
if (investments != null && investments.Count > 0)
{
Logger.LogSuccess("\n\nFound Investments: \n");
foreach (Investment investment in investments)
{
Logger.LogInfo("Amount={0}, Interest={1}, LateAmount={2}, UserName={3}",
investment.Amount, investment.Interest, investment.LateAmountTotal, investment.UserName);
}
}
else
{
Logger.LogWarning("\nNo Investments found.\n");
}
}
private static async Task ShowBids()
{
// Get list of pending API bids (bid status = 0 as example)
var bidsResult = await apiClient.GetBids(1, 100);
var bidList = bidsResult != null ? bidsResult.Payload : null;
if (bidList != null && bidList.Count > 0)
{
Logger.LogSuccess("\nFound Bids:\n");
foreach (var bid in bidList)
{
Logger.LogInfo("Requested={0}, Amount={1}, Actual={2}, Status={3}",
bid.BidRequestedDate, bid.RequestedBidAmount, bid.ActualBidAmount, bid.StatusCode);
}
}
else
{
Logger.LogWarning("\nNo bids found.\n");
}
}
private static async Task ShowAuctions()
{
// Get list auctions
var ratings = new List<string> { "AA", "A", "B" };
var auctionsResult = await apiClient.GetAuctions(1, 10, ratings);
var auctions = auctionsResult != null ? auctionsResult.Payload : null;
if (auctions == null)
return;
if (auctions.Count > 0)
{
Logger.LogSuccess("\n\nFound Auctions: \n");
foreach (Auction auction in auctions)
{
Logger.LogInfo("AuctionId={0}, Amount={1}, Rating={2}", auction.AuctionId,
auction.AppliedAmount, auction.Rating);
}
}
else
{
Logger.LogWarning("\nNo auctions found.\n");
}
// Check if there are any active auctions
if (auctions.Count > 0)
{
// Get first auction for bidding
int auctionIndex = new Random().Next(auctions.Count);
Guid auctionId = auctions[auctionIndex].AuctionId;
var auction = await apiClient.GetAuction(auctionId);
if (auction != null)
Logger.LogInfo("AuctionId={0}, Amount={1}, Rating={2}", auction.AuctionId, auction.AppliedAmount, auction.Rating);
// Construct 2 bid requests for this auction
var bids = new List<Bid> { new Bid(auction.AuctionId, 0), new Bid(auction.AuctionId, 0, 0) };
// send the bid request
if (await apiClient.BidOnAuction(bids))
Logger.LogSuccess("Bid Request Succeeded! \n");
else
Logger.LogError("Bid Request Failed! \n");
}
}
}
}