Skip to content

Commit 71bb94e

Browse files
authored
Add user agent header (#251)
* Add Header to differentiate PiwigoNG from other API users Add an header to all types of query using package_info_plus. This add an header right before the query but the netcode being a mess it may not be applied everywhere. Ideally this should be a dio baseoption but async code can't run in init of a class and package info plus doesn't support non async calls A third party package with async calls seems like the worst way of implementing this but this seems like the only option * Upgrade to flutter 3.38.4, bump dependencies (no major version bump)
1 parent 940f22b commit 71bb94e

3 files changed

Lines changed: 38 additions & 21 deletions

File tree

.fvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"flutter": "3.38.2"
2+
"flutter": "3.38.4"
33
}

lib/network/api_client.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:dio/dio.dart';
66
import 'package:dio/io.dart';
77
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
88
import 'package:flutter/foundation.dart';
9+
import 'package:package_info_plus/package_info_plus.dart';
910
import 'package:piwigo_ng/services/preferences_service.dart';
1011

1112
import 'api_interceptor.dart';
@@ -18,7 +19,8 @@ class ApiClient {
1819
..httpClientAdapter = sslHttpClientAdapter;
1920

2021
static HttpClientAdapter get sslHttpClientAdapter => IOHttpClientAdapter(
21-
createHttpClient: () => HttpClient()..badCertificateCallback = piwigoSSLBypass,
22+
createHttpClient: () =>
23+
HttpClient()..badCertificateCallback = piwigoSSLBypass,
2224
);
2325

2426
static bool piwigoSSLBypass(X509Certificate cert, String host, int port) {
@@ -35,6 +37,9 @@ class ApiClient {
3537
CancelToken? cancelToken,
3638
void Function(int, int)? onReceiveProgress,
3739
}) async {
40+
PackageInfo packageInfo = await PackageInfo.fromPlatform();
41+
dio.options.headers
42+
.addAll({'User-Agent': 'PiwigoNG/${packageInfo.version}'});
3843
Response response = await dio.get(
3944
path,
4045
queryParameters: queryParameters,
@@ -54,6 +59,9 @@ class ApiClient {
5459
void Function(int, int)? onSendProgress,
5560
void Function(int, int)? onReceiveProgress,
5661
}) async {
62+
PackageInfo packageInfo = await PackageInfo.fromPlatform();
63+
dio.options.headers
64+
.addAll({'User-Agent': 'PiwigoNG/${packageInfo.version}'});
5765
Response response = await dio.post(
5866
path,
5967
data: data,
@@ -75,6 +83,9 @@ class ApiClient {
7583
void Function(int, int)? onSendProgress,
7684
void Function(int, int)? onReceiveProgress,
7785
}) async {
86+
PackageInfo packageInfo = await PackageInfo.fromPlatform();
87+
dio.options.headers
88+
.addAll({'User-Agent': 'PiwigoNG/${packageInfo.version}'});
7889
Response response = await dio.put(
7990
path,
8091
data: data,
@@ -96,6 +107,9 @@ class ApiClient {
96107
void Function(int, int)? onSendProgress,
97108
void Function(int, int)? onReceiveProgress,
98109
}) async {
110+
PackageInfo packageInfo = await PackageInfo.fromPlatform();
111+
dio.options.headers
112+
.addAll({'User-Agent': 'PiwigoNG/${packageInfo.version}'});
99113
Response response = await dio.delete(
100114
path,
101115
data: data,
@@ -116,6 +130,8 @@ class ApiClient {
116130
CancelToken? cancelToken,
117131
void Function(int, int)? onReceiveProgress,
118132
}) async {
133+
PackageInfo packageInfo = await PackageInfo.fromPlatform();
134+
dio.options.headers = {'User-Agent': 'PiwigoNG/${packageInfo.version}'};
119135
Response response = await dio.download(
120136
path,
121137
outputPath,
@@ -134,7 +150,8 @@ class ApiClient {
134150
class SSLHttpOverrides extends HttpOverrides {
135151
@override
136152
HttpClient createHttpClient(SecurityContext? context) {
137-
return super.createHttpClient(context)..badCertificateCallback = ApiClient.piwigoSSLBypass;
153+
return super.createHttpClient(context)
154+
..badCertificateCallback = ApiClient.piwigoSSLBypass;
138155
}
139156
}
140157

pubspec.lock

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ packages:
205205
dependency: "direct main"
206206
description:
207207
name: device_info_plus
208-
sha256: dd0e8e02186b2196c7848c9d394a5fd6e5b57a43a546082c5820b1ec72317e33
208+
sha256: "4df8babf73058181227e18b08e6ea3520cf5fc5d796888d33b7cb0f33f984b7c"
209209
url: "https://pub.dev"
210210
source: hosted
211-
version: "12.2.0"
211+
version: "12.3.0"
212212
device_info_plus_platform_interface:
213213
dependency: transitive
214214
description:
@@ -625,10 +625,10 @@ packages:
625625
dependency: transitive
626626
description:
627627
name: image_picker_ios
628-
sha256: "997d100ce1dda5b1ba4085194c5e36c9f8a1fb7987f6a36ab677a344cd2dc986"
628+
sha256: "956c16a42c0c708f914021666ffcd8265dde36e673c9fa68c81f7d085d9774ad"
629629
url: "https://pub.dev"
630630
source: hosted
631-
version: "0.8.13+2"
631+
version: "0.8.13+3"
632632
image_picker_linux:
633633
dependency: transitive
634634
description:
@@ -881,10 +881,10 @@ packages:
881881
dependency: transitive
882882
description:
883883
name: path_provider_foundation
884-
sha256: "6192e477f34018ef1ea790c56fffc7302e3bc3efede9e798b934c252c8c105ba"
884+
sha256: "6d13aece7b3f5c5a9731eaf553ff9dcbc2eff41087fd2df587fd0fed9a3eb0c4"
885885
url: "https://pub.dev"
886886
source: hosted
887-
version: "2.5.0"
887+
version: "2.5.1"
888888
path_provider_linux:
889889
dependency: transitive
890890
description:
@@ -1058,18 +1058,18 @@ packages:
10581058
dependency: "direct main"
10591059
description:
10601060
name: shared_preferences
1061-
sha256: "6e8bf70b7fef813df4e9a36f658ac46d107db4b4cfe1048b477d4e453a8159f5"
1061+
sha256: "2939ae520c9024cb197fc20dee269cd8cdbf564c8b5746374ec6cacdc5169e64"
10621062
url: "https://pub.dev"
10631063
source: hosted
1064-
version: "2.5.3"
1064+
version: "2.5.4"
10651065
shared_preferences_android:
10661066
dependency: transitive
10671067
description:
10681068
name: shared_preferences_android
1069-
sha256: "46a46fd64659eff15f4638bbe19de43f9483f0e0bf024a9fb6b3582064bacc7b"
1069+
sha256: "83af5c682796c0f7719c2bbf74792d113e40ae97981b8f266fa84574573556bc"
10701070
url: "https://pub.dev"
10711071
source: hosted
1072-
version: "2.4.17"
1072+
version: "2.4.18"
10731073
shared_preferences_foundation:
10741074
dependency: transitive
10751075
description:
@@ -1327,18 +1327,18 @@ packages:
13271327
dependency: transitive
13281328
description:
13291329
name: video_player_android
1330-
sha256: "3f7ef3fb7b29f510e58f4d56b6ffbc3463b1071f2cf56e10f8d25f5b991ed85b"
1330+
sha256: d74b66f283afff135d5be0ceccca2ca74dff7df1e9b1eaca6bd4699875d3ae60
13311331
url: "https://pub.dev"
13321332
source: hosted
1333-
version: "2.8.21"
1333+
version: "2.8.22"
13341334
video_player_avfoundation:
13351335
dependency: transitive
13361336
description:
13371337
name: video_player_avfoundation
1338-
sha256: "6bced1739cf1f96f03058118adb8ac0dd6f96aa1a1a6e526424ab92fd2a6a77d"
1338+
sha256: e4d33b79a064498c6eb3a6a492b6a5012573d4943c28d566caf1a6c0840fe78d
13391339
url: "https://pub.dev"
13401340
source: hosted
1341-
version: "2.8.7"
1341+
version: "2.8.8"
13421342
video_player_platform_interface:
13431343
dependency: transitive
13441344
description:
@@ -1407,10 +1407,10 @@ packages:
14071407
dependency: transitive
14081408
description:
14091409
name: webview_flutter_android
1410-
sha256: "3fcca88ee2ae568807ebd42deed235bb8dd8e62b3e4d5caff67daa6bce062cca"
1410+
sha256: eeeb3fcd5f0ff9f8446c9f4bbc18a99b809e40297528a3395597d03aafb9f510
14111411
url: "https://pub.dev"
14121412
source: hosted
1413-
version: "4.10.9"
1413+
version: "4.10.11"
14141414
webview_flutter_platform_interface:
14151415
dependency: transitive
14161416
description:
@@ -1423,10 +1423,10 @@ packages:
14231423
dependency: transitive
14241424
description:
14251425
name: webview_flutter_wkwebview
1426-
sha256: a57b76a081bed3bf3a71a486bdf83642b00f1a7342043d50367cea68f338b1af
1426+
sha256: e49f378ed066efb13fc36186bbe0bd2425630d4ea0dbc71a18fdd0e4d8ed8ebc
14271427
url: "https://pub.dev"
14281428
source: hosted
1429-
version: "3.23.4"
1429+
version: "3.23.5"
14301430
win32:
14311431
dependency: transitive
14321432
description:

0 commit comments

Comments
 (0)