Skip to content

Commit

Permalink
refactor: fix second instance api url
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Nov 27, 2023
1 parent e42f717 commit 49e77a8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
9 changes: 4 additions & 5 deletions lib/core/internals/deeplink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ abstract class InternalDeeplink {
resolvedPath = resolvedPath.replaceFirst(fullScheme, '');
}
debugPrint('Incoming deeplink: $resolvedPath');
final InternalRouteRequest routeReq =
InternalRouteRequest.fromRawPath(path);
final InternalRoute? internalRoute = InternalRoutes.findMatch(routeReq);
if (internalRoute != null) {
internalRoute.handle(routeReq);
final InternalRouteUri uri = InternalRouteUri.fromRawPath(path);
final InternalRoute? route = InternalRoutes.findMatch(uri);
if (route != null) {
route.handle(uri);
return;
}
gNavigatorKey.currentState!.pushNamed(resolvedPath);
Expand Down
29 changes: 22 additions & 7 deletions lib/core/internals/router/route.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
class InternalRouteRequest {
const InternalRouteRequest({
class InternalRouteUri {
const InternalRouteUri({
required this.path,
required this.queries,
required this.hash,
});

factory InternalRouteRequest.fromUri(final Uri uri) => InternalRouteRequest(
factory InternalRouteUri.fromUri(final Uri uri) => InternalRouteUri(
path: uri.path,
queries: uri.queryParameters,
hash: uri.fragment,
);

factory InternalRouteRequest.fromRawPath(final String path) =>
InternalRouteRequest.fromUri(Uri.parse('http://localhost$path'));
factory InternalRouteUri.fromRawPath(final String path) =>
InternalRouteUri.fromUri(Uri.parse('http://localhost$path'));

final String path;
final Map<String, String> queries;
final String hash;

@override
String toString() {
final StringBuffer output = StringBuffer(path);
if (queries.isNotEmpty) {
output.write('?');
queries.forEach((final String k, final String v) {
output.write('$k=${Uri.encodeQueryComponent(v)}');
});
}
if (hash.isNotEmpty) {
output.write('#${Uri.encodeQueryComponent(hash)}');
}
return output.toString();
}
}

abstract class InternalRoute {
bool matches(final InternalRouteRequest req);
Future<void> handle(final InternalRouteRequest req);
bool matches(final InternalRouteUri uri);
Future<void> handle(final InternalRouteUri uri);
}
4 changes: 2 additions & 2 deletions lib/core/internals/router/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ abstract class InternalRoutes {
static InternalSecondInstanceRoute ping = InternalSecondInstanceRoute();
static InternalAnilistAuthRoute anilistAuth = InternalAnilistAuthRoute();

static InternalRoute? findMatch(final InternalRouteRequest req) =>
all.firstWhereOrNull((final InternalRoute x) => x.matches(req));
static InternalRoute? findMatch(final InternalRouteUri uri) =>
all.firstWhereOrNull((final InternalRoute x) => x.matches(uri));

static List<InternalRoute> get all => <InternalRoute>[ping, anilistAuth];
}
6 changes: 3 additions & 3 deletions lib/core/internals/router/routes/anilist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import 'package:kazahana/core/internals/router/route.dart';

class InternalAnilistAuthRoute extends InternalRoute {
@override
bool matches(final InternalRouteRequest req) => req.path == routeName;
bool matches(final InternalRouteUri uri) => uri.path == routeName;

@override
Future<void> handle(final InternalRouteRequest req) async {
final AnilistToken token = AnilistToken.parseHash(req.hash);
Future<void> handle(final InternalRouteUri uri) async {
final AnilistToken token = AnilistToken.parseHash(uri.hash);
await AnilistAuth.authenticate(token);
}

Expand Down
18 changes: 13 additions & 5 deletions lib/core/internals/router/routes/second_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ import 'package:kazahana/core/internals/router/route.dart';

class InternalSecondInstanceRoute extends InternalRoute {
@override
bool matches(final InternalRouteRequest req) => req.path == routeName;
bool matches(final InternalRouteUri uri) => uri.path == routeName;

@override
Future<void> handle(final InternalRouteRequest req) async {
final String? deeplink = req.queries['deeplink'];
Future<void> handle(final InternalRouteUri uri) async {
final String? deeplink = uri.queries['deeplink'];
if (deeplink != null) {
InternalDeeplink.handle(deeplink);
}
}

static const String routeName = '/second-instance';

static String constructRouteName(final String deeplink) =>
'$routeName${Uri.encodeQueryComponent(deeplink)}';
static String constructRouteName(final String deeplink) {
final InternalRouteUri uri = InternalRouteUri(
path: routeName,
queries: <String, String>{
'deeplink': deeplink,
},
hash: '',
);
return uri.toString();
}
}
7 changes: 3 additions & 4 deletions lib/core/internals/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ abstract class InternalServer {
static Future<void> listen() async {
server = await HttpServer.bind(host, port);
server.forEach((final HttpRequest req) {
final InternalRouteRequest routeReq =
InternalRouteRequest.fromUri(req.uri);
final InternalRoute? route = InternalRoutes.findMatch(routeReq);
final InternalRouteUri uri = InternalRouteUri.fromUri(req.uri);
final InternalRoute? route = InternalRoutes.findMatch(uri);
if (route != null) {
route.handle(routeReq);
route.handle(uri);
req.response.statusCode = successCode;
req.response.close();
}
Expand Down

0 comments on commit 49e77a8

Please sign in to comment.