Skip to content

Commit 3aeecb6

Browse files
authored
Add rawResponse (#144)
1 parent a1b2f09 commit 3aeecb6

14 files changed

+317
-227
lines changed

lib/src/client/response/collection_fetched.dart

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
class CollectionFetched {
5-
CollectionFetched(this.httpResponse, Map json) {
6-
final document = InboundDocument(json);
6+
CollectionFetched(this.rawResponse) {
7+
final document = InboundDocument(rawResponse.document ??
8+
(throw FormatException('The document must not be empty')));
79
collection.addAll(document.dataAsCollection());
810
included.addAll(document.included());
911
meta.addAll(document.meta());
1012
links.addAll(document.links());
1113
}
1214

13-
final Response httpResponse;
15+
// coverage:ignore-start
16+
/// The raw HTTP response
17+
@Deprecated('Use rawResponse.httpResponse instead')
18+
i.Response get httpResponse => rawResponse.httpResponse;
19+
// coverage:ignore-end
20+
21+
/// The raw JSON:API response
22+
final Response rawResponse;
1423

1524
/// The resource collection fetched from the server
1625
final collection = <Resource>[];

lib/src/client/response/related_resource_fetched.dart

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A related resource response.
56
///
67
/// https://jsonapi.org/format/#fetching-resources-responses
78
class RelatedResourceFetched {
8-
RelatedResourceFetched(this.httpResponse, Map json)
9-
: resource = InboundDocument(json).dataAsResourceOrNull() {
10-
final document = InboundDocument(json);
9+
RelatedResourceFetched(this.rawResponse) {
10+
final document = InboundDocument(rawResponse.document ??
11+
(throw FormatException('The document must not be empty')));
12+
resource = document.dataAsResourceOrNull();
1113
included.addAll(document.included());
1214
meta.addAll(document.meta());
1315
links.addAll(document.links());
1416
}
1517

16-
final Response httpResponse;
18+
// coverage:ignore-start
19+
/// The raw HTTP response
20+
@Deprecated('Use rawResponse.httpResponse instead')
21+
i.Response get httpResponse => rawResponse.httpResponse;
22+
// coverage:ignore-end
23+
24+
/// The raw JSON:API response
25+
final Response rawResponse;
1726

1827
/// Related resource. May be null
19-
final Resource? resource;
28+
late final Resource? resource;
2029

2130
/// Included resources
2231
final included = <Resource>[];
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A response to a relationship fetch request.
56
class RelationshipFetched<R extends Relationship> {
6-
RelationshipFetched(this.httpResponse, this.relationship);
7+
RelationshipFetched(this.rawResponse, this.relationship);
78

8-
static RelationshipFetched<ToMany> many(Response httpResponse, Map json) =>
9-
RelationshipFetched(httpResponse, InboundDocument(json).asToMany())
10-
..included.addAll(InboundDocument(json).included());
9+
static RelationshipFetched<ToMany> many(Response response) {
10+
final document = InboundDocument(response.document ??
11+
(throw FormatException('The document must not be empty')));
12+
return RelationshipFetched(response, document.asToMany())
13+
..included.addAll(document.included());
14+
}
1115

12-
static RelationshipFetched<ToOne> one(Response httpResponse, Map json) =>
13-
RelationshipFetched(httpResponse, InboundDocument(json).asToOne())
14-
..included.addAll(InboundDocument(json).included());
16+
static RelationshipFetched<ToOne> one(Response response) {
17+
final document = InboundDocument(response.document ??
18+
(throw FormatException('The document must not be empty')));
19+
return RelationshipFetched(response, document.asToOne())
20+
..included.addAll(document.included());
21+
}
22+
23+
// coverage:ignore-start
24+
/// The raw HTTP response
25+
@Deprecated('Use rawResponse.httpResponse instead')
26+
i.Response get httpResponse => rawResponse.httpResponse;
27+
// coverage:ignore-end
28+
29+
/// The raw JSON:API response
30+
final Response rawResponse;
1531

16-
final Response httpResponse;
1732
final R relationship;
1833
final included = <Resource>[];
1934
}

lib/src/client/response/relationship_updated.dart

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A response to a relationship request.
56
class RelationshipUpdated<R extends Relationship> {
6-
RelationshipUpdated(this.httpResponse, this.relationship);
7+
RelationshipUpdated(this.rawResponse, this.relationship);
78

8-
static RelationshipUpdated<ToMany> many(Response httpResponse, Map? json) =>
9-
RelationshipUpdated(
10-
httpResponse, json == null ? null : InboundDocument(json).asToMany());
9+
static RelationshipUpdated<ToMany> many(Response response) {
10+
final json = response.document;
11+
return RelationshipUpdated(
12+
response, json == null ? null : InboundDocument(json).asToMany());
13+
}
1114

12-
static RelationshipUpdated<ToOne> one(Response httpResponse, Map? json) =>
13-
RelationshipUpdated(
14-
httpResponse, json == null ? null : InboundDocument(json).asToOne());
15+
static RelationshipUpdated<ToOne> one(Response response) {
16+
final json = response.document;
17+
return RelationshipUpdated(
18+
response, json == null ? null : InboundDocument(json).asToOne());
19+
}
1520

16-
final Response httpResponse;
21+
// coverage:ignore-start
22+
/// The raw HTTP response
23+
@Deprecated('Use rawResponse.httpResponse instead')
24+
i.Response get httpResponse => rawResponse.httpResponse;
25+
// coverage:ignore-end
26+
27+
/// The raw JSON:API response
28+
final Response rawResponse;
1729

1830
/// Updated relationship. Null if "204 No Content" is returned.
1931
final R? relationship;
+17-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// Thrown when the server returns a non-successful response.
56
class RequestFailure implements Exception {
6-
RequestFailure(this.httpResponse, Map? document) {
7-
if (document != null) {
8-
errors.addAll(InboundDocument(document).errors());
9-
meta.addAll(InboundDocument(document).meta());
10-
}
7+
RequestFailure(this.rawResponse) {
8+
final json = rawResponse.document;
9+
if (json == null) return;
10+
final document = InboundDocument(json);
11+
errors.addAll(document.errors());
12+
meta.addAll(document.meta());
1113
}
1214

13-
final Response httpResponse;
15+
// coverage:ignore-start
16+
/// The raw HTTP response
17+
@Deprecated('Use rawResponse.httpResponse instead')
18+
i.Response get httpResponse => rawResponse.httpResponse;
19+
// coverage:ignore-end
20+
21+
/// The raw JSON:API response
22+
final Response rawResponse;
1423

1524
/// Error objects returned by the server
1625
final errors = <ErrorObject>[];
@@ -20,5 +29,5 @@ class RequestFailure implements Exception {
2029

2130
@override
2231
String toString() =>
23-
'JSON:API request failed with HTTP status ${httpResponse.statusCode}.';
32+
'JSON:API request failed with HTTP status ${rawResponse.httpResponse.statusCode}.';
2433
}

lib/src/client/response/resource_created.dart

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A response to a new resource creation request.
56
/// This is always a "201 Created" response.
67
///
78
/// https://jsonapi.org/format/#crud-creating-responses-201
89
class ResourceCreated {
9-
ResourceCreated(this.httpResponse, Map json)
10-
: resource = InboundDocument(json).dataAsResource() {
11-
meta.addAll(InboundDocument(json).meta());
12-
links.addAll(InboundDocument(json).links());
13-
included.addAll(InboundDocument(json).included());
10+
ResourceCreated(this.rawResponse) {
11+
final document = InboundDocument(rawResponse.document ??
12+
(throw FormatException('The document must not be empty')));
13+
resource = document.dataAsResource();
14+
included.addAll(document.included());
15+
meta.addAll(document.meta());
16+
links.addAll(document.links());
1417
}
1518

16-
final Response httpResponse;
19+
// coverage:ignore-start
20+
/// The raw HTTP response
21+
@Deprecated('Use rawResponse.httpResponse instead')
22+
i.Response get httpResponse => rawResponse.httpResponse;
23+
// coverage:ignore-end
24+
25+
/// The raw JSON:API response
26+
final Response rawResponse;
1727

1828
/// Created resource.
19-
final Resource resource;
29+
late final Resource resource;
2030

2131
/// Top-level meta data
2232
final meta = <String, Object?>{};

lib/src/client/response/resource_fetched.dart

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
/// A response to fetch a primary resource request
56
class ResourceFetched {
6-
ResourceFetched(this.httpResponse, Map json)
7-
: resource = InboundDocument(json).dataAsResource() {
8-
included.addAll(InboundDocument(json).included());
9-
meta.addAll(InboundDocument(json).meta());
10-
links.addAll(InboundDocument(json).links());
7+
ResourceFetched(this.rawResponse) {
8+
final document = InboundDocument(rawResponse.document ??
9+
(throw FormatException('The document must not be empty')));
10+
resource = document.dataAsResource();
11+
included.addAll(document.included());
12+
meta.addAll(document.meta());
13+
links.addAll(document.links());
1114
}
1215

13-
final Response httpResponse;
14-
final Resource resource;
16+
// coverage:ignore-start
17+
/// The raw HTTP response
18+
@Deprecated('Use rawResponse.httpResponse instead')
19+
i.Response get httpResponse => rawResponse.httpResponse;
20+
// coverage:ignore-end
21+
22+
/// The raw JSON:API response
23+
final Response rawResponse;
24+
25+
late final Resource resource;
1526

1627
/// Top-level meta data
1728
final meta = <String, Object?>{};

lib/src/client/response/resource_updated.dart

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
import 'package:http_interop/http_interop.dart';
1+
import 'package:http_interop/http_interop.dart' as i;
22
import 'package:json_api/document.dart';
3+
import 'package:json_api/src/client/response.dart';
34

45
class ResourceUpdated {
5-
ResourceUpdated(this.httpResponse, Map? json) : resource = _resource(json) {
6-
if (json != null) {
7-
included.addAll(InboundDocument(json).included());
8-
meta.addAll(InboundDocument(json).meta());
9-
links.addAll(InboundDocument(json).links());
6+
ResourceUpdated(this.rawResponse)
7+
: resource = _resource(rawResponse.document) {
8+
final document = rawResponse.document;
9+
if (document != null) {
10+
included.addAll(InboundDocument(document).included());
11+
meta.addAll(InboundDocument(document).meta());
12+
links.addAll(InboundDocument(document).links());
1013
}
1114
}
1215

1316
static Resource? _resource(Map? json) {
1417
if (json != null) {
1518
final doc = InboundDocument(json);
16-
if (doc.hasData) {
17-
return doc.dataAsResource();
18-
}
19+
if (doc.hasData) return doc.dataAsResource();
1920
}
2021
return null;
2122
}
2223

23-
final Response httpResponse;
24+
// coverage:ignore-start
25+
/// The raw HTTP response
26+
@Deprecated('Use rawResponse.httpResponse instead')
27+
i.Response get httpResponse => rawResponse.httpResponse;
28+
// coverage:ignore-end
29+
30+
/// The raw JSON:API response
31+
final Response rawResponse;
2432

2533
/// The created resource. Null for "204 No Content" responses.
2634
late final Resource? resource;

0 commit comments

Comments
 (0)