Skip to content

Commit 9f730ff

Browse files
committed
Added CryptoStatsSource project documentation
1 parent 0618730 commit 9f730ff

File tree

5 files changed

+81
-18
lines changed

5 files changed

+81
-18
lines changed

CryptoStatsSource/CoingeckoSource.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,27 @@ namespace CryptoStatsSource
1010
{
1111
public class CoingeckoSource : ICryptoStatsSource
1212
{
13+
// coingecko URL
1314
private const string BaseUrl = "https://api.coingecko.com/api/";
15+
16+
// API version
1417
private const string ApiVersion = "v3";
1518

19+
// initialise a HTTP client
1620
private readonly TinyRestClient _client = new(new HttpClient(), $"{BaseUrl}{ApiVersion}/");
1721

1822
public CoingeckoSource()
1923
{
24+
// set JSON attribute name formatting to snake case
2025
_client.Settings.Formatters.OfType<JsonFormatter>().First().UseSnakeCase();
2126
}
2227

28+
// make a call to the coins/markets API endpoint
2329
public async Task<List<MarketEntry>> GetMarketEntries(string currency, params string[] ids) => await _client
2430
.GetRequest("coins/markets").AddQueryParameter("vs_currency", currency)
2531
.AddQueryParameter("ids", String.Join(",", ids)).ExecuteAsync<List<MarketEntry>>();
2632

33+
// make a call to the coins/list API endpoint
2734
public async Task<List<Cryptocurrency>> GetAvailableCryptocurrencies() => await _client
2835
.GetRequest("coins/list").ExecuteAsync<List<Cryptocurrency>>();
2936
}

CryptoStatsSource/CryptoNameResolver.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,48 @@ namespace CryptoStatsSource
77
{
88
public interface ICryptocurrencyResolver
99
{
10+
/// <summary>
11+
/// Resolves a cryptocurrency based on the given cryptocurrency symbol. Returns null value when the symbol does
12+
/// not map to any cryptocurrency.
13+
/// </summary>
14+
/// <param name="symbol">Symbol based on which the cryptocurrency should be resolved</param>
15+
/// <returns>A task containing resolved cryptocurrency when finished</returns>
1016
public Task<Cryptocurrency> Resolve(string symbol);
1117

18+
/// <summary>
19+
/// Refreshes the database of cryptocurrencies mapped to their symbols
20+
/// </summary>
21+
/// <returns>A task that is finished when all cryptocurrencies are fetched and mapped to their symbols</returns>
1222
public Task Refresh();
1323
}
1424

1525
public class CryptocurrencyResolverImpl : ICryptocurrencyResolver
1626
{
27+
// used for retrieving cryptocurrency info
1728
private ICryptoStatsSource _cryptoStatsSource;
29+
30+
// a dictionary mapping symbols to cryptocurrencies
1831
private Dictionary<String, Cryptocurrency> _nameToCryptocurrencyDictionary;
1932

33+
/// <param name="cryptoStatsSource">CryptoStatsSource interface to be used</param>
2034
public CryptocurrencyResolverImpl(ICryptoStatsSource cryptoStatsSource)
2135
{
2236
_cryptoStatsSource = cryptoStatsSource;
2337
}
2438

2539
public async Task Refresh()
2640
{
27-
// TODO improve this
41+
// initialize the dictionary
2842
_nameToCryptocurrencyDictionary = new();
43+
44+
// fetch all cryptocurrencies and add them to the dictionary using the symbol as a key
2945
(await _cryptoStatsSource.GetAvailableCryptocurrencies()).ForEach(c =>
3046
_nameToCryptocurrencyDictionary.TryAdd(c.Symbol, c));
3147
}
3248

3349
public async Task<Cryptocurrency> Resolve(string symbol)
3450
{
51+
// refresh the dictionary if the symbol was not found in it
3552
if (_nameToCryptocurrencyDictionary?.GetValueOrDefault(symbol) == null) await Refresh();
3653

3754
return _nameToCryptocurrencyDictionary.GetValueOrDefault(symbol, null);

CryptoStatsSource/CryptoStatsSource.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ namespace CryptoStatsSource
66
{
77
public interface ICryptoStatsSource
88
{
9+
/// <summary>
10+
/// Gets all market entries matching the given IDs, price values are relative to the currency specified.
11+
/// </summary>
12+
/// <param name="currency">Code ("usd", "eur", "czk",...) of the currency the market entry prices should be relative to</param>
13+
/// <param name="ids">IDs of the market entries to be fetched</param>
14+
/// <returns>List of loaded market entries</returns>
915
public Task<List<MarketEntry>> GetMarketEntries(string currency, params string[] ids);
1016

17+
/// <summary>
18+
/// Gets a list of all available cryptocurrencies
19+
/// </summary>
20+
/// <returns>List of all available cryptocurrencies</returns>
1121
public Task<List<Cryptocurrency>> GetAvailableCryptocurrencies();
1222
}
1323
}

CryptoStatsSource/Model/MarketEntry.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Newtonsoft.Json;
2+
3+
namespace CryptoStatsSource.model
4+
{
5+
/// <summary>
6+
/// A record representing a market entry (market evaluation of a given cryptocurrency)
7+
/// </summary>
8+
/// <param name="Id">
9+
/// Id of the market entry
10+
/// </param>
11+
/// <param name="Symbol">
12+
/// Symbol of the market entry
13+
/// </param>
14+
/// <param name="CurrentPrice">
15+
/// Price of the entry evaluated at the time of creation
16+
/// </param>
17+
/// <param name="MarketCap">
18+
/// Market cap of the entry
19+
/// </param>
20+
/// <param name="PriceChange24H">
21+
/// Price change of the entry in the last 24 hours
22+
/// </param>>
23+
/// <param name="PriceChangePercentage24H">
24+
/// Relative price change of the entry in the last 24 hours
25+
/// </param>
26+
public record MarketEntry(string Id, string Symbol, string Name, decimal CurrentPrice, long MarketCap,
27+
[JsonProperty("price_change_24h", NullValueHandling = NullValueHandling.Ignore)]
28+
float? PriceChange24H = 0f,
29+
[JsonProperty("price_change_percentage_24h", NullValueHandling = NullValueHandling.Ignore)]
30+
float? PriceChangePercentage24H = 0f
31+
);
32+
33+
/// <summary>
34+
/// A record containing a basic information about a cryptocurrency
35+
/// </summary>
36+
/// <param name="Id">
37+
/// ID of the cryptocurrency
38+
/// </param>
39+
/// <param name="Symbol">
40+
/// Symbol of the cryptocurrency
41+
/// </param>
42+
/// <param name="Name">
43+
/// Name of the cryptocurrency
44+
/// </param>
45+
public record Cryptocurrency(string Id, string Symbol, string Name);
46+
}

0 commit comments

Comments
 (0)