Skip to content

Commit

Permalink
added partial support for pro endpoints
Browse files Browse the repository at this point in the history
- added coingecko banner
- Added support for the following coin PRO endpoints
    - getNewList
    - getTopGainersAndLosers
- Exported exposed classes
- Removed unnecessary unit test imports and replaced by the actual library
  • Loading branch information
pangz-lab committed Aug 25, 2023
1 parent 0b91251 commit 34e139e
Show file tree
Hide file tree
Showing 53 changed files with 708 additions and 151 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.2.0
2023/08/26 JST
- Added support for the following coin PRO endpoints
- getNewList
- getTopGainersAndLosers
- Exported exposed classes
- Removed unnecessary unit test imports and replaced by the actual library import
## 1.1.1
2023/07/14 JST
- Update http library to version 1.1.0
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ client.coins.getOhlcList(
);
```

## **[ 💰PRO Endpoints ]**
### 📤 */coins/list/new*
```dart
client.coins.getNewList();
```
### 📤 */coins/top_gainers_losers*
```dart
client.coins.getTopGainersAndLosers(
vsCurrency: Currencies.php,
duration: CoinDuration.in14Days,
topCoins: CoinRanking.top300
);
```

___
<br>

Expand Down
41 changes: 31 additions & 10 deletions lib/coingecko_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,68 @@
library coingecko_client;

export 'src/coingecko_client_base.dart';
export 'src/domain/base_endpoint.dart';
export 'src/domain/asset_platforms/asset_platforms_endpoint.dart';
export 'src/domain/categories/categories_endpoint.dart';
export 'src/domain/coins/coins_endpoint.dart';
export 'src/domain/companies/companies_endpoint.dart';
export 'src/domain/contract/contract_endpoint.dart';
export 'src/domain/derivatives/derivatives_endpoint.dart';
export 'src/domain/exchange_rates/exchange_rates_endpoint.dart';
export 'src/domain/exchanges/exchanges_endpoint.dart';
export 'src/domain/global/global_endpoint.dart';
export 'src/domain/indexes/indexes_endpoint.dart';
export 'src/domain/nfts/nfts_endpoint.dart';
export 'src/domain/ping/ping_endpoint.dart';
export 'src/domain/search/search_endpoint.dart';
export 'src/domain/simple/simple_endpoint.dart';
export 'src/domain/trending/trending_endpoint.dart';
export 'src/domain/asset_platforms/models/asset_platform.dart';
export 'src/domain/asset_platforms/models/asset_platforms_filter.dart';
export 'src/domain/categories/models/coin_categories_data_ordering.dart';
export 'src/domain/asset_platforms/enums/asset_platforms_filter.dart';
export 'src/domain/categories/enums/coin_categories_data_ordering.dart';
export 'src/domain/categories/models/coin_category_basic_info.dart';
export 'src/domain/categories/models/coin_category_info.dart';
export 'src/domain/coins/models/coin_basic_info.dart';
export 'src/domain/coins/models/coin_data_ordering.dart';
export 'src/domain/coins/models/coin_ranking_info.dart';
export 'src/domain/coins/models/coin_ranking_result.dart';
export 'src/domain/coins/models/coin_info.dart';
export 'src/domain/coins/models/coin_links.dart';
export 'src/domain/coins/models/coin_market.dart';
export 'src/domain/coins/models/coin_market_history.dart';
export 'src/domain/coins/models/coin_ohlc.dart';
export 'src/domain/coins/models/coin_price_change.dart';
export 'src/domain/coins/models/coin_tickers.dart';
export 'src/domain/coins/models/market_data.dart';
export 'src/domain/coins/models/roi.dart';
export 'src/domain/coins/models/ticker_info.dart';
export 'src/domain/coins/models/coin_basic_info.dart';
export 'src/domain/coins/enums/coin_data_ordering.dart';
export 'src/domain/coins/enums/coin_duration.dart';
export 'src/domain/coins/enums/coin_price_change.dart';
export 'src/domain/coins/enums/coin_ranking.dart';
export 'src/domain/companies/models/company_info.dart';
export 'src/domain/companies/models/company_list.dart';
export 'src/domain/contract/models/contract_info.dart';
export 'src/domain/contract/models/contract_market_history.dart';
export 'src/domain/derivatives/models/derivatives.dart';
export 'src/domain/derivatives/models/derivatives_exchange.dart';
export 'src/domain/derivatives/models/derivatives_exchange_ordering.dart';
export 'src/domain/derivatives/models/derivatives_tickers.dart';
export 'src/domain/derivatives/models/derivatives_ticker_info.dart';
export 'src/domain/derivatives/enums/derivatives_exchange_ordering.dart';
export 'src/domain/derivatives/enums/derivatives_tickers.dart';
export 'src/domain/exchanges/models/exchange_basic_info.dart';
export 'src/domain/exchanges/models/exchange_data_ordering.dart';
export 'src/domain/exchanges/models/exchange_info.dart';
export 'src/domain/exchanges/models/exchange_status.dart';
export 'src/domain/exchanges/models/exchange_volume_chart.dart';
export 'src/domain/exchanges/enums/exchange_data_ordering.dart';
export 'src/domain/exchange_rates/models/exchange_rate.dart';
export 'src/domain/global/models/global_crypto_info.dart';
export 'src/domain/global/models/global_defi_info.dart';
export 'src/domain/indexes/models/market_index.dart';
export 'src/domain/nfts/models/nfts_data_ordering.dart';
export 'src/domain/nfts/models/nft_basic_info.dart';
export 'src/domain/nfts/models/nft_info.dart';
export 'src/models/base_model.dart';
export 'src/domain/nfts/enums/nfts_data_ordering.dart';
export 'src/models/image.dart';
export 'src/models/currencies.dart';
export 'src/models/data_range.dart';
export 'src/models/historical_data.dart';
export 'src/models/exceptions/data_parsing_exception.dart';
export 'src/models/exceptions/network_request_exception.dart';
export 'src/models/base_model.dart';
4 changes: 2 additions & 2 deletions lib/src/domain/asset_platforms/asset_platforms_endpoint.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:coingecko_client/src/domain/asset_platforms/models/asset_platform.dart';
import 'package:coingecko_client/src/domain/base_endpoint.dart';
import 'package:coingecko_client/src/domain/asset_platforms/models/asset_platforms_filter.dart';
import 'package:coingecko_client/src/domain/asset_platforms/enums/asset_platforms_filter.dart';
import 'package:coingecko_client/src/models/exceptions/network_request_exception.dart';
import 'package:coingecko_client/src/models/exceptions/data_parsing_exception.dart';
import 'package:coingecko_client/src/services/http_request_service.dart';
Expand All @@ -11,7 +11,7 @@ import 'package:coingecko_client/src/services/http_request_service.dart';
class AssetPlatformsEndpoint extends BaseEndpoint {
AssetPlatformsEndpoint(HttpRequestServiceInterface httpRequestService,
{String? apiKey})
: super(httpRequestService, {apiKey: apiKey});
: super(httpRequestService, apiKey: apiKey);

/// List all asset platforms (Blockchain networks)
/// <br/><b>Endpoint </b>: /asset_platforms
Expand Down
9 changes: 6 additions & 3 deletions lib/src/domain/base_endpoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:io';
import 'package:coingecko_client/src/models/exceptions/data_parsing_exception.dart';
import 'package:coingecko_client/src/models/exceptions/network_request_exception.dart';
import 'package:coingecko_client/src/models/exceptions/request_exception.dart';
import 'package:coingecko_client/src/services/http_request_service.dart';

/// Main class that provides utility methods in preparing and sending the<br>
Expand All @@ -17,7 +18,7 @@ class BaseEndpoint {
/// Coingecko key name. This is embedded either in the url query
/// of the http request header. This will contain the API key
/// provided to the pro user when sent to the service.
static final apiKeyQueryParam = "x_cg_pro_api_key";
static final apiKeyQueryParam = "x-cg-pro-api-key";

/// Current API version supported
static final version = "/api/v3";
Expand All @@ -28,8 +29,7 @@ class BaseEndpoint {
String get endpointPath => _endpointPath;
HttpRequestServiceInterface httpRequestService;
String? _apiKey;
BaseEndpoint(this.httpRequestService, Map<String?, String?> map,
{String? apiKey}) {
BaseEndpoint(this.httpRequestService, {String? apiKey}) {
_apiKey = apiKey;
}

Expand All @@ -51,6 +51,9 @@ class BaseEndpoint {
/// ```
Future<dynamic> sendPro(String path) {
try {
if (_apiKey?.isEmpty ?? true) {
throw RequestException.apiKeyNotFound();
}
final headers = Map.fromEntries(
[MapEntry<String, String>(apiKeyQueryParam, _apiKey ?? '')]);
return _send(path, apiProHost, headers: headers);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/domain/categories/categories_endpoint.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:coingecko_client/src/domain/base_endpoint.dart';
import 'package:coingecko_client/src/domain/categories/models/coin_categories_data_ordering.dart';
import 'package:coingecko_client/src/domain/categories/enums/coin_categories_data_ordering.dart';
import 'package:coingecko_client/src/domain/categories/models/coin_category_basic_info.dart';
import 'package:coingecko_client/src/domain/categories/models/coin_category_info.dart';
import 'package:coingecko_client/src/models/exceptions/data_parsing_exception.dart';
Expand All @@ -11,7 +11,7 @@ import 'package:coingecko_client/src/services/http_request_service.dart';
class CategoriesEndpoint extends BaseEndpoint {
CategoriesEndpoint(HttpRequestServiceInterface httpRequestService,
{String? apiKey})
: super(httpRequestService, {apiKey: apiKey});
: super(httpRequestService, apiKey: apiKey);

/// List all categories
/// <br/><b>Endpoint </b>: /coins/categories/list
Expand Down
55 changes: 51 additions & 4 deletions lib/src/domain/coins/coins_endpoint.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'package:coingecko_client/src/domain/base_endpoint.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_basic_info.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_info.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_market.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_price_change.dart';
import 'package:coingecko_client/src/domain/base_endpoint.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_data_ordering.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_ranking_result.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_tickers.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_market_history.dart';
import 'package:coingecko_client/src/domain/coins/models/coin_ohlc.dart';
import 'package:coingecko_client/src/domain/coins/enums/coin_price_change.dart';
import 'package:coingecko_client/src/domain/coins/enums/coin_duration.dart';
import 'package:coingecko_client/src/domain/coins/enums/coin_ranking.dart';
import 'package:coingecko_client/src/domain/coins/enums/coin_data_ordering.dart';
import 'package:coingecko_client/src/models/currencies.dart';
import 'package:coingecko_client/src/models/data_range.dart';
import 'package:coingecko_client/src/models/exceptions/data_parsing_exception.dart';
Expand Down Expand Up @@ -37,7 +40,7 @@ import 'package:coingecko_client/src/services/http_request_service.dart';
class CoinsEndpoint extends BaseEndpoint {
CoinsEndpoint(HttpRequestServiceInterface httpRequestService,
{String? apiKey})
: super(httpRequestService, {apiKey: apiKey});
: super(httpRequestService, apiKey: apiKey);

/// List all supported coins id, name and symbol (no pagination required)
/// <br/><b>Endpoint </b>: /coins/list
Expand Down Expand Up @@ -319,4 +322,48 @@ class CoinsEndpoint extends BaseEndpoint {
rethrow;
}
}

/// [💰PRO Endpoint] Get the latest 200 coins (id) that recently listed on CoinGecko.com
/// <br/><b>Endpoint </b>: /coins/list/new
Future<List<CoinBasicInfo>> getNewList() async {
try {
final path = "/coins/list/new";
final result = List<dynamic>.of(await sendPro(path));
return result.map((value) => CoinBasicInfo.fromJson(value)).toList();
} on FormatException {
throw DataParsingException.unreadableData();
} on TypeError {
throw DataParsingException.mismatchedType();
} catch (_) {
rethrow;
}
}

/// [💰PRO Endpoint] Get the top 30 coins with largest price gain and loss by a specific time duration
/// <br/><b>Endpoint </b>: /coins/top_gainers_losers
///
/// [vs_currency] The target currency to use for filtering the data (usd, eur, jpy, etc.)
/// [duration] Filter result by time range - Default `24h`
/// [top_coins] Filter result by MarketCap ranking or all coins (including coins that do not have MarketCap ranking)
Future<CoinRankingResult> getTopGainersAndLosers(
{required Currencies vsCurrency,
CoinDuration? duration,
CoinRanking? topCoins}) async {
try {
final path = createEndpointPathUrl(rawQueryItems: {
'vs_currency': vsCurrency.code,
'duration': duration?.value,
'top_coins': topCoins?.value,
}, endpointPath: "/coins/top_gainers_losers");

final result = Map<String, dynamic>.from(await sendPro(path));
return CoinRankingResult.fromJson(result, vsCurrency);
} on FormatException {
throw DataParsingException.unreadableData();
} on TypeError {
throw DataParsingException.mismatchedType();
} catch (_) {
rethrow;
}
}
}
26 changes: 26 additions & 0 deletions lib/src/domain/coins/enums/coin_duration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// Used to filter data by duration time.
enum CoinDuration {
/// Data duration in 1 hour
in1Hour('1h'),

/// Data duration in 24 hours
in24Hours('24h'),

/// Data duration in 7 days
in7Days('7d'),

/// Data duration in 14 days
in14Days('14d'),

/// Data duration in 30 days
in30Days('30d'),

/// Data duration in 200 days
in200Days('60d'),

/// Data duration in 1 year
in1Year('1y');

const CoinDuration(this.value);
final String value;
}
17 changes: 17 additions & 0 deletions lib/src/domain/coins/enums/coin_ranking.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// Used to return the top N result.
enum CoinRanking {
/// Top 300
top300('300'),

/// Top 500
top500('500'),

/// Top 1000
top1000('1000'),

/// All
all('all');

const CoinRanking(this.value);
final String value;
}
8 changes: 7 additions & 1 deletion lib/src/domain/coins/models/coin_basic_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,34 @@ class CoinBasicInfo extends BaseModel {
String? _symbol;
String? _name;
Map<String, dynamic>? _platforms;
DateTime? _activatedAt;

CoinBasicInfo(
{String? id,
String? symbol,
String? name,
Map<String, dynamic>? platforms}) {
Map<String, dynamic>? platforms,
DateTime? activatedAt}) {
_id = id;
_symbol = symbol;
_name = name;
_platforms = platforms;
_activatedAt = activatedAt;
}

String? get id => _id;
String? get symbol => _symbol;
String? get name => _name;
Map<String, dynamic>? get platforms => _platforms;
DateTime? get activatedAt => _activatedAt;

/// Converts the raw json data(contained in a Map or List) to a CoinBasicInfo object
CoinBasicInfo.fromJson(Map<String, dynamic> json) {
_id = json['id'];
_symbol = json['symbol'];
_name = json['name'];
_platforms = toMap<dynamic>(json['platforms']);
_activatedAt = toDateFromTimestamp(json['activated_at']);
}

/// Converts the object to a Map to make it json serializable.
Expand All @@ -38,6 +43,7 @@ class CoinBasicInfo extends BaseModel {
data['symbol'] = _symbol;
data['name'] = _name;
data['platforms'] = _platforms;
data['activated_at'] = _activatedAt;
return data;
}
}
Loading

0 comments on commit 34e139e

Please sign in to comment.