Send app information in the user agent when making requests

This commit is contained in:
Roland Geider
2021-05-17 20:06:46 +02:00
parent 09ed4dc402
commit b59e30b220
6 changed files with 30 additions and 27 deletions

View File

@@ -233,4 +233,16 @@ class AuthProvider with ChangeNotifier {
// _authTimer = Timer(Duration(seconds: timeToExpiry), logout);
// }
/// Returns the application name and version
///
/// This is used in the headers when talking to the API
String getAppNameHeader() {
String out = '';
if (applicationVersion != null) {
out = '${applicationVersion!.version} '
'(${applicationVersion!.packageName}; '
'build: ${applicationVersion!.buildNumber})';
}
return 'wger App/$out';
}
}

View File

@@ -49,7 +49,7 @@ class WgerBaseProvider {
uri,
headers: {
HttpHeaders.authorizationHeader: 'Token ${auth.token}',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
HttpHeaders.userAgentHeader: auth.getAppNameHeader(),
},
);
@@ -70,7 +70,7 @@ class WgerBaseProvider {
headers: {
HttpHeaders.authorizationHeader: 'Token ${auth.token}',
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
HttpHeaders.userAgentHeader: auth.getAppNameHeader(),
},
body: json.encode(data),
);
@@ -90,7 +90,7 @@ class WgerBaseProvider {
headers: {
HttpHeaders.authorizationHeader: 'Token ${auth.token}',
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
HttpHeaders.userAgentHeader: auth.getAppNameHeader(),
},
body: json.encode(data),
);
@@ -111,7 +111,7 @@ class WgerBaseProvider {
deleteUrl,
headers: {
HttpHeaders.authorizationHeader: 'Token ${auth.token}',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
HttpHeaders.userAgentHeader: auth.getAppNameHeader(),
},
);

View File

@@ -192,7 +192,7 @@ class ExercisesProvider extends WgerBaseProvider with ChangeNotifier {
),
headers: <String, String>{
'Authorization': 'Token ${auth.token}',
'User-Agent': 'wger Workout Manager App',
'User-Agent': auth.getAppNameHeader(),
},
);

View File

@@ -60,7 +60,7 @@ class GalleryProvider extends WgerBaseProvider with ChangeNotifier {
var request = http.MultipartRequest('POST', makeUrl(_galleryUrlPath));
request.headers.addAll({
HttpHeaders.authorizationHeader: 'Token ${auth.token}',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
HttpHeaders.userAgentHeader: auth.getAppNameHeader(),
});
request.files.add(await http.MultipartFile.fromPath('image', imageFile.path));
request.fields['date'] = toDate(image.date)!;
@@ -79,7 +79,7 @@ class GalleryProvider extends WgerBaseProvider with ChangeNotifier {
var request = http.MultipartRequest('PATCH', makeUrl(_galleryUrlPath, id: image.id));
request.headers.addAll({
HttpHeaders.authorizationHeader: 'Token ${auth.token}',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
HttpHeaders.userAgentHeader: auth.getAppNameHeader(),
});
// Only send the image if a new one was selected

View File

@@ -313,7 +313,7 @@ class NutritionPlansProvider extends WgerBaseProvider with ChangeNotifier {
),
headers: <String, String>{
HttpHeaders.authorizationHeader: 'Token ${auth.token}',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
HttpHeaders.userAgentHeader: auth.getAppNameHeader(),
},
);

View File

@@ -16,8 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
@@ -35,10 +33,7 @@ void main() {
// Mock the server response
when(client.get(
Uri.https('localhost', 'api/v2/weightentry/', {'ordering': '-date'}),
headers: <String, String>{
HttpHeaders.authorizationHeader: 'Token ${testAuthProvider.token}',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
},
headers: anyNamed('headers'),
)).thenAnswer((_) async => http.Response(
'{"results": [{"id": 1, "date": "2021-01-01", "weight": "80.00"}, '
'{"id": 2, "date": "2021-01-10", "weight": "99"},'
@@ -58,15 +53,14 @@ void main() {
final client = MockClient();
// Mock the server response
when(client.post(Uri.https('localhost', 'api/v2/weightentry/'),
headers: {
HttpHeaders.authorizationHeader: 'Token ${testAuthProvider.token}',
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
},
body: '{"id":null,"weight":"80","date":"2021-01-01"}'))
.thenAnswer(
(_) async => http.Response('{"id": 25, "date": "2021-01-01", "weight": "80"}', 200));
when(
client.post(
Uri.https('localhost', 'api/v2/weightentry/'),
headers: anyNamed('headers'),
body: '{"id":null,"weight":"80","date":"2021-01-01"}',
),
).thenAnswer(
(_) async => http.Response('{"id": 25, "date": "2021-01-01", "weight": "80"}', 200));
// POST the data to the server
final WeightEntry weightEntry = WeightEntry(date: DateTime(2021, 1, 1), weight: 80);
@@ -85,10 +79,7 @@ void main() {
// Mock the server response
when(client.delete(
Uri.https('localhost', 'api/v2/weightentry/4/'),
headers: {
HttpHeaders.authorizationHeader: 'Token ${testAuthProvider.token}',
HttpHeaders.userAgentHeader: 'wger Workout Manager App',
},
headers: anyNamed('headers'),
)).thenAnswer((_) async => http.Response('', 200));
// DELETE the data from the server