mirror of
https://github.com/FoggedLens/deflock-app.git
synced 2026-09-28 03:51:43 +02:00
Add consistent User-Agent header to all HTTP clients
Create UserAgentClient (http.BaseClient wrapper) that injects a User-Agent header into every request, reading app name and version from VersionService and contact/homepage from dev_config.dart. Format follows OSM tile usage policy: DeFlock/<version> (+https://deflock.org; contact: admin@stopflock.com) Replaces 4 inconsistent hardcoded UA strings and adds UA to the 9 call sites that previously sent none. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
fe20356734
commit
0137fd66aa
@@ -0,0 +1,34 @@
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../dev_config.dart';
|
||||
import 'version_service.dart';
|
||||
|
||||
/// An [http.BaseClient] that injects a User-Agent header into every request.
|
||||
///
|
||||
/// Reads the app name and version dynamically from [VersionService] so the UA
|
||||
/// string stays in sync with pubspec.yaml without hard-coding values.
|
||||
///
|
||||
/// Uses [putIfAbsent] so a manually-set User-Agent is never overwritten.
|
||||
class UserAgentClient extends http.BaseClient {
|
||||
final http.Client _inner;
|
||||
|
||||
UserAgentClient([http.Client? inner]) : _inner = inner ?? http.Client();
|
||||
|
||||
/// The User-Agent string sent with every request.
|
||||
///
|
||||
/// Format follows OSM tile usage policy recommendations:
|
||||
/// `AppName/version (+homepage; contact: email)`
|
||||
static String get userAgent {
|
||||
final vs = VersionService();
|
||||
return '${vs.appName}/${vs.version} (+$kHomepageUrl; contact: $kContactEmail)';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<http.StreamedResponse> send(http.BaseRequest request) {
|
||||
request.headers.putIfAbsent('User-Agent', () => userAgent);
|
||||
return _inner.send(request);
|
||||
}
|
||||
|
||||
@override
|
||||
void close() => _inner.close();
|
||||
}
|
||||
Reference in New Issue
Block a user