forked from CarlHalstead/TornCityAPISharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTornApiWrapper.cs
243 lines (208 loc) · 7.9 KB
/
TornApiWrapper.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Threading.Tasks;
using TornCityAPISharp.Utils;
using TornCityAPISharp.Enums;
using TornCityAPISharp.FactionStats;
using TornCityAPISharp.PropertyStats;
using TornCityAPISharp.CompanyStats;
using TornCityAPISharp.MarketStats;
using TornCityAPISharp.UserStats;
using TornCityAPISharp.TornStats;
namespace TornCityAPISharp
{
/// <summary>
/// Wrapper class that does all the Torn api calls
/// </summary>
public class TornApiWrapper
{
#region Accessors
/// <summary>
/// Maximum number of calls that can be used;
/// </summary>
public int MaxNumberOfCalls { get; set; }
/// <summary>
/// Object that keeps track of the api key beeing used
/// </summary>
public API ApiKey
{
get
{
return _apiKey;
}
private set
{
this._apiKey = value;
}
}
#endregion
#region Mutators
private API _apiKey;
private GetObjectFromUrl apiCaller = new GetObjectFromUrl();
private string urlBase = @"https://api.torn.com/";
#endregion
#region Constructors
/// <summary>
/// Create a TornWrapper instance
/// </summary>
/// <param name="apiKey">Api to use</param>
/// <param name="maxNumberOfCalls">Number of call limit (Throws error when going over this amount).</param>
public TornApiWrapper(string apiKey, int maxNumberOfCalls)
{
ApiKey = new API(apiKey);
MaxNumberOfCalls = maxNumberOfCalls;
}
/// <summary>
/// Create a TornWrapper instance. That does not limit the amount of calls
/// </summary>
/// <param name="apiKey">Api to use</param>
public TornApiWrapper(string apiKey)
{
ApiKey = new API(apiKey);
MaxNumberOfCalls = int.MaxValue;
}
#endregion
#region Methods
/// <summary>
/// Call Methods from the Faction APi
/// </summary>
/// <typeparam name="T">Object that implements IFactionStatistics from the TornCityAPISharp.FactionStats namespace</typeparam>
/// <param name="factionID">Id of the faction or "" for user faction</param>
/// <returns>Object containing response of type T</returns>
public async Task<T> GetFromFactionApi<T>(string factionID) where T:IFactionStatistics,new()
{
var method = new T();
string url = urlBase + Fields.faction.ToString() + "/" + factionID + "?selections="+ method.GetMethodName() +"&key=" + _apiKey.ApiKey;
try
{
var response = await DoCall<T>(url);
return response;
}
catch(Exception e)
{
throw e;
}
}
/// <summary>
/// Call Methods from the property APi
/// </summary>
/// <typeparam name="T">object implementing IPropertyStatistics interface from the TornCityAPISharp.PropertyStats namespace</typeparam>
/// <param name="propertyId">A Property Id</param>
/// <returns>Object of type T</returns>
public async Task<T> GetFromPropertyApi<T>(string propertyId) where T : IPropertyStatistics, new()
{
var method = new T();
if(string.IsNullOrWhiteSpace(propertyId) == true)
{
throw new ArgumentException("Property id cannot be null of whitespace");
}
string url = urlBase + Fields.property.ToString() + "/" + propertyId + "?selections=" + method.GetMethodName() + "&key=" + _apiKey.ApiKey;
try
{
var response = await DoCall<T>(url);
return response;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// Call Methods from the company APi
/// </summary>
/// <typeparam name="T">object implementing ICompanyStatistics interface from the TornCityAPISharp.CompanyStats namespace</typeparam>
/// <param name="companyId">A company Id</param>
/// <returns>Object of type T</returns>
public async Task<T> GetFromCompanyApi<T>(string companyId) where T : ICompanyStatistics, new()
{
var method = new T();
string url = urlBase + Fields.company.ToString() + "/" + companyId + "?selections=" + method.GetMethodName() + "&key=" + _apiKey.ApiKey;
try
{
var response = await DoCall<T>(url);
return response;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// Call Methods from the market APi
/// </summary>
/// <typeparam name="T">object implementing IMarketStatistics interface from the TornCityAPISharp.MarketStats namespace</typeparam>
/// <param name="itemId">An item Id</param>
/// <returns>Object of type T</returns>
public async Task<T> GetFromMarketApi<T>(string itemId) where T : IMarketStatistics, new()
{
var method = new T();
string url = urlBase + Fields.market.ToString() + "/" + itemId + "?selections=" + method.GetMethodName() + "&key=" + _apiKey.ApiKey;
try
{
var response = await DoCall<T>(url);
return response;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// Call Methods from the user APi
/// </summary>
/// <typeparam name="T">object implementing IUserStatistics interface from the TornCityAPISharp.UserStats namespace</typeparam>
/// <param name="userId">A porperty Id</param>
/// <returns>Object of type T</returns>
public async Task<T> GetFromUserApi<T>(string userId = null) where T : IUserStatistics, new()
{
var method = new T();
string url = urlBase + Fields.user.ToString() + "/" + userId + "?selections=" + method.GetMethodName() + "&key=" + _apiKey.ApiKey;
try
{
var response = await DoCall<T>(url);
return response;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// Call Methods from the torn APi
/// </summary>
/// <typeparam name="T">object implementing ITornStats interface from the TornCityAPISharp.TornStats namespace</typeparam>
/// <param name="parameter">Could be an item number, honor number depending on the api that is beeing called</param>
/// <returns>Object of type T</returns>
public async Task<T> GetFromTornApi<T>(string parameter) where T : ITornStats, new()
{
var method = new T();
string url = urlBase + Fields.torn.ToString() + "/" + parameter + "?selections=" + method.GetMethodName() + "&key=" + _apiKey.ApiKey;
try
{
var response = await DoCall<T>(url);
return response;
}
catch (Exception e)
{
throw e;
}
}
private async Task<T> DoCall<T>(string url)
{
if (_apiKey.GetApiCount() > MaxNumberOfCalls)
{
throw new TornApiToManyRequestException("To many calls have been made with this api key maximum amount: " + this.MaxNumberOfCalls.ToString());
}
try
{
var response = await apiCaller.GetObject<T>(url);
_apiKey.AddApiCount();
return response;
}
catch(Exception e)
{
throw e;
}
}
#endregion
}
}