mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-12 18:12:23 -06:00
fix: Add http package
This commit is contained in:
@@ -13,21 +13,19 @@ import 'package:shelf/shelf_io.dart' as shelf_io;
|
|||||||
import 'package:shelf_router/shelf_router.dart' as shelf_router;
|
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';
|
|
||||||
|
|
||||||
Logger log = Logger('LocalMockApiServer');
|
Logger log = Logger('LocalMockApiServer');
|
||||||
|
|
||||||
class LocalMockApiServer {
|
class LocalMockApiServer {
|
||||||
static final host = 'localhost';
|
static const host = 'localhost';
|
||||||
|
|
||||||
static final port = 3131;
|
static const 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 =
|
var fixture = await rootBundle.loadString('packages/mock_server/fixtures/$name.json');
|
||||||
await rootBundle.loadString('packages/mock_server/fixtures/$name.json');
|
|
||||||
return json.decode(fixture);
|
return json.decode(fixture);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,13 +148,9 @@ class LocalMockApiServer {
|
|||||||
|
|
||||||
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>)
|
(createdTags['results'] as List<dynamic>).removeWhere((element) => element['id'] == tagId);
|
||||||
.removeWhere((element) => element['id'] == tagId);
|
|
||||||
return Response(204,
|
return Response(204,
|
||||||
body: null,
|
body: null, headers: {'Content-Type': 'application/json'}, encoding: null, context: null);
|
||||||
headers: {'Content-Type': 'application/json'},
|
|
||||||
encoding: null,
|
|
||||||
context: null);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/api/storage_paths/', (Request req) async {
|
app.get('/api/storage_paths/', (Request req) async {
|
||||||
@@ -185,8 +179,7 @@ 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
|
var thumb = await rootBundle.load('packages/mock_server/fixtures/lorem-ipsum.png');
|
||||||
.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()),
|
||||||
@@ -198,16 +191,14 @@ class LocalMockApiServer {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/api/documents/<docId>/metadata/',
|
app.get('/api/documents/<docId>/metadata/', (Request req, String docId) async {
|
||||||
(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/',
|
app.get('/api/documents/<docId>/suggestions/', (Request req, String docId) async {
|
||||||
(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);
|
||||||
@@ -244,10 +235,11 @@ class LocalMockApiServer {
|
|||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@@ -261,15 +253,14 @@ class LocalMockApiServer {
|
|||||||
|
|
||||||
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 readAsString())?[param];
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map?> bodyJsonMap() async {
|
Future<Map?> bodyJsonMap() async {
|
||||||
return jsonDecode(await this.readAsString());
|
return jsonDecode(await readAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
String? get accessToken =>
|
String? get accessToken => headers['Authorization']?.split('Bearer ').last;
|
||||||
this.headers['Authorization']?.split('Bearer ').last;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension JsonMockResponse on Response {
|
extension JsonMockResponse on Response {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ dependencies:
|
|||||||
logging: ^1.1.1
|
logging: ^1.1.1
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
http: ^1.0.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user