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';
|
part 'currency_api.g.dart';
|
||||||
|
|
||||||
@RestApi(baseUrl: "https://free.currencyconverterapi.com/api/v3/")
|
@RestApi(baseUrl: "https://exchange-rates.abstractapi.com/v1/")
|
||||||
abstract class CurrencyApi {
|
abstract class CurrencyApi {
|
||||||
factory CurrencyApi(Dio dio, {String baseUrl}) = _CurrencyApi;
|
factory CurrencyApi(Dio dio, {String baseUrl}) = _CurrencyApi;
|
||||||
|
|
||||||
static const api = String.fromEnvironment('currencyApiKey');
|
static const api = String.fromEnvironment('currencyApiKey');
|
||||||
|
|
||||||
static CurrencyApi create() {
|
static CurrencyApi create() {
|
||||||
final dio = AppDio.createDio();
|
final dio = AppDio.createDio();
|
||||||
dio.options.queryParameters.addAll({"apiKey": api});
|
dio.options.queryParameters.addAll({"api_key": api});
|
||||||
|
|
||||||
return _CurrencyApi(dio);
|
return _CurrencyApi(dio);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET("/convert?")
|
@GET("/live?")
|
||||||
Future<HttpResponse<ResponseObject>> getConversion(@Query("q") String currency);
|
Future<HttpResponse<ResponseObject>> getConversion(
|
||||||
|
@Query("base") String from, @Query("target") String to);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonSerializable()
|
@JsonSerializable()
|
||||||
class ResponseObject implements CurrencyMapper{
|
class ResponseObject implements CurrencyMapper {
|
||||||
dynamic query;
|
String base;
|
||||||
Map<String, CurrencyObject>? results;
|
String last_updated;
|
||||||
|
Map<String, double>? exchange_rates;
|
||||||
|
|
||||||
ResponseObject({
|
ResponseObject(this.base, this.last_updated, this.exchange_rates);
|
||||||
this.query,
|
|
||||||
this.results
|
|
||||||
});
|
|
||||||
|
|
||||||
factory ResponseObject.fromJson(Map<String, dynamic> json) => _$ResponseObjectFromJson(json);
|
factory ResponseObject.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$ResponseObjectFromJson(json);
|
||||||
Map<String, dynamic> toJson() => _$ResponseObjectToJson(this);
|
Map<String, dynamic> toJson() => _$ResponseObjectToJson(this);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Currency convert() {
|
Currency convert() {
|
||||||
CurrencyObject? cur = results?.entries.elementAt(0).value;
|
return Currency(base, exchange_rates?.keys.first, exchange_rates?.values.first);
|
||||||
return Currency(cur?.fr, cur?.to, cur?.val);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonSerializable()
|
@JsonSerializable()
|
||||||
class CurrencyObject{
|
class CurrencyObject {
|
||||||
String? id;
|
String? id;
|
||||||
String? fr;
|
String? fr;
|
||||||
String? to;
|
String? to;
|
||||||
double? val;
|
double? val;
|
||||||
|
|
||||||
CurrencyObject({
|
CurrencyObject({this.id, this.fr, this.to, this.val});
|
||||||
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);
|
Map<String, dynamic> toJson() => _$CurrencyObjectToJson(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,10 +33,8 @@ class RepositoryImpl extends Repository with SafeApiCall {
|
|||||||
String from = fromCurrency.getCurrencyCode();
|
String from = fromCurrency.getCurrencyCode();
|
||||||
String to = toCurrency.getCurrencyCode();
|
String to = toCurrency.getCurrencyCode();
|
||||||
|
|
||||||
String currency = "${from}_$to";
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ResponseObject responseObject = await getDataFromApiCall(_api.getConversion(currency));
|
ResponseObject responseObject = await getDataFromApiCall(_api.getConversion(from, to));
|
||||||
return responseObject.convert();
|
return responseObject.convert();
|
||||||
} on HttpException catch(error) {
|
} on HttpException catch(error) {
|
||||||
logger.e(error);
|
logger.e(error);
|
||||||
|
|||||||
@@ -379,7 +379,7 @@ packages:
|
|||||||
name: material_color_utilities
|
name: material_color_utilities
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.0"
|
version: "0.1.5"
|
||||||
meta:
|
meta:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -622,7 +622,7 @@ packages:
|
|||||||
name: source_span
|
name: source_span
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.9.1"
|
version: "1.9.0"
|
||||||
stack_trace:
|
stack_trace:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -685,7 +685,7 @@ packages:
|
|||||||
name: test_api
|
name: test_api
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.4.13"
|
version: "0.4.12"
|
||||||
timing:
|
timing:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -734,7 +734,7 @@ packages:
|
|||||||
name: vector_math
|
name: vector_math
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.3"
|
version: "2.1.2"
|
||||||
watcher:
|
watcher:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
Reference in New Issue
Block a user