diff --git a/lib/data/network/currency_api.dart b/lib/data/network/currency_api.dart index 71ebda4..9104905 100644 --- a/lib/data/network/currency_api.dart +++ b/lib/data/network/currency_api.dart @@ -8,57 +8,52 @@ import '../model/currency.dart'; part 'currency_api.g.dart'; -@RestApi(baseUrl: "https://free.currencyconverterapi.com/api/v3/") +@RestApi(baseUrl: "https://exchange-rates.abstractapi.com/v1/") abstract class CurrencyApi { factory CurrencyApi(Dio dio, {String baseUrl}) = _CurrencyApi; + static const api = String.fromEnvironment('currencyApiKey'); - + static CurrencyApi create() { final dio = AppDio.createDio(); - dio.options.queryParameters.addAll({"apiKey": api}); + dio.options.queryParameters.addAll({"api_key": api}); return _CurrencyApi(dio); } - @GET("/convert?") - Future> getConversion(@Query("q") String currency); + @GET("/live?") + Future> getConversion( + @Query("base") String from, @Query("target") String to); } @JsonSerializable() -class ResponseObject implements CurrencyMapper{ - dynamic query; - Map? results; +class ResponseObject implements CurrencyMapper { + String base; + String last_updated; + Map? exchange_rates; - ResponseObject({ - this.query, - this.results - }); + ResponseObject(this.base, this.last_updated, this.exchange_rates); - factory ResponseObject.fromJson(Map json) => _$ResponseObjectFromJson(json); + factory ResponseObject.fromJson(Map json) => + _$ResponseObjectFromJson(json); Map toJson() => _$ResponseObjectToJson(this); @override Currency convert() { - CurrencyObject? cur = results?.entries.elementAt(0).value; - return Currency(cur?.fr, cur?.to, cur?.val); + return Currency(base, exchange_rates?.keys.first, exchange_rates?.values.first); } - } @JsonSerializable() -class CurrencyObject{ +class CurrencyObject { String? id; String? fr; String? to; double? val; - CurrencyObject({ - this.id, - this.fr, - this.to, - this.val - }); + CurrencyObject({this.id, this.fr, this.to, this.val}); - factory CurrencyObject.fromJson(Map json) => _$CurrencyObjectFromJson(json); + factory CurrencyObject.fromJson(Map json) => + _$CurrencyObjectFromJson(json); Map toJson() => _$CurrencyObjectToJson(this); -} \ No newline at end of file +} diff --git a/lib/data/repository/repository_impl.dart b/lib/data/repository/repository_impl.dart index 5d6f3ba..bc32da7 100644 --- a/lib/data/repository/repository_impl.dart +++ b/lib/data/repository/repository_impl.dart @@ -33,10 +33,8 @@ class RepositoryImpl extends Repository with SafeApiCall { String from = fromCurrency.getCurrencyCode(); String to = toCurrency.getCurrencyCode(); - String currency = "${from}_$to"; - try { - ResponseObject responseObject = await getDataFromApiCall(_api.getConversion(currency)); + ResponseObject responseObject = await getDataFromApiCall(_api.getConversion(from, to)); return responseObject.convert(); } on HttpException catch(error) { logger.e(error); diff --git a/pubspec.lock b/pubspec.lock index 83f4454..53f0927 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -379,7 +379,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.2.0" + version: "0.1.5" meta: dependency: transitive description: @@ -622,7 +622,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.9.1" + version: "1.9.0" stack_trace: dependency: transitive description: @@ -685,7 +685,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.13" + version: "0.4.12" timing: dependency: transitive description: @@ -734,7 +734,7 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.3" + version: "2.1.2" watcher: dependency: transitive description: