-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added a new section / method for searching data on CoinGecko
- Loading branch information
1 parent
36da5fe
commit 9b3e27f
Showing
3 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import 'package:coingecko_api/helpers/convert.dart'; | ||
|
||
/// Coin search results data wrapper | ||
class CoinSearchResults { | ||
/// Identifier | ||
final String id; | ||
|
||
/// Coin name | ||
final String name; | ||
|
||
/// Coin API symbol | ||
final String apiSymbol; | ||
|
||
/// Coin symbol | ||
final String symbol; | ||
|
||
/// Market capitalization rank | ||
final int marketCapRank; | ||
|
||
/// Thumbnail image url | ||
final String thumb; | ||
|
||
/// Large image url | ||
final String large; | ||
|
||
CoinSearchResults.fromJson(Map<String, dynamic> json) | ||
: this.id = Convert.toStr(json['id'], ''), | ||
this.name = Convert.toStr(json['name'], ''), | ||
this.apiSymbol = Convert.toStr(json['api_symbol'], ''), | ||
this.symbol = Convert.toStr(json['symbol'], ''), | ||
this.marketCapRank = Convert.toInt(json['market_cap_rank'], 0), | ||
this.thumb = Convert.toStr(json['thumb'], ''), | ||
this.large = Convert.toStr(json['large'], ''); | ||
|
||
@override | ||
String toString() { | ||
return 'CoinSearchResults: id = $id, name = $name'; | ||
} | ||
} | ||
|
||
/// Exchange search results data wrapper | ||
class ExchangeSearchResults { | ||
/// Identifier | ||
final String id; | ||
|
||
/// Exchange name | ||
final String name; | ||
|
||
/// Exchange market type | ||
final String marketType; | ||
|
||
/// Thumbnail image url | ||
final String thumb; | ||
|
||
/// Large image url | ||
final String large; | ||
|
||
ExchangeSearchResults.fromJson(Map<String, dynamic> json) | ||
: this.id = Convert.toStr(json['id'], ''), | ||
this.name = Convert.toStr(json['name'], ''), | ||
this.marketType = Convert.toStr(json['market_type'], ''), | ||
this.thumb = Convert.toStr(json['thumb'], ''), | ||
this.large = Convert.toStr(json['large'], ''); | ||
|
||
@override | ||
String toString() { | ||
return 'ExchangeSearchResults: id = $id, name = $name'; | ||
} | ||
} | ||
|
||
/// Category search results data wrapper | ||
class CategorySearchResults { | ||
/// Identifier | ||
final int id; | ||
|
||
/// Category name | ||
final String name; | ||
|
||
CategorySearchResults.fromJson(Map<String, dynamic> json) | ||
: this.id = Convert.toInt(json['id'], 0), | ||
this.name = Convert.toStr(json['name'], ''); | ||
|
||
@override | ||
String toString() { | ||
return 'CategorySearchResults: id = $id, name = $name'; | ||
} | ||
} | ||
|
||
/// NFT search results data wrapper | ||
class NftSearchResults { | ||
/// Identifier | ||
final String id; | ||
|
||
/// NFT name | ||
final String name; | ||
|
||
/// NFT symbol | ||
final String symbol; | ||
|
||
/// Thumbnail image url | ||
final String thumb; | ||
|
||
NftSearchResults.fromJson(Map<String, dynamic> json) | ||
: this.id = Convert.toStr(json['id'], ''), | ||
this.name = Convert.toStr(json['name'], ''), | ||
this.symbol = Convert.toStr(json['symbol'], ''), | ||
this.thumb = Convert.toStr(json['thumb'], ''); | ||
|
||
@override | ||
String toString() { | ||
return 'NftSearchResults: id = $id, name = $name'; | ||
} | ||
} | ||
|
||
/// Search results data wrapper | ||
class SearchResults { | ||
/// Coins | ||
final List<CoinSearchResults>? coins; | ||
|
||
/// Exchanges | ||
final List<ExchangeSearchResults>? exchanges; | ||
|
||
/// Categories | ||
final List<CategorySearchResults>? categories; | ||
|
||
/// NFTs | ||
final List<NftSearchResults>? nfts; | ||
|
||
static List<CoinSearchResults>? _parseCoins(dynamic json) { | ||
final jsonList = Convert.toListN(json); | ||
return jsonList != null | ||
? jsonList.map((e) => CoinSearchResults.fromJson(e)).toList() | ||
: null; | ||
} | ||
|
||
static List<ExchangeSearchResults>? _parseExchanges(dynamic json) { | ||
final jsonList = Convert.toListN(json); | ||
return jsonList != null | ||
? jsonList.map((e) => ExchangeSearchResults.fromJson(e)).toList() | ||
: null; | ||
} | ||
|
||
static List<CategorySearchResults>? _parseCategories(dynamic json) { | ||
final jsonList = Convert.toListN(json); | ||
return jsonList != null | ||
? jsonList.map((e) => CategorySearchResults.fromJson(e)).toList() | ||
: null; | ||
} | ||
|
||
static List<NftSearchResults>? _parseNFTs(dynamic json) { | ||
final jsonList = Convert.toListN(json); | ||
return jsonList != null | ||
? jsonList.map((e) => NftSearchResults.fromJson(e)).toList() | ||
: null; | ||
} | ||
|
||
SearchResults.fromJson(Map<String, dynamic> json) | ||
: this.coins = _parseCoins(json['coins']), | ||
this.exchanges = _parseExchanges(json['exchanges']), | ||
this.categories = _parseCategories(json['categories']), | ||
this.nfts = _parseNFTs(json['nfts']); | ||
|
||
@override | ||
String toString() { | ||
return 'SearchResults'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'package:coingecko_api/coingecko_result.dart'; | ||
import 'package:coingecko_api/data/search_results.dart'; | ||
import 'package:dio/dio.dart'; | ||
|
||
/// The section that brings together the requests | ||
/// that are related to search | ||
class SearchSection { | ||
final Dio _dio; | ||
|
||
const SearchSection(this._dio); | ||
|
||
/// | ||
/// Search for coins, categories and markets listed on CoinGecko | ||
/// ordered by largest Market Cap first. | ||
/// | ||
/// Query: **/search** | ||
/// | ||
Future<CoinGeckoResult<SearchResults?>> searchFor({ | ||
required String query, | ||
}) async { | ||
final response = await _dio.get( | ||
'/search', | ||
queryParameters: { | ||
'query': query, | ||
}, | ||
); | ||
if (response.statusCode == 200) { | ||
final searchResults = SearchResults.fromJson(response.data); | ||
return CoinGeckoResult(searchResults); | ||
} else { | ||
return CoinGeckoResult( | ||
null, | ||
errorCode: response.statusCode ?? null, | ||
errorMessage: '${response.statusMessage} - ${response.data.toString()}', | ||
isError: true, | ||
); | ||
} | ||
} | ||
} |