Skip to content

Commit 118d834

Browse files
authored
Fix: Delete not sending body & Non-string params not being added. (#90)
* fix: delete method did not send body on request * build: version bump * fix: non-string params not added, retry policy example * docs: updated changelog * fix: added missing parameters to other types of requests * fix: adds encoding to delete requests from client
1 parent 6117ac3 commit 118d834

File tree

17 files changed

+560
-64
lines changed

17 files changed

+560
-64
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 1.0.2
4+
5+
- 📖  Changed: example project to showcase `RetryPolicy` usage.
6+
- 🐞  Fixed: `parameters` were missing in requests of type `POST`, `PUT`, `PATCH`, and `DELETE`.
7+
- 🐞  Fixed: `int` or other non-string parameters are not being added to request. Thanks to @
8+
Contributor
9+
meysammahfouzi
10+
- 🐞  Fixed: `body` is not sent in delete requests despite being accepted as parameter. Thanks to @MaciejZuk
11+
312
## 1.0.1
413

514
-  Changed: `ResponseData` now has `request` to allow checking on the request that triggered the response. Thanks to @II11II

example/ios/Flutter/Debug.xcconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
12
#include "Generated.xcconfig"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
12
#include "Generated.xcconfig"

example/ios/Podfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Uncomment this line to define a global platform for your project
2+
# platform :ios, '9.0'
3+
4+
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
5+
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
6+
7+
project 'Runner', {
8+
'Debug' => :debug,
9+
'Profile' => :release,
10+
'Release' => :release,
11+
}
12+
13+
def flutter_root
14+
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
15+
unless File.exist?(generated_xcode_build_settings_path)
16+
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
17+
end
18+
19+
File.foreach(generated_xcode_build_settings_path) do |line|
20+
matches = line.match(/FLUTTER_ROOT\=(.*)/)
21+
return matches[1].strip if matches
22+
end
23+
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
24+
end
25+
26+
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
27+
28+
flutter_ios_podfile_setup
29+
30+
target 'Runner' do
31+
use_frameworks!
32+
use_modular_headers!
33+
34+
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
35+
end
36+
37+
post_install do |installer|
38+
installer.pods_project.targets.each do |target|
39+
flutter_additional_ios_build_settings(target)
40+
end
41+
end

example/ios/Podfile.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
PODS:
2+
- Flutter (1.0.0)
3+
- shared_preferences (0.0.1):
4+
- Flutter
5+
6+
DEPENDENCIES:
7+
- Flutter (from `Flutter`)
8+
- shared_preferences (from `.symlinks/plugins/shared_preferences/ios`)
9+
10+
EXTERNAL SOURCES:
11+
Flutter:
12+
:path: Flutter
13+
shared_preferences:
14+
:path: ".symlinks/plugins/shared_preferences/ios"
15+
16+
SPEC CHECKSUMS:
17+
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
18+
shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d
19+
20+
PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
21+
22+
COCOAPODS: 1.10.1

example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
1111
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
12+
65B907C60867061250A77D6E /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A76843E3596405C7B6DFF1B /* Pods_Runner.framework */; };
1213
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
1314
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
1415
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
@@ -29,9 +30,12 @@
2930
/* End PBXCopyFilesBuildPhase section */
3031

3132
/* Begin PBXFileReference section */
33+
0F9682D7879846A313BD8569 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
3234
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
3335
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
3436
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
37+
65B9DA6E65158B5A69155CD5 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
38+
6DE2CE631932059D90A38FAB /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
3539
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
3640
74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3741
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
@@ -42,19 +46,40 @@
4246
97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4347
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
4448
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
49+
9A76843E3596405C7B6DFF1B /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4550
/* End PBXFileReference section */
4651

