/
WormsClient.cs
436 lines (396 loc) · 18.3 KB
/
WormsClient.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
namespace SpeciesDatabaseApi.MarineSpecies;
/// <summary>
/// The client for https://marinespecies.org API
/// </summary>
public class WormsClient : BaseClient
{
#region Static objects
/// <summary>
/// The client full name/provider
/// </summary>
public const string FullName = "World Register of Marine Species";
/// <summary>
/// The Api default address
/// </summary>
public static readonly Uri DefaultApiAddress = new("https://www.marinespecies.org/rest");
#endregion
#region Properties
/// <inheritdoc />
public override decimal Version => 1;
/// <inheritdoc />
public override string ClientFullName => FullName;
#endregion
#region Constructor
public WormsClient(HttpClient? httpClient = null) : base(DefaultApiAddress, httpClient)
{
}
#endregion
#region Attributes
/// <summary>
/// Get attribute definitions. To refer to root items specify ID = 0
/// </summary>
/// <param name="attributeId">The attribute definition id to search for</param>
/// <param name="includeInherited">Include the tree of children. Default=true</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AttributeKey[]?> GetAttributeKeys(int attributeId = 0, bool includeInherited = true, CancellationToken token = default)
{
var parameters = new QueryParameters("include_children", includeInherited);
return GetJsonAsync<AttributeKey[]>($"AphiaAttributeKeysByID/{attributeId}", parameters, token);
}
/// <summary>
/// Get a list of attributes for a given AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="includeInherited">Include attributes inherited from the taxon its parent(s). Default=false</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<Attribute[]?> GetAttributes(int aphiaId, bool includeInherited = false, CancellationToken token = default)
{
var parameters = new QueryParameters("include_inherited", includeInherited);
return GetJsonAsync<Attribute[]>($"AphiaAttributesByAphiaID/{aphiaId}", parameters, token);
}
/// <summary>
/// Get list values that are grouped by an CategoryID
/// </summary>
/// <param name="categoryId">The CategoryID to search for</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AttributeValue[]?> GetAttributeValues(int categoryId = 0, CancellationToken token = default)
{
return GetJsonAsync<AttributeValue[]>($"AphiaAttributeValuesByCategoryID/{categoryId}", token);
}
/// <summary>
/// Get a list of AphiaIDs (max 50) with attribute tree for a given attribute definition ID
/// </summary>
/// <param name="attributeId">The attribute definition id to search for</param>
/// <param name="offset">Starting record number, when retrieving next chunk of (50) records. Default=1</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaAttributeSet[]?> GetAphiaIdsAndAttributes(int attributeId, int offset = 1, CancellationToken token = default)
{
var parameters = new QueryParameters("offset", offset);
return GetJsonAsync<AphiaAttributeSet[]>($"AphiaIDsByAttributeKeyID/{attributeId}", parameters, token);
}
#endregion
#region Distributions
/// <summary>
/// Get all distributions for a given AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<Distribution[]?> GetDistributions(int aphiaId, CancellationToken token = default)
{
return GetJsonAsync<Distribution[]>($"AphiaDistributionsByAphiaID/{aphiaId}", token);
}
#endregion
#region External identifiers
/// <summary>
/// Get any external identifier(s) for a given AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="type">Type of external identifier to return.</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<string[]?> GetExternalId(int aphiaId, ExternalIdentifierTypeEnum type, CancellationToken token = default)
{
var parameters = new QueryParameters("type", type, valueCase:QueryParameterCase.Lower);
return GetJsonAsync<string[]>($"AphiaExternalIDByAphiaID/{aphiaId}", parameters, token);
}
/// <summary>
/// Get the Aphia Record for a given external identifier
/// </summary>
/// <param name="externalId">The external identifier to search for</param>
/// <param name="type">Type of external identifier to return.</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRecord?> GetRecordByExternalId(string externalId, ExternalIdentifierTypeEnum type, CancellationToken token = default)
{
var parameters = new QueryParameters("type", type, valueCase: QueryParameterCase.Lower);
return GetJsonAsync<AphiaRecord>($"AphiaRecordByExternalID/{externalId}", parameters, token);
}
#endregion
#region Sources
/// <summary>
/// Get one or more sources/references including links, for one AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<Source[]?> GetSources(int aphiaId, CancellationToken token = default)
{
return GetJsonAsync<Source[]>($"AphiaSourcesByAphiaID/{aphiaId}", token);
}
#endregion
#region Taxonomic data
/// <summary>
/// Get the direct children (max. 50) for a given AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="marineOnly">Limit to marine taxa. Default=true</param>
/// <param name="offset">Starting record number, when retrieving next chunk of (50) records. Default=1</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRecord[]?> GetChildren(int aphiaId, bool marineOnly = true, int offset = 1, CancellationToken token = default)
{
var parameters = new QueryParameters
{
{"marine_only", marineOnly},
{"offset", offset},
};
return GetJsonAsync<AphiaRecord[]>($"AphiaChildrenByAphiaID/{aphiaId}", parameters, token);
}
/// <summary>
/// Get the complete classification for one taxon. This also includes any sub or super ranks.
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<Classification?> GetClassification(int aphiaId, CancellationToken token = default)
{
return GetJsonAsync<Classification>($"AphiaClassificationByAphiaID/{aphiaId}", token);
}
/// <summary>
/// Get the AphiaID for a given name.
/// </summary>
/// <param name="scientificName">Name to search for</param>
/// <param name="marineOnly">Limit to marine taxa. Default=true</param>
/// <param name="token"></param>
/// <returns>NULL when no match is found; -999 when multiple matches are found; an integer(AphiaID) when one exact match was found</returns>
public Task<int?> GetAphiaId(string scientificName, bool marineOnly = true, CancellationToken token = default)
{
var parameters = new QueryParameters("marine_only", marineOnly);
return GetJsonAsync<int?>($"AphiaIDByName/{Uri.EscapeDataString(scientificName)}", parameters, token);
}
/// <summary>
/// Get the name for a given AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<string?> GetAphiaName(int aphiaId, CancellationToken token = default)
{
return GetJsonAsync<string>($"AphiaNameByAphiaID/{aphiaId}", token);
}
/// <summary>
/// Get the complete AphiaRecord for a given AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRecord?> GetRecord(int aphiaId, CancellationToken token = default)
{
return GetJsonAsync<AphiaRecord>($"AphiaRecordByAphiaID/{aphiaId}", token);
}
/// <summary>
/// Returns the linked open data implementation of an AphiaRecord for a given AphiaID.<br/>
/// There are multiple formats available that can be chosen with the Accept header. The default format is JSON-LD but RDF, Turtle and N-triples are also supported.
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<JsonNode?> GetFullRecord(int aphiaId, CancellationToken token = default)
{
// TODO: Implement this with DTO
return GetJsonAsync<JsonNode>($"AphiaRecordFullByAphiaID/{aphiaId}", token);
}
/// <summary>
/// Get an AphiaRecord for multiple AphiaIDs in one go (max: 50)
/// </summary>
/// <param name="aphiaIds">The AphiaIDs to search for</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRecord[]?> GetRecords(IEnumerable<int> aphiaIds, CancellationToken token = default)
{
var parameters = new QueryParameters("aphiaids[]", aphiaIds);
return GetJsonAsync<AphiaRecord[]>("AphiaRecordsByAphiaIDs", parameters, token);
}
/// <summary>
/// Lists all AphiaRecords (taxa) that have their last edit action (modified or added) during the specified period
/// </summary>
/// <param name="startDate">Start date time to lookup</param>
/// <param name="endDate">End date time to lookup</param>
/// <param name="marineOnly">Limit to marine taxa. Default=true</param>
/// <param name="offset">Starting record number, when retrieving next chunk of (50) records. Default=1</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRecord[]?> GetRecords(DateTime startDate, DateTime endDate, bool marineOnly = true, int offset = 1, CancellationToken token = default)
{
var parameters = new QueryParameters
{
{"startdate", startDate.ToString("O")},
{"enddate", endDate.ToString("O")},
{"marine_only", marineOnly},
{"offset", offset}
};
return GetJsonAsync<AphiaRecord[]>("AphiaRecordsByDate", parameters, token);
}
/// <summary>
/// For each given scientific name (may include authority), try to find one or more AphiaRecords, using the TAXAMATCH fuzzy matching algorithm by Tony Rees.<br/>
/// This allows you to(fuzzy) match multiple names in one call.<br/>
/// Limited to 50 names at once for performance reasons
/// </summary>
/// <param name="scientificNames">Names to search for</param>
/// <param name="marineOnly">Limit to marine taxa. Default=true</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<List<AphiaRecord[]>?> GetRecordsFuzzy(IEnumerable<string> scientificNames, bool marineOnly = true, CancellationToken token = default)
{
var parameters = new QueryParameters
{
{"scientificnames[]", scientificNames},
{"marine_only", marineOnly}
};
return GetJsonAsync<List<AphiaRecord[]>>("AphiaRecordsByMatchNames", parameters, token);
}
/// <summary>
/// Get one or more matching (max. 50) AphiaRecords for a given name
/// </summary>
/// <param name="scientificName">Name to search for</param>
/// <param name="like">Add a '%'-sign added after the ScientificName (SQL LIKE function). Default=true</param>
/// <param name="marineOnly">Limit to marine taxa. Default=true</param>
/// <param name="offset">Starting record number, when retrieving next chunk of (50) records. Default=1</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRecord[]?> GetRecords(string scientificName, bool like = true, bool marineOnly = true, int offset = 1, CancellationToken token = default)
{
var parameters = new QueryParameters
{
{"like", like},
{"marine_only", marineOnly},
{"offset", offset},
};
return GetJsonAsync<AphiaRecord[]>($"AphiaRecordsByName/{EscapeDataString(scientificName)}", parameters, token);
}
/// <summary>
/// For each given scientific name, try to find one or more AphiaRecords.<br/>
/// This allows you to match multiple names in one call.<br/>
/// Limited to 500 names at once for performance reasons.
/// </summary>
/// <param name="scientificNames">Names to search for</param>
/// <param name="like">Add a '%'-sign after the ScientificName (SQL LIKE function). Default=false</param>
/// <param name="marineOnly">Limit to marine taxa. Default=true</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<List<AphiaRecord[]>?> GetRecords(IEnumerable<string> scientificNames, bool like = false, bool marineOnly = true, CancellationToken token = default)
{
var parameters = new QueryParameters
{
{"scientificnames[]", scientificNames},
{"like", like},
{"marine_only", marineOnly}
};
return GetJsonAsync<List<AphiaRecord[]>>("AphiaRecordsByNames", parameters, token);
}
/// <summary>
/// Get the AphiaRecords for a given taxonRankID (max 50)
/// </summary>
/// <param name="taxonId">A taxonomic rank identifier</param>
/// <param name="belongsToAphiaId">Limit the results to descendants of the given AphiaID</param>
/// <param name="offset">Starting record number, when retrieving next chunk of (50) records. Default=1</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRecord[]?> GetRecords(int taxonId, int? belongsToAphiaId, int offset = 1, CancellationToken token = default)
{
var parameters = new QueryParameters
{
{"belongsTo", belongsToAphiaId},
{"offset", offset},
};
return GetJsonAsync<AphiaRecord[]>($"AphiaRecordsByTaxonRankID/{taxonId}", parameters, token);
}
/// <summary>
/// Get all synonyms for a given AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="offset">Starting record number, when retrieving next chunk of (50) records. Default=1</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRecord[]?> GetSynonyms(int aphiaId, int offset = 1, CancellationToken token = default)
{
var parameters = new QueryParameters("offset", offset);
return GetJsonAsync<AphiaRecord[]>($"AphiaSynonymsByAphiaID/{aphiaId}", parameters, token);
}
/// <summary>
/// Get taxonomic ranks by their identifier
/// </summary>
/// <param name="taxonId">A taxonomic rank identifier. Default=-1</param>
/// <param name="aphiaId">The AphiaID of the kingdom. Default=-1</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRank[]?> GetTaxonRanks(int taxonId = -1, int aphiaId = -1, CancellationToken token = default)
{
var parameters = new QueryParameters("AphiaID", aphiaId);
return GetJsonAsync<AphiaRank[]>($"AphiaTaxonRanksByID/{taxonId}", parameters, token);
}
/// <summary>
/// Get taxonomic ranks by their name
/// </summary>
/// <param name="taxonRank">A taxonomic rank. Default=empty</param>
/// <param name="aphiaId">The AphiaID of the kingdom. Default=-1</param>
/// <param name="token"></param>
/// <returns></returns>
public Task<AphiaRank[]?> GetTaxonRanks(string taxonRank = "", int aphiaId = -1, CancellationToken token = default)
{
var parameters = new QueryParameters("AphiaID", aphiaId);
return GetJsonAsync<AphiaRank[]>($"AphiaTaxonRanksByName/{Uri.EscapeDataString(taxonRank)}", parameters, token);
}
#endregion
#region Vernaculars
/// <summary>
/// Get one or more Aphia Records (max. 50) for a given vernacular
/// </summary>
/// <param name="vernacular">The vernacular to find records for</param>
/// <param name="like">Add a '%'-sign before and after the input (SQL LIKE '%vernacular%' function)</param>
/// <param name="offset">Starting record number, when retrieving next chunk of (50) records</param>
/// <param name="token"></param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<AphiaRecord[]?> GetRecordsByVernacular(string vernacular, bool like = false, int offset = 1, CancellationToken token = default)
{
var parameters = new QueryParameters
{
{nameof(like), like},
{nameof(offset), offset},
};
return GetJsonAsync<AphiaRecord[]>($"AphiaRecordsByVernacular/{EscapeDataString(vernacular)}", parameters, token);
}
/// <summary>
/// Get all vernaculars for a given AphiaID
/// </summary>
/// <param name="aphiaId">The AphiaID to search for</param>
/// <param name="token"></param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<VernacularItem[]?> GetVernaculars(int aphiaId, CancellationToken token = default)
{
return GetJsonAsync<VernacularItem[]>($"AphiaVernacularsByAphiaID/{aphiaId}", token);
}
#endregion
#region Static Methods
/// <summary>
/// Gets the link for an aphia based on aphia name<br/>
/// (This assumes that you have no idea if the taxon exists, or you do not want to look up the AphiaID)
/// </summary>
/// <param name="aphiaName">The aphia name to look for</param>
/// <returns></returns>
public static string GetAphiaLink(string aphiaName)
{
return $"https://www.marinespecies.org/aphia.php?p=taxlist&tName={EscapeDataString(aphiaName)}";
}
/// <summary>
/// Gets the link for an aphia based on aphiaId
/// </summary>
/// <param name="aphiaId">The aphiaId to look for</param>
/// <returns></returns>
public static string GetAphiaLink(int aphiaId)
{
return $"https://www.marinespecies.org/aphia.php?p=taxdetails&id={aphiaId}";
}
#endregion
}