-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExchangeRate.cs
More file actions
68 lines (63 loc) · 2.03 KB
/
ExchangeRate.cs
File metadata and controls
68 lines (63 loc) · 2.03 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace BankCurse
{
public class ExchangeRate
{
public int ExchangeId { set; get; }
public string ExchangeDate { set; get; } = DateTime.Today.ToString("yyyy-MM-dd");
public double Amount { set; get; }
public bool IsChanged { set; get; }
public Currency Currency { set; get; }
public ExchangeRate(int exchangeId, string exchangeDate, double amount, Currency currency)
{
ExchangeId = exchangeId;
ExchangeDate = exchangeDate;
Amount = amount;
Currency = currency;
IsChanged = true;
}
public ExchangeRate()
{
IsChanged = false;
}
public static double ParseValue(string date, string name)
{
try
{
string path = $@"https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?date={date}&json";
try
{
using (WebClient wc = new WebClient { Encoding = Encoding.UTF8 })
{
string html = wc.DownloadString(path);
var result = JsonConvert.DeserializeObject<List<Temp>>(html);
return result.Select(x => x).Single(x => x.Txt == name).Rate;
}
}
catch
{
throw new Exception("Не вдалось загрузити файл.");
}
}
catch
{
if(Convert.ToDateTime(date) != DateTime.Today.Date)
return -1;
throw new Exception("Сталася помилка.");
}
}
private class Temp
{
public int R030;
public string Txt;
public double Rate;
public string Cc;
public string ExchangeDate;
}
}
}