Skip to content

Commit

Permalink
Replace all host and Repository.displayHost with Repository.host
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-Andrieu committed Oct 20, 2024
1 parent f68b159 commit 34218a0
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/admin/repositories/group_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GroupRepository extends Repository {

Future<bool> deleteMember(Group group, SimpleUser user) async {
final response = await http.delete(
Uri.parse("$host${ext}membership"),
Uri.parse("${Repository.host}${ext}membership"),
headers: headers,
body: json.encode({"user_id": user.id, "group_id": group.id}),
);
Expand Down
3 changes: 1 addition & 2 deletions lib/auth/providers/is_connected_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class IsConnectedProvider extends StateNotifier<bool> {

Future isInternet() async {
try {
final result =
await http.get(Uri.parse("${Repository.displayHost}information"));
final result = await http.get(Uri.parse("${Repository.host}information"));
state = result.statusCode < 400;
} catch (e) {
state = false;
Expand Down
4 changes: 2 additions & 2 deletions lib/auth/providers/openid_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class OpenIdTokenProvider
final String redirectUrl = "${getTitanPackageName()}://authorized";
final String redirectUrlHost = "myecl.fr";
final String discoveryUrl =
"${Repository.displayHost}.well-known/openid-configuration";
"${Repository.host}.well-known/openid-configuration";
final List<String> scopes = ["API"];
OpenIdTokenProvider() : super(const AsyncValue.loading());

Expand All @@ -147,7 +147,7 @@ class OpenIdTokenProvider
final codeVerifier = generateRandomString(128);

final authUrl =
"${Repository.displayHost}auth/authorize?client_id=$clientId&response_type=code&scope=${scopes.join(" ")}&redirect_uri=$redirectUri&code_challenge=${hash(codeVerifier)}&code_challenge_method=S256";
"${Repository.host}auth/authorize?client_id=$clientId&response_type=code&scope=${scopes.join(" ")}&redirect_uri=$redirectUri&code_challenge=${hash(codeVerifier)}&code_challenge_method=S256";

state = const AsyncValue.loading();
try {
Expand Down
3 changes: 2 additions & 1 deletion lib/auth/repository/openid_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class OpenIdRepository extends Repository {
};
try {
final response = await http
.post(Uri.parse("${host}auth/token"), headers: headers, body: body)
.post(Uri.parse("${Repository.host}auth/token"),
headers: headers, body: body)
.timeout(const Duration(seconds: 5));
if (response.statusCode == 200) {
final token = jsonDecode(response.body)["access_token"];
Expand Down
4 changes: 2 additions & 2 deletions lib/settings/ui/pages/main_page/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class SettingsMainPage extends HookConsumerWidget {
onTap: () {
Clipboard.setData(
ClipboardData(
text: "${Repository.displayHost}calendar/ical",
text: "${Repository.host}calendar/ical",
),
).then((value) {
displayToastWithContext(
Expand Down Expand Up @@ -295,7 +295,7 @@ class SettingsMainPage extends HookConsumerWidget {
),
const SizedBox(height: 10),
AutoSizeText(
Repository.displayHost,
Repository.host,
maxLines: 1,
minFontSize: 10,
style: const TextStyle(
Expand Down
26 changes: 13 additions & 13 deletions lib/tools/repository/logo_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ abstract class LogoRepository extends Repository {

Future<Uint8List> getLogo(String id, {String suffix = ""}) async {
try {
final response =
await http.get(Uri.parse("$host$ext$id$suffix"), headers: headers);
final response = await http
.get(Uri.parse("${Repository.host}$ext$id$suffix"), headers: headers);
if (response.statusCode == 200) {
try {
return response.bodyBytes;
Expand Down Expand Up @@ -57,17 +57,17 @@ abstract class LogoRepository extends Repository {
String id, {
String suffix = "",
}) async {
final request =
http.MultipartRequest('POST', Uri.parse("$host$ext$id$suffix"))
..headers.addAll(headers)
..files.add(
http.MultipartFile.fromBytes(
'image',
bytes,
filename: 'image',
contentType: MediaType('image', 'jpeg'),
),
);
final request = http.MultipartRequest(
'POST', Uri.parse("${Repository.host}$ext$id$suffix"))
..headers.addAll(headers)
..files.add(
http.MultipartFile.fromBytes(
'image',
bytes,
filename: 'image',
contentType: MediaType('image', 'jpeg'),
),
);
final response = await request.send();
response.stream.transform(utf8.decoder).listen((value) async {
if (response.statusCode == 201) {
Expand Down
26 changes: 13 additions & 13 deletions lib/tools/repository/pdf_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ abstract class PdfRepository extends Repository {

Future<Uint8List> getPdf(String id, {String suffix = ""}) async {
try {
final response =
await http.get(Uri.parse("$host$ext$id$suffix"), headers: headers);
final response = await http
.get(Uri.parse("${Repository.host}$ext$id$suffix"), headers: headers);
if (response.statusCode == 200) {
try {
return response.bodyBytes;
Expand Down Expand Up @@ -72,17 +72,17 @@ abstract class PdfRepository extends Repository {
String id, {
String suffix = "",
}) async {
final request =
http.MultipartRequest('POST', Uri.parse("$host$ext$id$suffix"))
..headers.addAll(headers)
..files.add(
http.MultipartFile.fromBytes(
'pdf',
bytes,
filename: 'pdf',
contentType: MediaType('application', 'pdf'),
),
);
final request = http.MultipartRequest(
'POST', Uri.parse("${Repository.host}$ext$id$suffix"))
..headers.addAll(headers)
..files.add(
http.MultipartFile.fromBytes(
'pdf',
bytes,
filename: 'pdf',
contentType: MediaType('application', 'pdf'),
),
);
final response = await request.send();
response.stream.transform(utf8.decoder).listen((value) async {
if (response.statusCode == 201) {
Expand Down
36 changes: 9 additions & 27 deletions lib/tools/repository/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import 'package:myecl/tools/functions.dart';
import 'package:myecl/tools/logs/logger.dart';

abstract class Repository {
final String host = getTitanHost();

static final String displayHost = getTitanHost();
static final String host = getTitanHost();
static const String expiredTokenDetail = "Could not validate credentials";
final String ext = "";
final Map<String, String> headers = {
Expand All @@ -36,9 +34,7 @@ abstract class Repository {
if (response.statusCode == 200) {
try {
String toDecode = response.body;
if (host == displayHost) {
toDecode = utf8.decode(response.body.runes.toList());
}
toDecode = utf8.decode(response.body.runes.toList());
if (!kIsWeb) {
cacheManager.writeCache(ext + suffix, toDecode);
}
Expand All @@ -55,9 +51,7 @@ abstract class Repository {
);
try {
String toDecode = response.body;
if (host == displayHost) {
toDecode = utf8.decode(response.body.runes.toList());
}
toDecode = utf8.decode(response.body.runes.toList());
final decoded = jsonDecode(toDecode);
if (decoded["detail"] == expiredTokenDetail) {
throw AppException(ErrorType.tokenExpire, decoded["detail"]);
Expand Down Expand Up @@ -113,9 +107,7 @@ abstract class Repository {
if (response.statusCode == 200) {
try {
String toDecode = response.body;
if (host == displayHost || decode) {
toDecode = utf8.decode(response.body.runes.toList());
}
toDecode = utf8.decode(response.body.runes.toList());
if (!kIsWeb) {
cacheManager.writeCache(ext + id + suffix, toDecode);
}
Expand All @@ -132,9 +124,7 @@ abstract class Repository {
);
try {
String toDecode = response.body;
if (host == displayHost) {
toDecode = utf8.decode(response.body.runes.toList());
}
toDecode = utf8.decode(response.body.runes.toList());
final decoded = jsonDecode(toDecode);
if (decoded["detail"] == expiredTokenDetail) {
throw AppException(ErrorType.tokenExpire, decoded["detail"]);
Expand Down Expand Up @@ -187,9 +177,7 @@ abstract class Repository {
if (response.statusCode == 201) {
try {
String toDecode = response.body;
if (host == displayHost) {
toDecode = utf8.decode(response.body.runes.toList());
}
toDecode = utf8.decode(response.body.runes.toList());
return jsonDecode(toDecode);
} catch (e) {
logger.error(
Expand All @@ -204,9 +192,7 @@ abstract class Repository {
"POST ${ext + suffix}\n${response.statusCode} ${response.body}",
);
String toDecode = response.body;
if (host == displayHost) {
toDecode = utf8.decode(response.body.runes.toList());
}
toDecode = utf8.decode(response.body.runes.toList());
final decoded = jsonDecode(toDecode);
if (decoded["detail"] == expiredTokenDetail) {
throw AppException(ErrorType.tokenExpire, decoded["detail"]);
Expand Down Expand Up @@ -236,9 +222,7 @@ abstract class Repository {
"PATCH ${ext + tId + suffix}\n${response.statusCode} ${response.body}",
);
String toDecode = response.body;
if (host == displayHost) {
toDecode = utf8.decode(response.body.runes.toList());
}
toDecode = utf8.decode(response.body.runes.toList());
final decoded = jsonDecode(toDecode);
if (decoded["detail"] == expiredTokenDetail) {
throw AppException(ErrorType.tokenExpire, decoded["detail"]);
Expand Down Expand Up @@ -266,9 +250,7 @@ abstract class Repository {
"DELETE ${ext + tId + suffix}\n${response.statusCode} ${response.body}",
);
String toDecode = response.body;
if (host == displayHost) {
toDecode = utf8.decode(response.body.runes.toList());
}
toDecode = utf8.decode(response.body.runes.toList());
final decoded = jsonDecode(toDecode);
if (decoded["detail"] == expiredTokenDetail) {
throw AppException(ErrorType.tokenExpire, decoded["detail"]);
Expand Down

0 comments on commit 34218a0

Please sign in to comment.