mirror of
https://github.com/hmalik144/easy_cc_flutter.git
synced 2025-12-10 03:05:34 +00:00
61 lines
1.7 KiB
Dart
61 lines
1.7 KiB
Dart
// ignore_for_file: non_constant_identifier_names, depend_on_referenced_packages
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:easy_cc_flutter/data/network/app_dio.dart';
|
|
import 'package:json_annotation/json_annotation.dart';
|
|
import 'package:retrofit/retrofit.dart';
|
|
|
|
import '../model/currency.dart';
|
|
|
|
part 'currency_api.g.dart';
|
|
|
|
@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({"api_key": api});
|
|
|
|
return _CurrencyApi(dio);
|
|
}
|
|
|
|
@GET("/live?")
|
|
Future<HttpResponse<ResponseObject>> getConversion(
|
|
@Query("base") String from, @Query("target") String to);
|
|
}
|
|
|
|
@JsonSerializable()
|
|
class ResponseObject implements CurrencyMapper {
|
|
String base;
|
|
String last_updated;
|
|
Map<String, double>? exchange_rates;
|
|
|
|
ResponseObject(this.base, this.last_updated, this.exchange_rates);
|
|
|
|
factory ResponseObject.fromJson(Map<String, dynamic> json) =>
|
|
_$ResponseObjectFromJson(json);
|
|
Map<String, dynamic> toJson() => _$ResponseObjectToJson(this);
|
|
|
|
@override
|
|
Currency convert() {
|
|
return Currency(base, exchange_rates?.keys.first, exchange_rates?.values.first);
|
|
}
|
|
}
|
|
|
|
@JsonSerializable()
|
|
class CurrencyObject {
|
|
String? id;
|
|
String? fr;
|
|
String? to;
|
|
double? val;
|
|
|
|
CurrencyObject({this.id, this.fr, this.to, this.val});
|
|
|
|
factory CurrencyObject.fromJson(Map<String, dynamic> json) =>
|
|
_$CurrencyObjectFromJson(json);
|
|
Map<String, dynamic> toJson() => _$CurrencyObjectToJson(this);
|
|
}
|