-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
64 lines (58 loc) · 2.25 KB
/
Program.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
using CommandLine;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
namespace MyAppConsole
{
class Program
{
static void Main(string[] args)
{
Parser.Default.ParseArguments<CommandLineOptions>(args).WithParsed(o =>
{
var returnObject = new ReturnObject();
try
{
Console.WriteLine("API import starting...");
var results = GetDataFromAPI(o.ApiKey, o.Symbol);
// var values = JsonConvert.DeserializeObject<TypeResults>(results);
// SaveValuesInDatabase(values);
Console.WriteLine("API import ended successfully");
} catch (Exception e)
{
returnObject.Error = -1;
returnObject.ErrorMessage = e.Message;
returnObject.StackTrace = e.StackTrace;
Console.Write(JsonConvert.SerializeObject(returnObject));
}
});
}
private static string GetDataFromAPI(string apiKey, string symbol)
{
var parameters = new Dictionary<string, string>() {
{ "symbol", symbol },
{ "interval", "1d" }
};
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("APIKEYPARAM", apiKey);
var baseUrl = "https://api.binance.com/api/v3/klines";
var url = baseUrl + "?" + string.Join("&", parameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)));
var task = client.GetAsync(url);
task.Wait();
if (!task.Result.IsSuccessStatusCode)
{
throw new Exception(task.Result.StatusCode.ToString());
}
var readTask = task.Result.Content.ReadAsStringAsync();
readTask.Wait();
return readTask.Result;
}
}
}
}