Skip to content

Commit 2876a1a

Browse files
committed
Add HTTP dependency and implement
1 parent a417214 commit 2876a1a

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

analysis_options.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
# The following line activates a set of recommended lints for Flutter apps,
99
# packages, and plugins designed to encourage good coding practices.
10+
analyzer:
11+
errors:
12+
depend_on_referenced_packages: ignore
1013
include: package:flutter_lints/flutter.yaml
1114

1215
linter:

lib/service.dart

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// This code is just example code to show how mimic app work
2+
// This code is not working and just for educational purpose
3+
4+
import 'dart:async';
5+
import 'dart:math';
6+
import 'package:http/http.dart' as http;
7+
import 'dart:convert';
8+
9+
// ignore: non_constant_identifier_names
10+
final Map<String, dynamic> CONFIG = {
11+
'base_url': 'https://app.ofppt-langues.ma',
12+
'heartbeat_settings': {
13+
'min_interval': 5,
14+
'max_interval': 45
15+
},
16+
'user_agents': [
17+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
18+
]
19+
};
20+
21+
class PlatformActivitySimulator {
22+
final String? authToken;
23+
final String? deviceId;
24+
final String baseUrl;
25+
bool _isRunning = false;
26+
Timer? _heartbeatTimer;
27+
final Random _random = Random();
28+
29+
PlatformActivitySimulator({this.authToken, this.deviceId})
30+
: baseUrl = CONFIG['base_url'] {
31+
// Logger.root.level = Level.ALL;
32+
}
33+
34+
35+
Map<String, String> _getDefaultHeaders() {
36+
return {
37+
'accept': '*/*',
38+
'content-type': 'application/json',
39+
'User-Agent': (CONFIG['user_agents'] as List).first,
40+
'x-auth-token': authToken ?? '',
41+
'x-device-id': deviceId ?? ''
42+
};
43+
}
44+
45+
Future<bool> sendHeartbeat(String eventType) async {
46+
final String endpoint = '$baseUrl/gw/eventapi/main/api/event/internal/events';
47+
final payload = {
48+
'action': 'platform.application.$eventType',
49+
'timeZone': 'UTC'
50+
};
51+
52+
try {
53+
for (int attempt = 0; attempt < 3; attempt++) {
54+
try {
55+
final response = await http.post(
56+
Uri.parse(endpoint),
57+
headers: _getDefaultHeaders(),
58+
body: jsonEncode(payload),
59+
);
60+
61+
return response.statusCode == 200;
62+
} catch (e) {
63+
if (attempt == 2) rethrow;
64+
await Future.delayed(Duration(seconds: pow(2, attempt).toInt()));
65+
}
66+
}
67+
} catch (e) {
68+
return false;
69+
}
70+
return false;
71+
}
72+
73+
void start() {
74+
if (_isRunning) return;
75+
_isRunning = true;
76+
_runHeartbeat();
77+
}
78+
79+
void stop() {
80+
_isRunning = false;
81+
_heartbeatTimer?.cancel();
82+
sendHeartbeat('dead').then((_) {
83+
});
84+
}
85+
86+
void _runHeartbeat() async {
87+
while (_isRunning) {
88+
await sendHeartbeat('alive');
89+
90+
final interval = _random.nextDouble() *
91+
(CONFIG['heartbeat_settings']['max_interval'] -
92+
CONFIG['heartbeat_settings']['min_interval']) +
93+
CONFIG['heartbeat_settings']['min_interval'];
94+
95+
await Future.delayed(Duration(seconds: interval.toInt()));
96+
}
97+
}
98+
}
99+
100+
void main() {
101+
final simulator = PlatformActivitySimulator(
102+
authToken: 'your_auth_token_here',
103+
deviceId: 'your_device_id_here'
104+
);
105+
106+
simulator.start();
107+
108+
Future.delayed(const Duration(minutes: 1), () {
109+
simulator.stop();
110+
});
111+
}

pubspec.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ packages:
7575
description: flutter
7676
source: sdk
7777
version: "0.0.0"
78+
http:
79+
dependency: "direct main"
80+
description:
81+
name: http
82+
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
83+
url: "https://pub.dev"
84+
source: hosted
85+
version: "1.2.2"
86+
http_parser:
87+
dependency: transitive
88+
description:
89+
name: http_parser
90+
sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571"
91+
url: "https://pub.dev"
92+
source: hosted
93+
version: "4.1.2"
7894
leak_tracker:
7995
dependency: transitive
8096
description:
@@ -192,6 +208,14 @@ packages:
192208
url: "https://pub.dev"
193209
source: hosted
194210
version: "0.7.3"
211+
typed_data:
212+
dependency: transitive
213+
description:
214+
name: typed_data
215+
sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
216+
url: "https://pub.dev"
217+
source: hosted
218+
version: "1.4.0"
195219
vector_math:
196220
dependency: transitive
197221
description:
@@ -208,6 +232,14 @@ packages:
208232
url: "https://pub.dev"
209233
source: hosted
210234
version: "14.3.0"
235+
web:
236+
dependency: transitive
237+
description:
238+
name: web
239+
sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb
240+
url: "https://pub.dev"
241+
source: hosted
242+
version: "1.1.0"
211243
sdks:
212244
dart: ">=3.6.0 <4.0.0"
213245
flutter: ">=3.18.0-18.0.pre.54"

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies:
3434
# The following adds the Cupertino Icons font to your application.
3535
# Use with the CupertinoIcons class for iOS style icons.
3636
cupertino_icons: ^1.0.8
37+
http: ^1.2.2
3738

3839
dev_dependencies:
3940
flutter_test:

0 commit comments

Comments
 (0)