forked from ontehfritz/CSharpMtgDb.Info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDb.cs
357 lines (305 loc) · 11.7 KB
/
Db.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System;
using System.Net;
using Newtonsoft.Json;
using MtgDb.Info;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace MtgDb.Info.Driver
{
public class Db
{
public string ApiUrl { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="MtgDb.Info.Driver.Db"/> class.
/// </summary>
public Db ()
{
ApiUrl = "https://api.mtgdb.info";
}
/// <summary>
/// Initializes a new instance of the <see cref="Mtgdb.Info.Wrapper.Db"/> class.
/// Only use this method if you running a local version MtgDB api.
/// </summary>
/// <param name="url">Custom Url if not using: "http://api.mtgdb.info";</param>
public Db(string url)
{
ApiUrl = url;
}
public string[] GetCardRarityTypes()
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/cards/rarity", this.ApiUrl);
var json = client.DownloadString(url);
string [] types = JsonConvert.DeserializeObject<string[]>(json);
return types;
}
}
public string[] GetCardTypes()
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/cards/types", this.ApiUrl);
var json = client.DownloadString(url);
string [] types = JsonConvert.DeserializeObject<string[]>(json);
return types;
}
}
public string[] GetCardSubTypes()
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/cards/subtypes", this.ApiUrl);
var json = client.DownloadString(url);
string [] types = JsonConvert.DeserializeObject<string[]>(json);
return types;
}
}
/// <summary>
/// Get a card by multiverse Id
/// </summary>
/// <returns>A card</returns>
/// <param name="id">Multiverse Id</param>
public Card GetCard(int id)
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/cards/{1}", this.ApiUrl, id.ToString());
var json = client.DownloadString(url);
Card card = JsonConvert.DeserializeObject<Card>(json);
return card;
}
}
/// <summary>
/// Gets the random card.
/// </summary>
/// <returns>The random card.</returns>
public Card GetRandomCard()
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/cards/random", this.ApiUrl);
var json = client.DownloadString(url);
Card card = JsonConvert.DeserializeObject<Card>(json);
return card;
}
}
/// <summary>
/// Gets the random card in set.
/// </summary>
/// <returns>The random card in set.</returns>
/// <param name="setId">Set identifier.</param>
public Card GetRandomCardInSet(string setId)
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/sets/{1}/cards/random", this.ApiUrl,setId);
var json = client.DownloadString(url);
Card card = JsonConvert.DeserializeObject<Card>(json);
return card;
}
}
/// <summary>
/// Gets all sets on mtgdb.info
/// </summary>
/// <returns>Mtg Sets</returns>
/// <param name="setIds">3 character identifier</param>
public CardSet[] GetSets(string [] setIds)
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/sets/{1}", this.ApiUrl,
string.Join(",", setIds));
var json = client.DownloadString(url);
List<CardSet> sets = new List<CardSet>();
if(setIds.Length == 1)
{
sets.Add (JsonConvert.DeserializeObject<CardSet>(json));
}
else
{
sets.AddRange (JsonConvert.DeserializeObject<CardSet[]>(json));
}
return sets.ToArray();
}
}
/// <summary>
/// Get multiple cards by multiverse Id.
/// </summary>
/// <returns>return an array of Card objects</returns>
/// <param name="multiverseIds">Multiverse identifiers.</param>
public Card[] GetCards(int [] multiverseIds)
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/cards/{1}", this.ApiUrl,
string.Join(",", multiverseIds));
var json = client.DownloadString(url);
List<Card> cards = new List<Card>();
if(multiverseIds.Length == 1)
{
cards.Add (JsonConvert.DeserializeObject<Card>(json));
}
else
{
cards.AddRange (JsonConvert.DeserializeObject<Card[]>(json));
}
return cards.ToArray();
}
}
/// <summary>
/// Returns all prints of a card by name
/// </summary>
/// <returns>Array of Card Objects</returns>
/// <param name="name">Name of the card</param>
public Card[] GetCards(string name)
{
if(name == null || name == "")
{
throw new ArgumentException("Cannot be null or blank", "name");
}
using (var client = new WebClient())
{
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
name = rgx.Replace(name, "");
string url = string.Format ("{0}/cards/{1}", this.ApiUrl, name);
var json = client.DownloadString(url);
Card[] cards = null;
try
{
cards = JsonConvert.DeserializeObject<Card[]>(json);
}
catch(Exception e)
{
cards = null;
}
return cards;
}
}
/// <summary>
/// Gets the entire card database. Useful creating a local copy. This method may take some time to run
/// </summary>
/// <returns>Array of Card objects</returns>
public Card[] GetCards()
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/cards/", this.ApiUrl);
var json = client.DownloadString(url);
Card[] cards = JsonConvert.DeserializeObject<Card[]>(json);
return cards;
}
}
/// <summary>
/// Filters the cards by Card field
/// </summary>
/// <returns>Array of Card objects.</returns>
/// <param name="property">The field name you want to fileter by</param>
/// <param name="value">the value of the field to match</param>
public Card[] FilterCards(string property, string value)
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/cards/?{1}={2}", this.ApiUrl, property, value);
var json = client.DownloadString(url);
Card[] cards = JsonConvert.DeserializeObject<Card[]>(json);
return cards;
}
}
/// <summary>
/// Gets cards in a set. You can use start and end to page through the card sets. This uses the card number.
/// </summary>
/// <returns>Array of Card objects.</returns>
/// <param name="setId">Set identifier.</param>
/// <param name="start">Optional: which card number to start at</param>
/// <param name="end">Optional: which card to retrieve up to and including</param>
public Card[] GetSetCards(string setId, int start = 0, int end = 0)
{
using (var client = new WebClient())
{
string url = null;
if(start > 0 || end > 0)
{
url = string.Format ("{0}/sets/{1}/cards/?start={2}&end={3}",
this.ApiUrl, setId, start, end);
}
else
{
url = string.Format ("{0}/sets/{1}/cards/", this.ApiUrl, setId);
}
var json = client.DownloadString(url);
Card[] cards = JsonConvert.DeserializeObject<Card[]>(json);
return cards;
}
}
public Card GetCardInSet(string setId, int setNumber)
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/sets/{1}/cards/{2}", this.ApiUrl,
setId, setNumber.ToString());
var json = client.DownloadString(url);
Card card = JsonConvert.DeserializeObject<Card>(json);
return card;
}
}
/// <summary>
/// Gets the set.
/// </summary>
/// <returns>A CardSet Object</returns>
/// <param name="setId">Set identifier.</param>
public CardSet GetSet(string setId)
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/sets/{1}", this.ApiUrl, setId);
var json = client.DownloadString(url);
CardSet set = JsonConvert.DeserializeObject<CardSet>(json);
return set;
}
}
/// <summary>
/// Gets all sets.
/// </summary>
/// <returns>An array of CardSet Objects.</returns>
public CardSet[] GetSets()
{
using (var client = new WebClient())
{
string url = string.Format ("{0}/sets/", this.ApiUrl);
var json = client.DownloadString(url);
CardSet[] sets = JsonConvert.DeserializeObject<CardSet[]>(json);
return sets;
}
}
/// <summary>
/// Search the for the specified test in the serach name field.
/// It will strip out all characters that are not alpha-numeric.
/// </summary>
/// <param name="text">An array of Card objects</param>
public Card[] Search(string text, int start = 0, int limit = 0, bool isComplex = false)
{
using (var client = new WebClient())
{
if(isComplex)
{
string url = string.Format ("{0}/search/?q={1}&start={2}&limit={3}",
this.ApiUrl, Uri.EscapeDataString(text),start,limit);
var json = client.DownloadString(url);
Card[] cards = JsonConvert.DeserializeObject<Card[]>(json);
return cards;
}
else
{
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
text = rgx.Replace(text, "");
string url = string.Format ("{0}/search/{1}?start={2}&limit={3}",
this.ApiUrl, text, start, limit);
var json = client.DownloadString(url);
Card[] cards = JsonConvert.DeserializeObject<Card[]>(json);
return cards;
}
}
}
}
}