Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Update routes.dart to automatically configure root url
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobortolan committed Nov 26, 2023
1 parent 2156e6a commit c6de460
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 45 deletions.
18 changes: 15 additions & 3 deletions lib/base/helpers/model_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ class ModelGenerator {
final courses = List.generate(
random.nextInt(5) + 1,
(index) => Course(
id: index, name: 'Course$index', teachingTerm: 'W', year: 2023),
id: index,
name: 'Course$index',
teachingTerm: 'W',
year: 2023,
),
);
final administeredCourses = List.generate(
random.nextInt(3) + 1,
(index) => Course(
id: index, name: 'Course$index', teachingTerm: 'W', year: 2023),
id: index,
name: 'Course$index',
teachingTerm: 'W',
year: 2023,
),
);
final pinnedCourses = List.generate(
random.nextInt(3) + 1,
(index) => Course(
id: index, name: 'Course$index', teachingTerm: 'W', year: 2023),
id: index,
name: 'Course$index',
teachingTerm: 'W',
year: 2023,
),
);
final settings = List.generate(
random.nextInt(3) + 1,
Expand Down
2 changes: 1 addition & 1 deletion lib/base/networking/api/auth_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AuthHandler {
String username,
String password,
) async {
const url = Routes.basicLogin;
var url = Routes.basicLogin;
final cookieJar = CookieJar();
final dio = Dio(
BaseOptions(
Expand Down
76 changes: 35 additions & 41 deletions lib/routes.dart
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
// ignore_for_file: constant_identifier_names
import 'dart:io';

class AppConfig {
AppConfig._(); // Private constructor

// Determine the root URL based on the platform
// Used for development only. Once the api is deployed, this can be ignored.
static String get _rootUrl {
if (Platform.isAndroid) {
return 'http://10.0.2.2:8081/api';
} else if (Platform.isIOS) {
return 'http://localhost:8081/api';
}
throw UnsupportedError('Unsupported platform');
}

// AUTHENTICATION
static String get basicAuthUrl =>
'${_rootUrl.replaceFirst('/api', '')}/login';
static String get ssoAuthUrl => 'https://live.rbg.tum.de/saml/out';
static String get ssoRedirectUrl => 'https://live.rbg.tum.de';

// NOTE: 10.0.2.2 is the android emulator's alias for the loopback address on
// the host machine. On iOS, use either localhost or the actual IP address
// of the host machine.

// API ROOT URL
const _ROOT_URL = 'http://10.0.2.2:8081/api';
const _GRPC_HOST = '10.0.2.2';
const _GRPC_PORT = 12544;

// AUTHENTICATION
const _BASIC_AUTH_URL = 'http://10.0.2.2:8081/login';
const _SSO_AUTH_URL = 'https://live.rbg.tum.de/saml/out';
const _SSO_REDIRECT_URL = 'https://live.rbg.tum.de';

// COURSES
const _COURSES_PATH = '/courses';
const _COURSES_LIVE_PATH = '/courses/live';
const _COURSES_USER_PATH = '/courses/user';
const _COURSES_USER_PINNED_PATH = '/courses/user/pinned';
const _COURSES_PUBLIC_PATH = '/courses/public';

// SEMESTERS
const _SEMESTERS_PATH = '/semesters';
// gRPC routes
static String get grpcHost {
return Platform.isAndroid
? '10.0.2.2'
: 'localhost'; // Or host machine IP for iOS
}

// USER AND SERVER NOTIFICATIONS
const _NOTIFICATIONS_USER_PATH = '/notifications';
const _NOTIFICATIONS_SERVER_PATH = '/notifications/server';
static const int grpcPort = 12544;
}

class Routes {
// HTTP routes
static const basicLogin = _BASIC_AUTH_URL;
static const ssoLogin = _SSO_AUTH_URL;
static const ssoRedirect = _SSO_REDIRECT_URL;
static const courses = _ROOT_URL + _COURSES_PATH;
static const coursesLive = _ROOT_URL + _COURSES_LIVE_PATH;
static const coursesUser = _ROOT_URL + _COURSES_USER_PATH;
static const coursesUserPinned = _ROOT_URL + _COURSES_USER_PINNED_PATH;
static const coursesPublic = _ROOT_URL + _COURSES_PUBLIC_PATH;
static const semesters = _ROOT_URL + _SEMESTERS_PATH;
static const notificationsUser = _ROOT_URL + _NOTIFICATIONS_USER_PATH;
static const notificationsServer = _ROOT_URL + _NOTIFICATIONS_SERVER_PATH;
static String get basicLogin => AppConfig.basicAuthUrl;
static String get ssoLogin => AppConfig.ssoAuthUrl;
static String get ssoRedirect => AppConfig.ssoRedirectUrl;

// gRPC routes
static const grpcHost = _GRPC_HOST;
static const grpcPort = _GRPC_PORT;
// gRPC config
static String get grpcHost => AppConfig.grpcHost;
static const int grpcPort = AppConfig.grpcPort;
}

0 comments on commit c6de460

Please sign in to comment.