Skip to content

Commit a0193ab

Browse files
committed
feat: add automatic retry on 401 responses
1 parent bb0cd7d commit a0193ab

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 1.2.1
22
- Added field and request method option for the multipart file upload
3+
- Added automatic retry on 401 Unauthorized errors after refreshing credentials
34

45
## 1.2.0
56
- Added support for multipart file uploads.

lib/src/api_service.dart

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,47 +56,61 @@ class HttpApiService<DefaultRepresentation extends Object> {
5656
Encoding? encoding,
5757
bool isAuthenticated = false,
5858
}) async {
59-
var headersWithAuth = headers ?? {};
60-
if (isAuthenticated) {
61-
var credentials = await authenticationService.getCredentials();
62-
headersWithAuth = {
63-
...headersWithAuth,
64-
...credentials.headers,
59+
Future<http.Response> makeRequest() async {
60+
var headersWithAuth = headers ?? {};
61+
if (isAuthenticated) {
62+
var credentials = await authenticationService.getCredentials();
63+
headersWithAuth = {
64+
...headersWithAuth,
65+
...credentials.headers,
66+
};
67+
}
68+
69+
return switch (method) {
70+
RequestMethod.delete => _client.delete(
71+
endpoint,
72+
headers: headersWithAuth,
73+
body: body,
74+
encoding: encoding,
75+
),
76+
RequestMethod.get => _client.get(
77+
endpoint,
78+
headers: headersWithAuth,
79+
),
80+
RequestMethod.patch => _client.patch(
81+
endpoint,
82+
headers: headersWithAuth,
83+
body: body,
84+
encoding: encoding,
85+
),
86+
RequestMethod.post => _client.post(
87+
endpoint,
88+
headers: headersWithAuth,
89+
body: body,
90+
encoding: encoding,
91+
),
92+
RequestMethod.put => _client.put(
93+
endpoint,
94+
headers: headersWithAuth,
95+
body: body,
96+
encoding: encoding,
97+
),
6598
};
6699
}
67100

68-
var result = switch (method) {
69-
RequestMethod.delete => _client.delete(
70-
endpoint,
71-
headers: headersWithAuth,
72-
body: body,
73-
encoding: encoding,
74-
),
75-
RequestMethod.get => _client.get(
76-
endpoint,
77-
headers: headersWithAuth,
78-
),
79-
RequestMethod.patch => _client.patch(
80-
endpoint,
81-
headers: headersWithAuth,
82-
body: body,
83-
encoding: encoding,
84-
),
85-
RequestMethod.post => _client.post(
86-
endpoint,
87-
headers: headersWithAuth,
88-
body: body,
89-
encoding: encoding,
90-
),
91-
RequestMethod.put => _client.put(
92-
endpoint,
93-
headers: headersWithAuth,
94-
body: body,
95-
encoding: encoding,
96-
),
97-
};
101+
var response = await makeRequest();
102+
103+
if (response.statusCode == 401 && isAuthenticated) {
104+
try {
105+
await authenticationService.refreshCredentials();
106+
107+
response = await makeRequest();
108+
} on Exception {
109+
return response;
110+
}
111+
}
98112

99-
return result;
113+
return response;
100114
}
101115

102116
Future<http.Response> _upload({

0 commit comments

Comments
 (0)