Line data Source code
1 : import 'dart:io';
2 :
3 : import 'package:app_pym/core/error/exceptions.dart';
4 : import 'package:flutter/foundation.dart';
5 : import 'package:http/http.dart' as http;
6 : import 'package:injectable/injectable.dart';
7 :
8 : abstract class MetropoleRemoteDataSource {
9 : Stream<List<int>> download();
10 :
11 : Future<DateTime> get timestamp;
12 : }
13 :
14 : /// [Not used] Remote data source using lepilote
15 : ///
16 : /// Couldn't be used during the Covid-19.
17 : @prod
18 : @LazySingleton(as: MetropoleRemoteDataSource)
19 : class MetropoleRemoteDataSourceImpl implements MetropoleRemoteDataSource {
20 : final http.Client client;
21 :
22 1 : MetropoleRemoteDataSourceImpl({@required this.client});
23 :
24 : @override
25 1 : Stream<List<int>> download() async* {
26 : // Download file
27 1 : final http.Request request = http.Request(
28 : 'GET',
29 1 : Uri.parse(
30 : "http://tsvc.pilote4.cityway.fr/api/Export/v1/GetExportedDataFile?ExportFormat=Gtfs&OperatorCode=CPA"),
31 : );
32 3 : final http.StreamedResponse streamedResponse = await client.send(request);
33 :
34 2 : if (streamedResponse.statusCode != HttpStatus.ok) {
35 1 : throw ServerException(
36 3 : 'http://tsvc.pilote4.cityway.fr/api/: ${streamedResponse.statusCode} ${streamedResponse.reasonPhrase}');
37 : }
38 :
39 2 : yield* streamedResponse.stream;
40 : }
41 :
42 : @override
43 1 : Future<DateTime> get timestamp async {
44 : // Get zip url
45 1 : final http.Request request = http.Request(
46 : 'GET',
47 1 : Uri.parse(
48 : "http://tsvc.pilote4.cityway.fr/api/Export/v1/GetExportedDataFile?ExportFormat=Gtfs&OperatorCode=CPA"),
49 : );
50 3 : final http.StreamedResponse streamedResponse = await client.send(request);
51 :
52 2 : if (streamedResponse.statusCode != HttpStatus.ok) {
53 1 : throw ServerException(
54 3 : 'http://tsvc.pilote4.cityway.fr/api/: ${streamedResponse.statusCode} ${streamedResponse.reasonPhrase}');
55 : }
56 :
57 2 : final contentDisposition = streamedResponse.headers[
58 : 'content-disposition']; // attachment; filename=Gtfs_CPA___2020-05-27.zip
59 :
60 2 : return DateTime.parse(contentDisposition.substring(32, 42));
61 : }
62 : }
|