mirror of
https://github.com/hmalik144/easy_cc_flutter.git
synced 2026-03-18 07:35:54 +00:00
Removal of api key
Network calls, integration of retrofit api added backup api
This commit is contained in:
35
lib/data/network/backupCurrencyApi.dart
Normal file
35
lib/data/network/backupCurrencyApi.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:retrofit/retrofit.dart';
|
||||
|
||||
import '../model/Currency.dart';
|
||||
|
||||
part 'backupCurrencyApi.g.dart';
|
||||
|
||||
@RestApi(baseUrl: "https://api.frankfurter.app/")
|
||||
abstract class BackupCurrencyApi {
|
||||
factory BackupCurrencyApi(Dio dio, {String baseUrl}) = _BackupCurrencyApi;
|
||||
|
||||
@GET("latest?")
|
||||
Future<HttpResponse<CurrencyResponse>> getCurrencyRate(@Query("from") String currencyFrom,
|
||||
@Query("to") String currencyTo);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class CurrencyResponse implements Mapper{
|
||||
String? data;
|
||||
double amount;
|
||||
Map<String, double>? rates;
|
||||
String? base;
|
||||
|
||||
CurrencyResponse(this.data, this.amount, this.rates, this.base);
|
||||
|
||||
factory CurrencyResponse.fromJson(Map<String, dynamic> json) => _$CurrencyResponseFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$CurrencyResponseToJson(this);
|
||||
|
||||
@override
|
||||
Currency convert() {
|
||||
MapEntry<String, double>? entry = rates?.entries.elementAt(0);
|
||||
return Currency(base, entry?.key, entry?.value);
|
||||
}
|
||||
}
|
||||
54
lib/data/network/currencyApi.dart
Normal file
54
lib/data/network/currencyApi.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:retrofit/retrofit.dart';
|
||||
|
||||
import '../model/Currency.dart';
|
||||
|
||||
part 'currencyApi.g.dart';
|
||||
|
||||
@RestApi(baseUrl: "https://free.currencyconverterapi.com/api/v3/")
|
||||
abstract class CurrencyApi {
|
||||
factory CurrencyApi(Dio dio, {String baseUrl}) = _CurrencyApi;
|
||||
|
||||
@GET("/convert?")
|
||||
Future<HttpResponse<ResponseObject>> getConversion(@Query("apiKey") String apiKey, @Query("q") String currency);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class ResponseObject implements Mapper{
|
||||
dynamic query;
|
||||
Map<String, CurrencyObject>? results;
|
||||
|
||||
ResponseObject({
|
||||
this.query,
|
||||
this.results
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
30
lib/data/network/safeApiCall.dart
Normal file
30
lib/data/network/safeApiCall.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:retrofit/retrofit.dart';
|
||||
|
||||
import '../../main.dart';
|
||||
|
||||
mixin SafeApiCall {
|
||||
Future<T> getDataFromApiCall<T>(Future<HttpResponse<T>> apiCall) async {
|
||||
try {
|
||||
HttpResponse<T> httpResponse = await apiCall;
|
||||
return httpResponse.data;
|
||||
} on DioError catch(dioError) {
|
||||
Map<String, dynamic>? errorResponse = dioError.response?.data;
|
||||
String error;
|
||||
|
||||
if (errorResponse?["error"] != null){
|
||||
error = errorResponse!["error"];
|
||||
} else if (dioError.error != null){
|
||||
error = dioError.error;
|
||||
} else {
|
||||
error = "Failed to retrieve data from api";
|
||||
}
|
||||
|
||||
logger.e(dioError.error);
|
||||
|
||||
throw HttpException(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user