import 'dart:io'; import 'package:http/http.dart' as http; import 'package:path_provider/path_provider.dart'; class AuthedClientInner extends http.BaseClient { final String _secret; final http.Client _inner; AuthedClientInner(this._secret, this._inner); @override Future send(http.BaseRequest request) { request.headers['X-ZT1-Auth'] = _secret; return _inner.send(request); } // @override // Future post(Uri url, // {Map? headers, Object? body, Encoding? encoding}) { // Map hd = headers ?? {}; // hd['content-type'] = 'application/json'; // return super.post(url, headers: hd, body: body, encoding: encoding); // } } class AuthedClient { late Future client; Future loadSecret() async { final path = (await getApplicationDocumentsDirectory()).path; final file = File('$path/run/authtoken'); final token = await file.readAsString(); return AuthedClientInner(token.trim(), http.Client()); } AuthedClient() { client = loadSecret(); } }