4752
/* Begin PBXFrameworksBuildPhase section */
4853
97C146EB1CF9000F007C117D /* Frameworks */ = {
4954
isa = PBXFrameworksBuildPhase;
5055
buildActionMask = 2147483647;
5156
files = (
57+
65B907C60867061250A77D6E /* Pods_Runner.framework in Frameworks */,
5258
);
5359
runOnlyForDeploymentPostprocessing = 0;
5460
};
5561
/* End PBXFrameworksBuildPhase section */
5662

5763
/* Begin PBXGroup section */
64+
18592708EC18A958100A9026 /* Pods */ = {
65+
isa = PBXGroup;
66+
children = (
67+
65B9DA6E65158B5A69155CD5 /* Pods-Runner.debug.xcconfig */,
68+
0F9682D7879846A313BD8569 /* Pods-Runner.release.xcconfig */,
69+
6DE2CE631932059D90A38FAB /* Pods-Runner.profile.xcconfig */,
70+
);
71+
name = Pods;
72+
path = Pods;
73+
sourceTree = "<group>";
74+
};
75+
3994D8CD7A88340F930600D1 /* Frameworks */ = {
76+
isa = PBXGroup;
77+
children = (
78+
9A76843E3596405C7B6DFF1B /* Pods_Runner.framework */,
79+
);
80+
name = Frameworks;
81+
sourceTree = "<group>";
82+
};
5883
9740EEB11CF90186004384FC /* Flutter */ = {
5984
isa = PBXGroup;
6085
children = (
@@ -72,6 +97,8 @@
7297
9740EEB11CF90186004384FC /* Flutter */,
7398
97C146F01CF9000F007C117D /* Runner */,
7499
97C146EF1CF9000F007C117D /* Products */,
100+
18592708EC18A958100A9026 /* Pods */,
101+
3994D8CD7A88340F930600D1 /* Frameworks */,
75102
);
76103
sourceTree = "<group>";
77104
};
@@ -113,12 +140,14 @@
113140
isa = PBXNativeTarget;
114141
buildConfigurationList = 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */;
115142
buildPhases = (
143+
985586E5047784C00F0EE9EF /* [CP] Check Pods Manifest.lock */,
116144
9740EEB61CF901F6004384FC /* Run Script */,
117145
97C146EA1CF9000F007C117D /* Sources */,
118146
97C146EB1CF9000F007C117D /* Frameworks */,
119147
97C146EC1CF9000F007C117D /* Resources */,
120148
9705A1C41CF9048500538489 /* Embed Frameworks */,
121149
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
150+
D272D0CB52AEB248E1813299 /* [CP] Embed Pods Frameworks */,
122151
);
123152
buildRules = (
124153
);
@@ -205,6 +234,46 @@
205234
shellPath = /bin/sh;
206235
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build";
207236
};
237+
985586E5047784C00F0EE9EF /* [CP] Check Pods Manifest.lock */ = {
238+
isa = PBXShellScriptBuildPhase;
239+
buildActionMask = 2147483647;
240+
files = (
241+
);
242+
inputFileListPaths = (
243+
);
244+
inputPaths = (
245+
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
246+
"${PODS_ROOT}/Manifest.lock",
247+
);
248+
name = "[CP] Check Pods Manifest.lock";
249+
outputFileListPaths = (
250+
);
251+
outputPaths = (
252+
"$(DERIVED_FILE_DIR)/Pods-Runner-checkManifestLockResult.txt",
253+
);
254+
runOnlyForDeploymentPostprocessing = 0;
255+
shellPath = /bin/sh;
256+
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
257+
showEnvVarsInLog = 0;
258+
};
259+
D272D0CB52AEB248E1813299 /* [CP] Embed Pods Frameworks */ = {
260+
isa = PBXShellScriptBuildPhase;
261+
buildActionMask = 2147483647;
262+
files = (
263+
);
264+
inputPaths = (
265+
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
266+
"${BUILT_PRODUCTS_DIR}/shared_preferences/shared_preferences.framework",
267+
);
268+
name = "[CP] Embed Pods Frameworks";
269+
outputPaths = (
270+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/shared_preferences.framework",
271+
);
272+
runOnlyForDeploymentPostprocessing = 0;
273+
shellPath = /bin/sh;
274+
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
275+
showEnvVarsInLog = 0;
276+
};
208277
/* End PBXShellScriptBuildPhase section */
209278

