Files
deflock-app/lib/services/http_client.dart
Doug Borg 0137fd66aa 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>
2026-02-24 19:31:27 -07:00

35 lines
1.1 KiB
Dart

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();
}