-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
export 'reports/germany_nrw.dart'; | ||
import '../../common.dart'; | ||
|
||
export 'apis/germany_nrw.dart'; | ||
|
||
enum WrestlingApiProvider { | ||
deNwRingenApi; | ||
|
||
WrestlingApi get api { | ||
switch (this) { | ||
case WrestlingApiProvider.deNwRingenApi: | ||
throw UnimplementedError(); | ||
return NrwGermanyWrestlingApi(); | ||
} | ||
} | ||
} | ||
|
||
/// Abstraction for providing an api interface. | ||
abstract class WrestlingApi {} | ||
abstract class WrestlingApi { | ||
Future<List<League>> importLeagues({int? season}); | ||
} |
93 changes: 93 additions & 0 deletions
93
wrestling_scoreboard_common/lib/src/services/apis/germany_nrw.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart' as http; | ||
|
||
import '../../../common.dart'; | ||
|
||
extension NrwGermanyLeague on League { | ||
static Future<League> fromJson(Map<String, dynamic> e, GetSingleOfTypeCallback getSingle) async { | ||
final boutConfig = await getSingle<BoutConfig>(e['boutSchemeId'] as int); | ||
return League( | ||
id: e['id'] as int?, | ||
name: e['name'] as String, | ||
startDate: e['start_date'] as DateTime, | ||
seasonPartitions: e['season_partitions'] as int, | ||
boutConfig: boutConfig, | ||
); | ||
} | ||
} | ||
|
||
class NrwGermanyWrestlingApi extends WrestlingApi { | ||
final _apiUrl = 'https://www.brv-ringen.de/Api/v1/cs/'; | ||
|
||
@override | ||
Future<List<League>> importLeagues({int? season}) async { | ||
return await _getLeagueList(seasonId: season?.toString()); | ||
} | ||
|
||
static Future<T> getSingleFromDataType<T extends DataObject>(int id) { | ||
// return getControllerFromDataType(T).getSingle(id) as Future<T>; | ||
throw UnimplementedError(); | ||
} | ||
|
||
/// Get all seasons | ||
Future<List<String>> _getSeasonList() async { | ||
final uri = Uri.parse(_apiUrl).replace(queryParameters: { | ||
'op': 'listSaison', | ||
}); | ||
final response = await http.get(uri); | ||
if (response.statusCode == 200) { | ||
final Map<String, dynamic> json = jsonDecode(response.body); | ||
final Map<String, dynamic> competitionList = json['ligaList']; | ||
return await Future.wait(competitionList.entries.map((entry) async => entry.value.toString())); | ||
} else { | ||
throw Exception('Failed to get the saison list: ${response.reasonPhrase ?? response.statusCode.toString()}'); | ||
} | ||
} | ||
|
||
/// Get leagues of a season | ||
Future<List<League>> _getLeagueList({ | ||
String? seasonId, | ||
}) async { | ||
seasonId ??= DateTime.now().year.toString(); | ||
final uri = Uri.parse(_apiUrl).replace(queryParameters: { | ||
'op': 'listLiga', | ||
'sid': seasonId, | ||
}); | ||
final response = await http.get(uri); | ||
if (response.statusCode == 200) { | ||
final Map<String, dynamic> json = jsonDecode(response.body); | ||
final Map<String, dynamic> ligaList = json['ligaList']; | ||
return await Future.wait( | ||
ligaList.entries.map((entry) => NrwGermanyLeague.fromJson(entry.value, getSingleFromDataType))); | ||
} else { | ||
throw Exception( | ||
'Failed to get the liga list (seasonId: $seasonId): ${response.reasonPhrase ?? response.statusCode.toString()}'); | ||
} | ||
} | ||
|
||
/// Get team matches of a league | ||
Future<List<League>> _getCompetitionList({ | ||
String? seasonId, | ||
String ligaId = 'Gruppenliga', | ||
String regionId = 'Süd', | ||
}) async { | ||
seasonId ??= DateTime.now().year.toString(); | ||
final uri = Uri.parse(_apiUrl).replace(queryParameters: { | ||
'op': 'listCompetition', | ||
'sid': seasonId, | ||
'ligaId': ligaId, | ||
'rid': regionId, | ||
}); | ||
final response = await http.get(uri); | ||
if (response.statusCode == 200) { | ||
final Map<String, dynamic> json = jsonDecode(response.body); | ||
final Map<String, dynamic> competitionList = json['competitionList']; | ||
return await Future.wait( | ||
competitionList.entries.map((entry) => NrwGermanyLeague.fromJson(entry.value, getSingleFromDataType))); | ||
} else { | ||
throw Exception( | ||
'Failed to get the competition list (seasonId: $seasonId, ligaId: $ligaId, rid: $regionId): ${response.reasonPhrase ?? response.statusCode.toString()}'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:test/test.dart'; | ||
import 'package:wrestling_scoreboard_common/common.dart'; | ||
|
||
void main() { | ||
group('APIs', () { | ||
test('Germany, NRW', () async { | ||
final wrestlingApi = WrestlingApiProvider.deNwRingenApi.api; | ||
final leagues = await wrestlingApi.importLeagues(season: 2023); | ||
expect( | ||
leagues, | ||
'', | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
wrestling_scoreboard_server/lib/controllers/service_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:shelf/shelf.dart'; | ||
import 'package:wrestling_scoreboard_common/common.dart'; | ||
|
||
import 'entity_controller.dart'; | ||
|
||
class ServiceController { | ||
Future<Response> import(Request request, String provider) async { | ||
final apiProvider = WrestlingApiProvider.values.byName(provider); | ||
try { | ||
final leagues = await apiProvider.api.importLeagues(season: 2023); | ||
// Iterable<EntityController> entityControllers = | ||
// dataTypes.map((t) => EntityController.getControllerFromDataType(t)); | ||
// await Future.forEach(entityControllers, (e) => e.deleteMany()); | ||
return Response.ok('{"status": "success"}'); | ||
} catch (err) { | ||
return Response.internalServerError(body: '{"err": "$err"}'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters