Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Add HttpInterceptor to HttpHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
blazern committed Oct 3, 2021
1 parent 67d1fc1 commit 82cae89
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/utils/HttpHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ import '../model/User.dart';
import 'OpenFoodAPIConfiguration.dart';
import 'QueryType.dart';

abstract class HttpInterceptor {
Future<http.Response>? interceptGet(
Uri uri, User? user, UserAgent? userAgent, QueryType? queryType);
Future<http.Response>? interceptPost(
Uri uri, Map<String, String?> body, User user, QueryType? queryType);
Future<Status>? interceptMultipart(Uri uri, Map<String, String> body,
Map<String, Uri>? files, User? user, QueryType? queryType);
}

/// General functions for sending http requests (post, get, multipart, ...)
class HttpHelper {
static HttpInterceptor? interceptor;
static final HttpHelper _singleton = HttpHelper._internal();
factory HttpHelper() => _singleton;
HttpHelper._internal();
Expand All @@ -31,6 +41,11 @@ class HttpHelper {
UserAgent? userAgent,
QueryType? queryType,
}) async {
final interception =
interceptor?.interceptGet(uri, user, userAgent, queryType);
if (interception != null) {
return await interception;
}
http.Response response = await http.get(
uri,
headers: _buildHeaders(
Expand All @@ -53,6 +68,11 @@ class HttpHelper {
Future<http.Response> doPostRequest(
Uri uri, Map<String, String?> body, User user,
{QueryType? queryType}) async {
final interception = interceptor?.interceptPost(uri, body, user, queryType);
if (interception != null) {
return await interception;
}

http.Response response = await http.post(
uri,
headers: _buildHeaders(
Expand All @@ -78,6 +98,12 @@ class HttpHelper {
User? user,
QueryType? queryType,
}) async {
final interception =
interceptor?.interceptMultipart(uri, body, files, user, queryType);
if (interception != null) {
return await interception;
}

var request = http.MultipartRequest('POST', uri);

request.headers.addAll(
Expand Down

0 comments on commit 82cae89

Please sign in to comment.