mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-11 02:08:03 -06:00
FEATURE dart format
This commit is contained in:
@@ -14,50 +14,41 @@ import 'package:shelf_router/shelf_router.dart' as shelf_router;
|
|||||||
|
|
||||||
import 'package:flutter/services.dart' show rootBundle;
|
import 'package:flutter/services.dart' show rootBundle;
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
Logger log = Logger('LocalMockApiServer');
|
Logger log = Logger('LocalMockApiServer');
|
||||||
|
|
||||||
|
|
||||||
class LocalMockApiServer {
|
class LocalMockApiServer {
|
||||||
|
|
||||||
static final host = 'localhost';
|
static final host = 'localhost';
|
||||||
|
|
||||||
static final port = 3131;
|
static final port = 3131;
|
||||||
|
|
||||||
static get baseUrl => 'http://$host:$port/';
|
static get baseUrl => 'http://$host:$port/';
|
||||||
|
|
||||||
|
|
||||||
late shelf_router.Router app;
|
late shelf_router.Router app;
|
||||||
Future<Map<String, dynamic>> loadFixture(String name) async {
|
Future<Map<String, dynamic>> loadFixture(String name) async {
|
||||||
var fixture = await rootBundle.loadString('packages/mock_server/fixtures/$name.json');
|
var fixture =
|
||||||
|
await rootBundle.loadString('packages/mock_server/fixtures/$name.json');
|
||||||
return json.decode(fixture);
|
return json.decode(fixture);
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalMockApiServer() {
|
LocalMockApiServer() {
|
||||||
|
|
||||||
app = shelf_router.Router();
|
app = shelf_router.Router();
|
||||||
|
|
||||||
Map<String, dynamic> createdTags = {};
|
Map<String, dynamic> createdTags = {};
|
||||||
|
|
||||||
|
|
||||||
app.get('/api/', (Request req) async {
|
app.get('/api/', (Request req) async {
|
||||||
log.info('Responding to /api');
|
log.info('Responding to /api');
|
||||||
return JsonMockResponse.ok({
|
return JsonMockResponse.ok({});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/api/token/', (Request req) async {
|
app.post('/api/token/', (Request req) async {
|
||||||
log.info('Responding to /api/token/');
|
log.info('Responding to /api/token/');
|
||||||
var body = await req.bodyJsonMap();
|
var body = await req.bodyJsonMap();
|
||||||
if (body?['username'] == 'admin' && body?['password'] == 'test') {
|
if (body?['username'] == 'admin' && body?['password'] == 'test') {
|
||||||
return JsonMockResponse.ok({
|
return JsonMockResponse.ok({'token': 'testToken'});
|
||||||
'token': 'testToken'
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
return Response.unauthorized(
|
return Response.unauthorized('Unauthorized');
|
||||||
'Unauthorized'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/api/ui_settings/', (Request req) async {
|
app.get('/api/ui_settings/', (Request req) async {
|
||||||
@@ -113,23 +104,21 @@ class LocalMockApiServer {
|
|||||||
"slug": body?['name'],
|
"slug": body?['name'],
|
||||||
"name": body?['name'],
|
"name": body?['name'],
|
||||||
"color": body?['color'],
|
"color": body?['color'],
|
||||||
"text_color":"#000000",
|
"text_color": "#000000",
|
||||||
"match":"",
|
"match": "",
|
||||||
"matching_algorithm": body?['matching_algorithm'],
|
"matching_algorithm": body?['matching_algorithm'],
|
||||||
"is_insensitive": body?['is_insensitive'],
|
"is_insensitive": body?['is_insensitive'],
|
||||||
"is_inbox_tag":false,
|
"is_inbox_tag": false,
|
||||||
"owner":1,
|
"owner": 1,
|
||||||
"user_can_change":true,
|
"user_can_change": true,
|
||||||
"document_count": Random().nextInt(200)
|
"document_count": Random().nextInt(200)
|
||||||
};
|
};
|
||||||
(createdTags['results'] as List<dynamic>).add(data);
|
(createdTags['results'] as List<dynamic>).add(data);
|
||||||
return Response(
|
return Response(201,
|
||||||
201,
|
body: jsonEncode(data),
|
||||||
body: jsonEncode(data),
|
headers: {'Content-Type': 'application/json'},
|
||||||
headers: {'Content-Type': 'application/json'},
|
encoding: null,
|
||||||
encoding: null,
|
context: null);
|
||||||
context: null
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.put('/api/tags/<tagId>/', (Request req, String tagId) async {
|
app.put('/api/tags/<tagId>/', (Request req, String tagId) async {
|
||||||
@@ -140,36 +129,34 @@ class LocalMockApiServer {
|
|||||||
"slug": body?['name'],
|
"slug": body?['name'],
|
||||||
"name": body?['name'],
|
"name": body?['name'],
|
||||||
"color": body?['color'],
|
"color": body?['color'],
|
||||||
"text_color":"#000000",
|
"text_color": "#000000",
|
||||||
"match":"",
|
"match": "",
|
||||||
"matching_algorithm": body?['matching_algorithm'],
|
"matching_algorithm": body?['matching_algorithm'],
|
||||||
"is_insensitive": body?['is_insensitive'],
|
"is_insensitive": body?['is_insensitive'],
|
||||||
"is_inbox_tag":false,
|
"is_inbox_tag": false,
|
||||||
"owner":1,
|
"owner": 1,
|
||||||
"user_can_change":true,
|
"user_can_change": true,
|
||||||
"document_count": Random().nextInt(200)
|
"document_count": Random().nextInt(200)
|
||||||
};
|
};
|
||||||
var index = (createdTags['results'] as List<dynamic>).indexWhere((element) => element['id'] == body?['id']);
|
var index = (createdTags['results'] as List<dynamic>)
|
||||||
|
.indexWhere((element) => element['id'] == body?['id']);
|
||||||
(createdTags['results'] as List<dynamic>)[index] = data;
|
(createdTags['results'] as List<dynamic>)[index] = data;
|
||||||
return Response(
|
return Response(200,
|
||||||
200,
|
|
||||||
body: jsonEncode(data),
|
body: jsonEncode(data),
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
encoding: null,
|
encoding: null,
|
||||||
context: null
|
context: null);
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.delete('/api/tags/<tagId>/', (Request req, String tagId) async {
|
app.delete('/api/tags/<tagId>/', (Request req, String tagId) async {
|
||||||
log.info('Responding to PUT /api/tags/<tagId>/');
|
log.info('Responding to PUT /api/tags/<tagId>/');
|
||||||
(createdTags['results'] as List<dynamic>).removeWhere((element) => element['id'] == tagId);
|
(createdTags['results'] as List<dynamic>)
|
||||||
return Response(
|
.removeWhere((element) => element['id'] == tagId);
|
||||||
204,
|
return Response(204,
|
||||||
body: null,
|
body: null,
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
encoding: null,
|
encoding: null,
|
||||||
context: null
|
context: null);
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/api/storage_paths/', (Request req) async {
|
app.get('/api/storage_paths/', (Request req) async {
|
||||||
@@ -198,7 +185,8 @@ class LocalMockApiServer {
|
|||||||
|
|
||||||
app.get('/api/documents/<docId>/thumb/', (Request req, String docId) async {
|
app.get('/api/documents/<docId>/thumb/', (Request req, String docId) async {
|
||||||
log.info('Responding to /api/documents/<docId>/thumb/');
|
log.info('Responding to /api/documents/<docId>/thumb/');
|
||||||
var thumb = await rootBundle.load('packages/mock_server/fixtures/lorem-ipsum.png');
|
var thumb = await rootBundle
|
||||||
|
.load('packages/mock_server/fixtures/lorem-ipsum.png');
|
||||||
try {
|
try {
|
||||||
var resp = Response.ok(
|
var resp = Response.ok(
|
||||||
http.ByteStream.fromBytes(thumb.buffer.asInt8List()),
|
http.ByteStream.fromBytes(thumb.buffer.asInt8List()),
|
||||||
@@ -210,14 +198,16 @@ class LocalMockApiServer {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/api/documents/<docId>/metadata/', (Request req, String docId) async {
|
app.get('/api/documents/<docId>/metadata/',
|
||||||
|
(Request req, String docId) async {
|
||||||
log.info('Responding to /api/documents/<docId>/metadata/');
|
log.info('Responding to /api/documents/<docId>/metadata/');
|
||||||
var data = await loadFixture('metadata');
|
var data = await loadFixture('metadata');
|
||||||
return JsonMockResponse.ok(data);
|
return JsonMockResponse.ok(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
//This is not yet used in the app
|
//This is not yet used in the app
|
||||||
app.get('/api/documents/<docId>/suggestions/', (Request req, String docId) async {
|
app.get('/api/documents/<docId>/suggestions/',
|
||||||
|
(Request req, String docId) async {
|
||||||
log.info('Responding to /api/documents/<docId>/suggestions/');
|
log.info('Responding to /api/documents/<docId>/suggestions/');
|
||||||
var data = await loadFixture('suggestions');
|
var data = await loadFixture('suggestions');
|
||||||
return JsonMockResponse.ok(data);
|
return JsonMockResponse.ok(data);
|
||||||
@@ -247,80 +237,48 @@ class LocalMockApiServer {
|
|||||||
var data = await loadFixture('statistics');
|
var data = await loadFixture('statistics');
|
||||||
return JsonMockResponse.ok(data);
|
return JsonMockResponse.ok(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Future<void> start() async {
|
Future<void> start() async {
|
||||||
|
|
||||||
log.info('starting...');
|
log.info('starting...');
|
||||||
|
|
||||||
|
|
||||||
var handler = const Pipeline().addMiddleware(
|
var handler = const Pipeline().addMiddleware(
|
||||||
|
|
||||||
logRequests(logger: (message, isError) {
|
logRequests(logger: (message, isError) {
|
||||||
|
|
||||||
if (isError)
|
if (isError)
|
||||||
|
|
||||||
log.severe(message);
|
log.severe(message);
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
||||||
log.info(message);
|
log.info(message);
|
||||||
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
).addHandler(app);
|
).addHandler(app);
|
||||||
|
|
||||||
|
|
||||||
var server = await shelf_io.serve(handler, host, port);
|
var server = await shelf_io.serve(handler, host, port);
|
||||||
|
|
||||||
server.autoCompress = true;
|
server.autoCompress = true;
|
||||||
|
|
||||||
|
|
||||||
log.info('serving on: $baseUrl');
|
log.info('serving on: $baseUrl');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extension on Request {
|
extension on Request {
|
||||||
|
|
||||||
Future<String?> bodyJsonValue(String param) async {
|
Future<String?> bodyJsonValue(String param) async {
|
||||||
|
|
||||||
return jsonDecode(await this.readAsString())?[param];
|
return jsonDecode(await this.readAsString())?[param];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map?> bodyJsonMap() async {
|
Future<Map?> bodyJsonMap() async {
|
||||||
|
|
||||||
return jsonDecode(await this.readAsString());
|
return jsonDecode(await this.readAsString());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
String? get accessToken =>
|
String? get accessToken =>
|
||||||
|
|
||||||
this.headers['Authorization']?.split('Bearer ').last;
|
this.headers['Authorization']?.split('Bearer ').last;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extension JsonMockResponse on Response {
|
extension JsonMockResponse on Response {
|
||||||
|
|
||||||
static ok<T>(T json, {int delay = 800}) async {
|
static ok<T>(T json, {int delay = 800}) async {
|
||||||
|
|
||||||
await Future.delayed(Duration(milliseconds: delay)); // Emulate lag
|
await Future.delayed(Duration(milliseconds: delay)); // Emulate lag
|
||||||
|
|
||||||
return Response.ok(
|
return Response.ok(
|
||||||
|
|
||||||
jsonEncode(json),
|
jsonEncode(json),
|
||||||
|
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user