2024-09-18 15:07:08 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-10-07 11:01:02 +02:00
|
|
|
import 'package:ambito/src/domain/entity/entities.dart';
|
2024-10-28 16:17:09 +01:00
|
|
|
import 'package:ambito/src/domain/entity/funding_program/funding_program.dart';
|
2024-09-18 15:07:08 +02:00
|
|
|
import 'package:ambito/src/packages/ambito_api/restclient.dart';
|
2024-10-28 16:17:09 +01:00
|
|
|
import 'package:ambito/src/packages/ambito_db/repositories/funding_program_repository.dart';
|
2024-10-21 15:01:20 +02:00
|
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
2024-09-18 15:07:08 +02:00
|
|
|
|
2024-10-28 16:17:09 +01:00
|
|
|
import '../ambito_db/base_db.dart';
|
2024-09-18 15:07:08 +02:00
|
|
|
|
|
|
|
class BaseApi {
|
2024-10-21 15:01:20 +02:00
|
|
|
Map tables = {};
|
|
|
|
|
|
|
|
init() {
|
|
|
|
tables = jsonDecode(dotenv.get('BASEROW_IDS'));
|
|
|
|
}
|
|
|
|
|
2024-09-18 15:07:08 +02:00
|
|
|
Future<bool> getContent(String table) async {
|
2024-10-21 15:01:20 +02:00
|
|
|
init();
|
2024-09-18 15:07:08 +02:00
|
|
|
int tableId = tables[table] ?? 0;
|
|
|
|
if (tableId > 0) {
|
|
|
|
var response =
|
|
|
|
await RestClient().get('$tableId/?user_field_names=true&size=200');
|
|
|
|
if (response.statusCode == 200) {
|
2024-10-21 15:01:20 +02:00
|
|
|
var json = _jsonDecoded(response.body);
|
2024-09-18 15:07:08 +02:00
|
|
|
switch (table) {
|
|
|
|
case 'baumarten':
|
|
|
|
for (var baumart in json['results']) {
|
2024-10-21 15:01:20 +02:00
|
|
|
BaumartenRepository().put(Baumarten.fromJson(baumart));
|
2024-09-18 15:07:08 +02:00
|
|
|
}
|
|
|
|
break;
|
2024-10-28 16:17:09 +01:00
|
|
|
case 'measure':
|
|
|
|
for (var measure in json['results']) {
|
|
|
|
MeasureRepository().put(Measure.fromJson(measure));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'measure_combination':
|
|
|
|
for (var measure_combination in json['results']) {
|
|
|
|
MeasureCombinationRepository()
|
|
|
|
.put(MeasureCombination.fromJson(measure_combination));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'organism':
|
|
|
|
for (var organism in json['results']) {
|
|
|
|
OrganismRepository().put(Organism.fromJson(organism));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'funding_program':
|
|
|
|
for (var fundingProgram in json['results']) {
|
|
|
|
FundingProgramRepository()
|
|
|
|
.put(FundingProgram.fromJson(fundingProgram));
|
2024-09-18 15:07:08 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-10-21 15:01:20 +02:00
|
|
|
|
|
|
|
dynamic _jsonDecoded(String input) {
|
|
|
|
return jsonDecode(utf8.decode(input.runes.toList()));
|
|
|
|
}
|
2024-09-18 15:07:08 +02:00
|
|
|
}
|