-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClass1.cs
151 lines (133 loc) · 4.61 KB
/
Class1.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using Newtonsoft.Json;
using totleAPI.totleDecode;
using totleAPI.totlePOSTbodies;
using System.Net;
namespace totleAPI
{
public class totleCore
{
public enum httpTypes
{
POST = 0,
GET = 1,
PUT = 2,
DELETE = 3
}
private const string urlBase = "https://api.totle.com";
private string performTotleCall(string endpoint, List<Tuple<string, string>> queryStr,
List<Tuple<string, string>> headers, httpTypes http_type, object body = null, bool isMultipleSwap = false)
{
try
{
var client = new HttpClient();
client.BaseAddress = new Uri(urlBase);
if (queryStr != null)
{
endpoint = endpoint + "?";
foreach (var qStr in queryStr) endpoint = endpoint + qStr.Item1 + "=" + qStr.Item2 + "&";
var one = endpoint.Length - 1;
endpoint = endpoint.Substring(0, one);
}
if (headers != null)
foreach (var head in headers)
client.DefaultRequestHeaders.Add(head.Item1, head.Item2);
HttpContent httpContent = null;
if (body != null)
{
var inf = JsonConvert.SerializeObject(body);
httpContent = new StringContent(inf);
httpContent.Headers.ContentType.MediaType = "application/json";
}
HttpResponseMessage msg = null;
if (http_type == httpTypes.GET) msg = client.GetAsync(endpoint).Result;
if (http_type == httpTypes.POST) msg = client.PostAsync(endpoint, httpContent).Result;
if (http_type == httpTypes.DELETE) msg = client.DeleteAsync(endpoint).Result;
if (http_type == httpTypes.PUT) msg = client.PutAsync(endpoint, httpContent).Result;
if (msg.IsSuccessStatusCode)
{
var ret = msg.Content.ReadAsStringAsync().Result;
return ret;
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("issue with totle endpoint: " + endpoint);
Console.ForegroundColor = ConsoleColor.White;
return null;
}
catch (Exception i)
{
return null;
}
}
public TotleExchangeObj getExchanges()
{
try
{
var exchs = performTotleCall("/exchanges", null, null, httpTypes.GET);
var exchDecode = exchangesDecode.decode(exchs);
return exchDecode;
}
catch (Exception i)
{
return null;
}
}
public TotleTokenObj getTokens()
{
try
{
var tokens = performTotleCall("/tokens", null, null, httpTypes.GET);
var tokenDecode = tokensDecode.decode(tokens);
return tokenDecode;
}
catch (Exception i)
{
return null;
}
}
public swapReturnObj pushSwap(swapBodyObjMultiple body, string coin)
{
try
{
string f = body.swaps[0].destinationAsset;
var swapRet = performTotleCall("/swap", null, null, httpTypes.POST, body);
swapReturnDecode swap = new swapReturnDecode();
var obj = swap.decode(swapRet);
return obj;
}
catch (Exception i)
{
return null;
}
}
public swapReturnObj pushSwap(swapBodyObjSingle body)
{
try
{
var swapRet = performTotleCall("/swap", null, null, httpTypes.POST, body);
swapReturnDecode swap = new swapReturnDecode();
var obj = swap.decode(swapRet);
return obj;
}
catch (Exception i)
{
return null;
}
}
public TotlePayObj pushPay(payBody body)
{
try
{
var payRet = performTotleCall("/pay", null, null, httpTypes.POST, body);
var obj = payDecode.decode(payRet);
return obj;
}
catch (Exception i)
{
return null;
}
}
}
}