mirror of
https://github.com/hmalik144/easy_cc_flutter.git
synced 2025-12-10 03:05:34 +00:00
UPDATED API
This commit is contained in:
@@ -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<HttpResponse<ResponseObject>> getConversion(@Query("q") String currency);
|
||||
@GET("/live?")
|
||||
Future<HttpResponse<ResponseObject>> getConversion(
|
||||
@Query("base") String from, @Query("target") String to);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class ResponseObject implements CurrencyMapper{
|
||||
dynamic query;
|
||||
Map<String, CurrencyObject>? results;
|
||||
class ResponseObject implements CurrencyMapper {
|
||||
String base;
|
||||
String last_updated;
|
||||
Map<String, double>? exchange_rates;
|
||||
|
||||
ResponseObject({
|
||||
this.query,
|
||||
this.results
|
||||
});
|
||||
ResponseObject(this.base, this.last_updated, this.exchange_rates);
|
||||
|
||||
factory ResponseObject.fromJson(Map<String, dynamic> json) => _$ResponseObjectFromJson(json);
|
||||
factory ResponseObject.fromJson(Map<String, dynamic> json) =>
|
||||
_$ResponseObjectFromJson(json);
|
||||
Map<String, dynamic> 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<String, dynamic> json) => _$CurrencyObjectFromJson(json);
|
||||
factory CurrencyObject.fromJson(Map<String, dynamic> json) =>
|
||||
_$CurrencyObjectFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$CurrencyObjectToJson(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user