5
5
using Newtonsoft . Json ;
6
6
using TwelveDataSharp . Models ;
7
7
using TwelveDataSharp . DataModels ;
8
+ using TwelveDataSharp . Interfaces ;
8
9
9
10
/*
10
11
* TwelveDataSharp - a .NET Standard 2.0 library for accessing stock market data from Twelve Data
14
15
15
16
namespace TwelveDataSharp
16
17
{
17
- public class TwelveDataClient
18
+ public class TwelveDataClient : ITwelveDataClient
18
19
{
19
- private string apiKey = "" ;
20
+ private string _apiKey = string . Empty ;
21
+ private readonly HttpClient _client ;
20
22
21
- public TwelveDataClient ( string key )
23
+ public TwelveDataClient ( string key , HttpClient client )
22
24
{
23
- apiKey = key ;
25
+ _apiKey = key ;
26
+ _client = client ;
24
27
}
25
28
26
29
/*
@@ -33,9 +36,8 @@ public async Task<TwelveDataQuote> GetQuoteAsync(string symbol, string interval
33
36
{
34
37
try
35
38
{
36
- var client = new HttpClient ( ) ;
37
- string endpoint = "https://api.twelvedata.com/quote?symbol=" + symbol + "&interval=" + interval + "&apikey=" + apiKey ;
38
- var response = await client . GetAsync ( endpoint ) ;
39
+ string endpoint = "https://api.twelvedata.com/quote?symbol=" + symbol + "&interval=" + interval + "&apikey=" + _apiKey ;
40
+ var response = await _client . GetAsync ( endpoint ) ;
39
41
string responseString = await response . Content . ReadAsStringAsync ( ) ;
40
42
var jsonResponse = JsonConvert . DeserializeObject < TwelveDataSharp . Models . TimeSeriesQuote > ( responseString ) ;
41
43
TwelveDataQuote quote = new TwelveDataQuote ( )
@@ -78,9 +80,8 @@ public async Task<TwelveDataPrice> GetRealTimePriceAsync(string symbol)
78
80
{
79
81
try
80
82
{
81
- var client = new HttpClient ( ) ;
82
- string endpoint = "https://api.twelvedata.com/price?symbol=" + symbol + "&apikey=" + apiKey ;
83
- var response = await client . GetAsync ( endpoint ) ;
83
+ string endpoint = "https://api.twelvedata.com/price?symbol=" + symbol + "&apikey=" + _apiKey ;
84
+ var response = await _client . GetAsync ( endpoint ) ;
84
85
string responseString = await response . Content . ReadAsStringAsync ( ) ;
85
86
var jsonResponse = JsonConvert . DeserializeObject < TwelveDataSharp . Models . TimeSeriesRealTimePrice > ( responseString ) ;
86
87
TwelveDataPrice realTimePrice = new TwelveDataPrice ( )
@@ -108,9 +109,8 @@ public async Task<TwelveDataTimeSeries> GetTimeSeriesAsync(string symbol, string
108
109
{
109
110
try
110
111
{
111
- var client = new HttpClient ( ) ;
112
- string endpoint = "https://api.twelvedata.com/time_series?symbol=" + symbol + "&interval=" + interval + "&apikey=" + apiKey ;
113
- var response = await client . GetAsync ( endpoint ) ;
112
+ string endpoint = "https://api.twelvedata.com/time_series?symbol=" + symbol + "&interval=" + interval + "&apikey=" + _apiKey ;
113
+ var response = await _client . GetAsync ( endpoint ) ;
114
114
string responseString = await response . Content . ReadAsStringAsync ( ) ;
115
115
var jsonResponse = JsonConvert . DeserializeObject < TwelveDataSharp . Models . TimeSeriesStocks > ( responseString ) ;
116
116
List < TimeSeriesValues > values = new List < TimeSeriesValues > ( ) ;
@@ -160,8 +160,8 @@ public async Task<TwelveDataTimeSeriesAverage> GetTimeSeriesAverageAsync(string
160
160
try
161
161
{
162
162
var client = new HttpClient ( ) ;
163
- string endpoint = "https://api.twelvedata.com/avg?symbol=" + symbol + "&interval=" + interval + "&apikey=" + apiKey ;
164
- var response = await client . GetAsync ( endpoint ) ;
163
+ string endpoint = "https://api.twelvedata.com/avg?symbol=" + symbol + "&interval=" + interval + "&apikey=" + _apiKey ;
164
+ var response = await _client . GetAsync ( endpoint ) ;
165
165
string responseString = await response . Content . ReadAsStringAsync ( ) ;
166
166
var jsonResponse = JsonConvert . DeserializeObject < TwelveDataSharp . Models . TechnicalIndicatorAvg > ( responseString ) ;
167
167
List < TimeSeriesAverages > values = new List < TimeSeriesAverages > ( ) ;
@@ -211,9 +211,8 @@ public async Task<TwelveDataAdx> GetAdxValuesAsync(string symbol, string interva
211
211
{
212
212
try
213
213
{
214
- var client = new HttpClient ( ) ;
215
- string endpoint = "https://api.twelvedata.com/adx?symbol=" + symbol + "&interval=" + interval + "&apikey=" + apiKey ;
216
- var response = await client . GetAsync ( endpoint ) ;
214
+ string endpoint = "https://api.twelvedata.com/adx?symbol=" + symbol + "&interval=" + interval + "&apikey=" + _apiKey ;
215
+ var response = await _client . GetAsync ( endpoint ) ;
217
216
string responseString = await response . Content . ReadAsStringAsync ( ) ;
218
217
var jsonResponse = JsonConvert . DeserializeObject < TwelveDataSharp . Models . TechnicalIndicatorAdx > ( responseString ) ;
219
218
List < AdxValues > values = new List < AdxValues > ( ) ;
@@ -262,9 +261,8 @@ public async Task<TwelveDataBollingerBands> GetBollingerBands(string symbol, str
262
261
{
263
262
try
264
263
{
265
- var client = new HttpClient ( ) ;
266
- string endpoint = "https://api.twelvedata.com/bbands?symbol=" + symbol + "&interval=" + interval + "&apikey=" + apiKey ;
267
- var response = await client . GetAsync ( endpoint ) ;
264
+ string endpoint = "https://api.twelvedata.com/bbands?symbol=" + symbol + "&interval=" + interval + "&apikey=" + _apiKey ;
265
+ var response = await _client . GetAsync ( endpoint ) ;
268
266
string responseString = await response . Content . ReadAsStringAsync ( ) ;
269
267
var jsonResponse = JsonConvert . DeserializeObject < TwelveDataSharp . Models . TechnicalIndicatorBbands > ( responseString ) ;
270
268
List < BollingerBandValue > values = new List < BollingerBandValue > ( ) ;
0 commit comments