210279
/* Begin PBXSourcesBuildPhase section */

example/ios/Runner.xcworkspace/contents.xcworkspacedata

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/credentials.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
// Replace this if you are going to run the example. You can get your own at https://openweathermap.org/
2+
const String OPEN_WEATHER_EXPIRED_API_KEY = "b04a4a7c5a71de47d7cdb0s5687c5d14";
23
const String OPEN_WEATHER_API_KEY = "b04a4a7c5a71de47d7cdb0c7607c5d14";

example/lib/main.dart

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:io';
33

44
import 'package:flutter/material.dart';
55
import 'package:http_interceptor/http_interceptor.dart';
6+
import 'package:shared_preferences/shared_preferences.dart';
67
import 'credentials.dart'; // If you are going to run this example you need to replace the key.
78
import 'cities.dart'; // This is just a List of Maps that contains the suggested cities.
89

@@ -24,12 +25,34 @@ class HomeScreen extends StatefulWidget {
2425

2526
class _HomeScreenState extends State<HomeScreen> {
2627
WeatherRepository repository = WeatherRepository(
27-
InterceptedClient.build(interceptors: [
28-
WeatherApiInterceptor(),
29-
LoggerInterceptor(),
30-
]),
28+
InterceptedClient.build(
29+
interceptors: [
30+
WeatherApiInterceptor(),
31+
LoggerInterceptor(),
32+
],
33+
retryPolicy: ExpiredTokenRetryPolicy(),
34+
),
3135
);
3236

37+
@override
38+
void initState() {
39+
super.initState();
40+
41+
clearStorageForDemoPurposes();
42+
}
43+
44+
Future<void> clearStorageForDemoPurposes() async {
45+
final cache = await SharedPreferences.getInstance();
46+
47+
cache.setString(appToken, OPEN_WEATHER_EXPIRED_API_KEY);
48+
}
49+
50+
@override
51+
void dispose() {
52+
repository.client.close();
53+
super.dispose();
54+
}
55+
3356
@override
3457
Widget build(BuildContext context) {
3558
return Scaffold(
@@ -298,11 +321,15 @@ class LoggerInterceptor implements InterceptorContract {
298321
}
299322
}
300323

324+
const String appToken = "TOKEN";
325+
301326
class WeatherApiInterceptor implements InterceptorContract {
302327
@override
303328
Future<RequestData> interceptRequest({required RequestData data}) async {
304329
try {
305-
data.params['appid'] = OPEN_WEATHER_API_KEY;
330+
final cache = await SharedPreferences.getInstance();
331+
332+
data.params['appid'] = cache.getString(appToken);
306333
data.params['units'] = 'metric';
307334
data.headers[HttpHeaders.contentTypeHeader] = "application/json";
308335
} catch (e) {
@@ -316,3 +343,29 @@ class WeatherApiInterceptor implements InterceptorContract {
316343
Future<ResponseData> interceptResponse({required ResponseData data}) async =>
317344
data;
318345
}
346+
347+
class ExpiredTokenRetryPolicy extends RetryPolicy {
348+
@override
349+
int get maxRetryAttempts => 2;
350+
351+
@override
352+
bool shouldAttemptRetryOnException(Exception reason) {
353+
print(reason);
354+
355+
return false;
356+
}
357+
358+
@override
359+
Future<bool> shouldAttemptRetryOnResponse(ResponseData response) async {
360+
if (response.statusCode == 401) {
361+
print("Retrying request...");
362+
final cache = await SharedPreferences.getInstance();
363+
364+
cache.setString(appToken, OPEN_WEATHER_API_KEY);
365+
366+
return true;
367+
}
368+
369+
return false;
370+
}
371+
}

0 commit comments

Comments
 (0)