Skip to content

Commit

Permalink
feat: Added a new section / method for searching data on CoinGecko
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor-pelykh committed Sep 18, 2022
1 parent 36da5fe commit 9b3e27f
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/coingecko_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:coingecko_api/sections/exchanges_section.dart';
import 'package:coingecko_api/sections/global_section.dart';
import 'package:coingecko_api/sections/indexes_section.dart';
import 'package:coingecko_api/sections/ping_section.dart';
import 'package:coingecko_api/sections/search_section.dart';
import 'package:coingecko_api/sections/simple_section.dart';
import 'package:coingecko_api/sections/trending_section.dart';
import 'package:dio/dio.dart';
Expand All @@ -34,6 +35,7 @@ class CoinGeckoApi {
late IndexesSection _indexes;
late DerivativesSection _derivatives;
late ExchangeRatesSection _exchangeRates;
late SearchSection _search;
late TrendingSection _trending;
late GlobalSection _global;
late CompaniesSection _companies;
Expand Down Expand Up @@ -70,6 +72,10 @@ class CoinGeckoApi {
/// The section that brings together the requests that are related to exchange rates
ExchangeRatesSection get exchangeRates => _exchangeRates;

/// The section that brings together the requests
/// that are related to search
SearchSection get search => _search;

/// The section that brings together the requests
/// that are related to trending stats
TrendingSection get trending => _trending;
Expand Down Expand Up @@ -155,6 +161,7 @@ class CoinGeckoApi {
_indexes = IndexesSection(_dio);
_derivatives = DerivativesSection(_dio);
_exchangeRates = ExchangeRatesSection(_dio);
_search = SearchSection(_dio);
_trending = TrendingSection(_dio);
_global = GlobalSection(_dio);
_companies = CompaniesSection(_dio);
Expand Down
167 changes: 167 additions & 0 deletions lib/data/search_results.dart
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';
}
}
39 changes: 39 additions & 0 deletions lib/sections/search_section.dart
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,
);
}
}
}

0 comments on commit 9b3e27f

Please sign in to comment.