Skip to content

Commit

Permalink
Merge pull request #6 from La-Table-Ronde/fix/error-503-cdn
Browse files Browse the repository at this point in the history
fix: fixed a bug where when switching network error 503 would appear once again
  • Loading branch information
AdamLBS authored Mar 19, 2024
2 parents 549aee1 + 08f5629 commit a23b8c4
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/calendar_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class EventDataSource extends CalendarDataSource {
}

class CalendarWidget extends StatefulWidget {
const CalendarWidget({Key? key}) : super(key: key);
const CalendarWidget({super.key});

@override
State<CalendarWidget> createState() => _CalendarWidgetState();
Expand Down
28 changes: 3 additions & 25 deletions lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'package:workmanager/workmanager.dart';
import 'globals.dart' as globals;

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
const HomePage({super.key});

@override
State<HomePage> createState() => _HomePageState();
Expand Down Expand Up @@ -53,7 +53,7 @@ class _HomePageState extends State<HomePage> {
}

class HomePageLoggedIn extends StatefulWidget {
const HomePageLoggedIn({Key? key}) : super(key: key);
const HomePageLoggedIn({super.key});

@override
State<HomePageLoggedIn> createState() => _HomePageLoggedInState();
Expand Down Expand Up @@ -333,30 +333,8 @@ Future<Profile> getProfileData() async {
});

const url = 'https://intra.epitech.eu/user/?format=json';
HttpClient client = HttpClient();
HttpClientRequest clientRequest = await client.getUrl(Uri.parse(url));
clientRequest.cookies.addAll(cookieMap.entries
.map((e) => Cookie(e.key, e.value))
.toList(growable: false));
final metric =
FirebasePerformance.instance.newHttpMetric(url, HttpMethod.Get);
await metric.start();
HttpClientResponse response = await clientRequest.close();
await metric.stop();

if (response.statusCode != 200) {
return Future.error("Error${response.statusCode}");
}
final stringData = await response.transform(utf8.decoder).join();

final stringData = await fetchData(url);
final value = jsonDecode(stringData);
metric
..responseContentType = response.headers.contentType.toString()
..responsePayloadSize = utf8.encode(stringData).length
..requestPayloadSize = utf8.encode("").length
..httpResponseCode = response.statusCode
..putAttribute("request_payload", stringData);
await metric.stop();
Profile myProfile = Profile(
gpa: value['gpa'][0]['gpa'] == null
? "0"
Expand Down
5 changes: 2 additions & 3 deletions lib/home_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import 'globals.dart' as globals;

class HomeWidget extends StatefulWidget {
const HomeWidget(
{Key? key,
{super.key,
required this.data,
required this.projects,
this.notifications,
required this.index})
: super(key: key);
required this.index});
final Future<List<Projects>>? projects;
final Profile data;
final Future<List<Notifications>>? notifications;
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class _LoginIntraState extends State<LoginIntra> {
Workmanager().registerOneOffTask(
"check-connection", "check-connection-task");
}
if (context.mounted) {
if (mounted) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
Expand Down Expand Up @@ -359,7 +359,7 @@ Future<void> getNewCookie() async {
url: WebUri.uri(Uri.parse(
"https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id=e05d4149-1624-4627-a5ba-7472a39e43ab&redirect_uri=https%3A%2F%2Fintra.epitech.eu%2Fauth%2Foffice365&state=%2F&HSU=1&login_hint=$email"))),
onLoadStop: (controller, url) async {
if (url == Uri.parse("https://intra.epitech.eu/")) {
if (url == WebUri("https://intra.epitech.eu/")) {
loadingCompleter.complete();
var userCookie = await cookieManager.getCookies(
url: WebUri.uri(Uri.parse("https://intra.epitech.eu/")));
Expand Down
5 changes: 2 additions & 3 deletions lib/notifications_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import 'package:shared_preferences/shared_preferences.dart';
// ignore: must_be_immutable
class NotificationsWidget extends StatefulWidget {
NotificationsWidget(
{Key? key,
{super.key,
required this.notifications,
this.projects,
required this.data})
: super(key: key);
required this.data});
Future<List<Projects>>? projects;
final Profile data;
Future<List<Notifications>>? notifications;
Expand Down
2 changes: 1 addition & 1 deletion lib/onboarding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:my_intra/main.dart';
import 'globals.dart' as globals;

class OnboardingWidget extends StatefulWidget {
const OnboardingWidget({Key? key}) : super(key: key);
const OnboardingWidget({super.key});

@override
State<OnboardingWidget> createState() => _OnboardingWidgetState();
Expand Down
3 changes: 1 addition & 2 deletions lib/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import 'package:pie_chart/pie_chart.dart';
import 'globals.dart' as globals;

class ProfilePage extends StatefulWidget {
const ProfilePage({Key? key, required this.data, required this.scaffoldKey})
: super(key: key);
const ProfilePage({super.key, required this.data, required this.scaffoldKey});
final Profile data;
final GlobalKey<ScaffoldState> scaffoldKey;

Expand Down
24 changes: 19 additions & 5 deletions lib/utils/fetch_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'dart:convert';
import 'dart:io';

import 'package:firebase_performance/firebase_performance.dart';
import 'package:flutter/foundation.dart';
import 'package:my_intra/main.dart';
import 'package:shared_preferences/shared_preferences.dart';

Future<String> fetchData(String url) async {
Expand All @@ -16,6 +18,8 @@ Future<String> fetchData(String url) async {
});
HttpClient client = HttpClient();
HttpClientRequest clientRequest = await client.getUrl(Uri.parse(url));
clientRequest.headers.set('User-Agent',
'Mozilla/5.0 (Linux; Android 10; SM-G960F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Mobile Safari/537.36');
clientRequest.cookies.addAll(cookieMap.entries
.map((e) => Cookie(e.key, e.value))
.toList(growable: false));
Expand All @@ -32,13 +36,23 @@ Future<String> fetchData(String url) async {
..putAttribute("request_payload", stringData);

await metric.stop();
if (response.statusCode != 200) {
return Future.error("Error${response.statusCode}");
if (response.statusCode == 503) {
if (kDebugMode) {
print("CDN Blocked us getting new cookie");
}
await getNewCookie();
if (kDebugMode) {
print("got it");
}
return await fetchData(url);
} else if (response.statusCode != 200) {
return Future.error("Error : ${response.statusCode}");
}
return stringData;
}

Future<HttpClientResponse> postData(String url, Map<String, String>? body) async {
Future<HttpClientResponse> postData(
String url, Map<String, String>? body) async {
final prefs = await SharedPreferences.getInstance();
String? cookies = prefs.getString("cookies");
if (cookies == null) {
Expand Down Expand Up @@ -71,7 +85,7 @@ Future<HttpClientResponse> postData(String url, Map<String, String>? body) async
return response;
}

Future<List<int>> fetchBytes (String url) async {
Future<List<int>> fetchBytes(String url) async {
final prefs = await SharedPreferences.getInstance();
String? cookies = prefs.getString("cookies");
if (cookies == null) {
Expand All @@ -97,4 +111,4 @@ Future<List<int>> fetchBytes (String url) async {
return Future.error("Error${response.statusCode}");
}
return bytes;
}
}
54 changes: 43 additions & 11 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,10 @@ packages:
dependency: "direct main"
description:
name: google_fonts
sha256: "5b1726fee554d1cc9db1baef8061b126567ff0a1140a03ed7de936e62f2ab98b"
sha256: b1ac0fe2832c9cc95e5e88b57d627c5e68c223b9657f4b96e1487aa9098c7b82
url: "https://pub.dev"
source: hosted
version: "6.2.0"
version: "6.2.1"
html:
dependency: "direct main"
description:
Expand Down Expand Up @@ -480,6 +480,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.7"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "2.0.1"
lints:
dependency: transitive
description:
Expand All @@ -492,26 +516,26 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.16+1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.11.0"
open_file_plus:
dependency: "direct main"
description:
Expand All @@ -524,10 +548,10 @@ packages:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.9.0"
path_parsing:
dependency: transitive
description:
Expand Down Expand Up @@ -893,6 +917,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
vm_service:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "13.0.0"
web:
dependency: transitive
description:
Expand Down Expand Up @@ -975,4 +1007,4 @@ packages:
version: "6.5.0"
sdks:
dart: ">=3.2.0 <4.0.0"
flutter: ">=3.16.0"
flutter: ">=3.19.2"
3 changes: 2 additions & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_intra/main.dart';

void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(const LoginIntra());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down

0 comments on commit a23b8c4

Please sign in to comment.