11import 'package:json_api/document.dart' ;
22import 'package:json_api/src/client/status_code.dart' ;
3+ import 'package:json_api/src/nullable.dart' ;
34
4- /// A response returned by JSON:API cars_server
5+ /// A response returned by JSON:API client
56class Response <Data extends PrimaryData > {
67 /// HTTP status code
78 final int status;
@@ -10,32 +11,46 @@ class Response<Data extends PrimaryData> {
1011 /// May be null.
1112 final Document <Data > document;
1213
14+ /// The document received with `202 Accepted` response (if any)
15+ /// https://jsonapi.org/recommendations/#asynchronous-processing
16+ final Document <ResourceData > asyncDocument;
17+
1318 /// Headers returned by the server.
1419 final Map <String , String > headers;
1520
16- Response (this .status, this .headers, {this .document}) {
17- // TODO: Check for null and content-type
18- }
21+ Response (this .status, this .headers, {this .document, this .asyncDocument});
1922
2023 /// Primary Data from the document (if any)
2124 Data get data => document.data;
2225
26+ /// Primary Data from the async document (if any)
27+ ResourceData get asyncData => asyncDocument.data;
28+
2329 /// Was the request successful?
2430 ///
25- /// For pending (202 Accepted) requests [isSuccessful] is always false.
31+ /// For pending (202 Accepted) requests both [isSuccessful] and [isFailed]
32+ /// are always false.
2633 bool get isSuccessful => StatusCode (status).isSuccessful;
2734
28- /// Is a request is accepted but not finished yet (e.g. queued) [isPending] is true .
29- /// HTTP Status 202 Accepted should be returned for pending requests .
30- /// The "Content-Location" header should have a link to the job queue and
31- /// [document] should contain a queued job resource object.
35+ /// This property is an equivalent of `202 Accepted` HTTP status .
36+ /// It indicates that the request is accepted but not finished yet (e.g. queued) .
37+ /// The [contentLocation] should have a link to the job queue resource and
38+ /// [asyncData] may contain a queued job resource object.
3239 ///
3340 /// See: https://jsonapi.org/recommendations/#asynchronous-processing
34- bool get isPending => StatusCode (status).isPending;
41+ bool get isAsync => StatusCode (status).isPending;
3542
3643 /// Any non 2** status code is considered a failed operation.
3744 /// For failed requests, [document] is expected to contain [ErrorDocument]
3845 bool get isFailed => StatusCode (status).isFailed;
3946
40- String get location => headers['location' ];
47+ /// The `Location` HTTP header value. For `201 Created` responses this property
48+ /// contains the location of a newly created resource.
49+ Uri get location => nullable (Uri .parse)(headers['location' ]);
50+
51+ /// The `Content-Location` HTTP header value. For `202 Accepted` responses
52+ /// this property contains the location of the Job Queue resource.
53+ ///
54+ /// More details: https://jsonapi.org/recommendations/#asynchronous-processing
55+ Uri get contentLocation => nullable (Uri .parse)(headers['content-location' ]);
4156}
0 commit comments