Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support following redirects #318

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions links/gql_http_link/lib/src/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class HttpLink extends Link {
/// Default HTTP headers
final Map<String, String> defaultHeaders;

/// set to `false` to throw exceptions on redirects in request chain, defaults to true
final bool followRedirects;

/// set to `true` to use the HTTP `GET` method for queries (but not for mutations)
final bool useGETForQueries;

Expand Down Expand Up @@ -104,6 +107,7 @@ class HttpLink extends Link {
this.serializer = const RequestSerializer(),
this.parser = const ResponseParser(),
this.httpResponseDecoder = _defaultHttpResponseDecoder,
this.followRedirects = true,
}) : uri = Uri.parse(uri) {
_httpClient = httpClient ?? http.Client();
}
Expand All @@ -116,8 +120,9 @@ class HttpLink extends Link {
final httpResponse = await _executeRequest(request);

final response = await _parseHttpResponse(httpResponse);
final maxStatusCode = followRedirects ? 400 : 300;

if (httpResponse.statusCode >= 300 ||
if (httpResponse.statusCode >= maxStatusCode ||
(response.data == null && response.errors == null)) {
throw HttpLinkServerException(
response: httpResponse,
Expand Down Expand Up @@ -156,6 +161,17 @@ class HttpLink extends Link {
try {
final responseBody = await httpResponseDecoder(httpResponse);
return parser.parseResponse(responseBody!);
} on FormatException {
if (!followRedirects &&
(httpResponse.statusCode == 301 || httpResponse.statusCode == 302)) {
rethrow;
}

// Empty data object sent to bypass HttpLinkServerException in subsequent request() invocation
return Response(
data: const <String, dynamic>{},
response: const <String, dynamic>{},
);
} catch (e, stackTrace) {
throw HttpLinkParserException(
originalException: e,
Expand Down Expand Up @@ -207,7 +223,9 @@ class HttpLink extends Link {
_encodeAsUriParams,
)(body),
),
)..headers.addAll(headers);
)
..headers.addAll(headers)
..followRedirects = followRedirects;
}

final httpBody = _encodeAttempter(
Expand All @@ -223,11 +241,13 @@ class HttpLink extends Link {
return http.MultipartRequest("POST", uri)
..body = httpBody
..addAllFiles(fileMap)
..headers.addAll(headers);
..headers.addAll(headers)
..followRedirects = followRedirects;
}
return http.Request("POST", uri)
..body = httpBody
..headers.addAll(headers);
..headers.addAll(headers)
..followRedirects = followRedirects;
}

/// wrap an encoding transform in exception handling
Expand Down