-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrencyConverter.cs
163 lines (147 loc) · 5.16 KB
/
CurrencyConverter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Newtonsoft.Json;
using RestSharp;
namespace Sharpvin
{
internal class ER_API_Obj
{
public string result { get; set; }
public string documentation { get; set; }
public string terms_of_use { get; set; }
public string time_last_update_unix { get; set; }
public string time_last_update_utc { get; set; }
public string time_next_update_unix { get; set; }
public string time_next_update_utc { get; set; }
public string base_code { get; set; }
public ConversionRate conversion_rates { get; set; }
}
internal class ConversionRate
{
public double AED { get; set; }
public double ARS { get; set; }
public double AUD { get; set; }
public double BGN { get; set; }
public double BRL { get; set; }
public double BSD { get; set; }
public double CAD { get; set; }
public double CHF { get; set; }
public double CLP { get; set; }
public double CNY { get; set; }
public double COP { get; set; }
public double CZK { get; set; }
public double DKK { get; set; }
public double DOP { get; set; }
public double EGP { get; set; }
public double EUR { get; set; }
public double FJD { get; set; }
public double GBP { get; set; }
public double GTQ { get; set; }
public double HKD { get; set; }
public double HRK { get; set; }
public double HUF { get; set; }
public double IDR { get; set; }
public double ILS { get; set; }
public double INR { get; set; }
public double ISK { get; set; }
public double JPY { get; set; }
public double KRW { get; set; }
public double KZT { get; set; }
public double MXN { get; set; }
public double MYR { get; set; }
public double NOK { get; set; }
public double NZD { get; set; }
public double PAB { get; set; }
public double PEN { get; set; }
public double PHP { get; set; }
public double PKR { get; set; }
public double PLN { get; set; }
public double PYG { get; set; }
public double RON { get; set; }
public double RUB { get; set; }
public double SAR { get; set; }
public double SEK { get; set; }
public double SGD { get; set; }
public double THB { get; set; }
public double TRY { get; set; }
public double TWD { get; set; }
public double UAH { get; set; }
public double USD { get; set; }
public double UYU { get; set; }
public double ZAR { get; set; }
}
internal class CurrencyConverter
{
private readonly HttpClient client;
private ConversionRate rates;
public CurrencyConverter()
{
client = new HttpClient();
rates = new ConversionRate();
}
public async Task<bool> UpdateRates()
{
try
{
String url = "https://v6.exchangerate-api.com/v6/413271983b9242697900a781/latest/USD";
using (HttpResponseMessage response = await client.GetAsync(url))
{
using (HttpContent content = response.Content)
{
if (content != null)
{
String json = content.ReadAsStringAsync().Result;
ER_API_Obj obj = JsonConvert.DeserializeObject<ER_API_Obj>(json);
rates = obj.conversion_rates;
Console.WriteLine("Rates updated");
return true;
}
else
{
Console.WriteLine("HttpContent is null");
return false;
}
}
}
}
catch (Exception)
{
return false;
}
}
public double Convert(double value, String code_from, String code_to)
{
code_from = code_from.ToUpper();
code_to = code_to.ToUpper();
double rate1, rate2;
Type typ = rates.GetType();
PropertyInfo? prop = typ.GetProperty(code_from);
if (prop != null)
{
var r1 = prop.GetValue(rates, null);
rate1 = (double)r1;
}
else
{
throw new Exception("Invalid currency code");
}
prop = typ.GetProperty(code_to);
if (prop != null)
{
var r2 = prop.GetValue(rates, null);
rate2 = (double)r2;
}
else
{
throw new Exception("Invalid currency code");
}
double result = value * (rate2 / rate1);
return Math.Round(result, 2);
}
}
}