Skip to content

Add rawResponse #144

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

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions lib/src/client/response/collection_fetched.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import 'package:http_interop/http_interop.dart';
import 'package:http_interop/http_interop.dart' as i;
import 'package:json_api/document.dart';
import 'package:json_api/src/client/response.dart';

class CollectionFetched {
CollectionFetched(this.httpResponse, Map json) {
final document = InboundDocument(json);
CollectionFetched(this.rawResponse) {
final document = InboundDocument(rawResponse.document ??
(throw FormatException('The document must not be empty')));
collection.addAll(document.dataAsCollection());
included.addAll(document.included());
meta.addAll(document.meta());
links.addAll(document.links());
}

final Response httpResponse;
// coverage:ignore-start
/// The raw HTTP response
@Deprecated('Use rawResponse.httpResponse instead')
i.Response get httpResponse => rawResponse.httpResponse;
// coverage:ignore-end

/// The raw JSON:API response
final Response rawResponse;

/// The resource collection fetched from the server
final collection = <Resource>[];
Expand Down
21 changes: 15 additions & 6 deletions lib/src/client/response/related_resource_fetched.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import 'package:http_interop/http_interop.dart';
import 'package:http_interop/http_interop.dart' as i;
import 'package:json_api/document.dart';
import 'package:json_api/src/client/response.dart';

/// A related resource response.
///
/// https://jsonapi.org/format/#fetching-resources-responses
class RelatedResourceFetched {
RelatedResourceFetched(this.httpResponse, Map json)
: resource = InboundDocument(json).dataAsResourceOrNull() {
final document = InboundDocument(json);
RelatedResourceFetched(this.rawResponse) {
final document = InboundDocument(rawResponse.document ??
(throw FormatException('The document must not be empty')));
resource = document.dataAsResourceOrNull();
included.addAll(document.included());
meta.addAll(document.meta());
links.addAll(document.links());
}

final Response httpResponse;
// coverage:ignore-start
/// The raw HTTP response
@Deprecated('Use rawResponse.httpResponse instead')
i.Response get httpResponse => rawResponse.httpResponse;
// coverage:ignore-end

/// The raw JSON:API response
final Response rawResponse;

/// Related resource. May be null
final Resource? resource;
late final Resource? resource;

/// Included resources
final included = <Resource>[];
Expand Down
33 changes: 24 additions & 9 deletions lib/src/client/response/relationship_fetched.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import 'package:http_interop/http_interop.dart';
import 'package:http_interop/http_interop.dart' as i;
import 'package:json_api/document.dart';
import 'package:json_api/src/client/response.dart';

/// A response to a relationship fetch request.
class RelationshipFetched<R extends Relationship> {
RelationshipFetched(this.httpResponse, this.relationship);
RelationshipFetched(this.rawResponse, this.relationship);

static RelationshipFetched<ToMany> many(Response httpResponse, Map json) =>
RelationshipFetched(httpResponse, InboundDocument(json).asToMany())
..included.addAll(InboundDocument(json).included());
static RelationshipFetched<ToMany> many(Response response) {
final document = InboundDocument(response.document ??
(throw FormatException('The document must not be empty')));
return RelationshipFetched(response, document.asToMany())
..included.addAll(document.included());
}

static RelationshipFetched<ToOne> one(Response httpResponse, Map json) =>
RelationshipFetched(httpResponse, InboundDocument(json).asToOne())
..included.addAll(InboundDocument(json).included());
static RelationshipFetched<ToOne> one(Response response) {
final document = InboundDocument(response.document ??
(throw FormatException('The document must not be empty')));
return RelationshipFetched(response, document.asToOne())
..included.addAll(document.included());
}

// coverage:ignore-start
/// The raw HTTP response
@Deprecated('Use rawResponse.httpResponse instead')
i.Response get httpResponse => rawResponse.httpResponse;
// coverage:ignore-end

/// The raw JSON:API response
final Response rawResponse;

final Response httpResponse;
final R relationship;
final included = <Resource>[];
}
30 changes: 21 additions & 9 deletions lib/src/client/response/relationship_updated.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import 'package:http_interop/http_interop.dart';
import 'package:http_interop/http_interop.dart' as i;
import 'package:json_api/document.dart';
import 'package:json_api/src/client/response.dart';

/// A response to a relationship request.
class RelationshipUpdated<R extends Relationship> {
RelationshipUpdated(this.httpResponse, this.relationship);
RelationshipUpdated(this.rawResponse, this.relationship);

static RelationshipUpdated<ToMany> many(Response httpResponse, Map? json) =>
RelationshipUpdated(
httpResponse, json == null ? null : InboundDocument(json).asToMany());
static RelationshipUpdated<ToMany> many(Response response) {
final json = response.document;
return RelationshipUpdated(
response, json == null ? null : InboundDocument(json).asToMany());
}

static RelationshipUpdated<ToOne> one(Response httpResponse, Map? json) =>
RelationshipUpdated(
httpResponse, json == null ? null : InboundDocument(json).asToOne());
static RelationshipUpdated<ToOne> one(Response response) {
final json = response.document;
return RelationshipUpdated(
response, json == null ? null : InboundDocument(json).asToOne());
}

final Response httpResponse;
// coverage:ignore-start
/// The raw HTTP response
@Deprecated('Use rawResponse.httpResponse instead')
i.Response get httpResponse => rawResponse.httpResponse;
// coverage:ignore-end

/// The raw JSON:API response
final Response rawResponse;

/// Updated relationship. Null if "204 No Content" is returned.
final R? relationship;
Expand Down
25 changes: 17 additions & 8 deletions lib/src/client/response/request_failure.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import 'package:http_interop/http_interop.dart';
import 'package:http_interop/http_interop.dart' as i;
import 'package:json_api/document.dart';
import 'package:json_api/src/client/response.dart';

/// Thrown when the server returns a non-successful response.
class RequestFailure implements Exception {
RequestFailure(this.httpResponse, Map? document) {
if (document != null) {
errors.addAll(InboundDocument(document).errors());
meta.addAll(InboundDocument(document).meta());
}
RequestFailure(this.rawResponse) {
final json = rawResponse.document;
if (json == null) return;
final document = InboundDocument(json);
errors.addAll(document.errors());
meta.addAll(document.meta());
}

final Response httpResponse;
// coverage:ignore-start
/// The raw HTTP response
@Deprecated('Use rawResponse.httpResponse instead')
i.Response get httpResponse => rawResponse.httpResponse;
// coverage:ignore-end

/// The raw JSON:API response
final Response rawResponse;

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

@override
String toString() =>
'JSON:API request failed with HTTP status ${httpResponse.statusCode}.';
'JSON:API request failed with HTTP status ${rawResponse.httpResponse.statusCode}.';
}
26 changes: 18 additions & 8 deletions lib/src/client/response/resource_created.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import 'package:http_interop/http_interop.dart';
import 'package:http_interop/http_interop.dart' as i;
import 'package:json_api/document.dart';
import 'package:json_api/src/client/response.dart';

/// A response to a new resource creation request.
/// This is always a "201 Created" response.
///
/// https://jsonapi.org/format/#crud-creating-responses-201
class ResourceCreated {
ResourceCreated(this.httpResponse, Map json)
: resource = InboundDocument(json).dataAsResource() {
meta.addAll(InboundDocument(json).meta());
links.addAll(InboundDocument(json).links());
included.addAll(InboundDocument(json).included());
ResourceCreated(this.rawResponse) {
final document = InboundDocument(rawResponse.document ??
(throw FormatException('The document must not be empty')));
resource = document.dataAsResource();
included.addAll(document.included());
meta.addAll(document.meta());
links.addAll(document.links());
}

final Response httpResponse;
// coverage:ignore-start
/// The raw HTTP response
@Deprecated('Use rawResponse.httpResponse instead')
i.Response get httpResponse => rawResponse.httpResponse;
// coverage:ignore-end

/// The raw JSON:API response
final Response rawResponse;

/// Created resource.
final Resource resource;
late final Resource resource;

/// Top-level meta data
final meta = <String, Object?>{};
Expand Down
27 changes: 19 additions & 8 deletions lib/src/client/response/resource_fetched.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import 'package:http_interop/http_interop.dart';
import 'package:http_interop/http_interop.dart' as i;
import 'package:json_api/document.dart';
import 'package:json_api/src/client/response.dart';

/// A response to fetch a primary resource request
class ResourceFetched {
ResourceFetched(this.httpResponse, Map json)
: resource = InboundDocument(json).dataAsResource() {
included.addAll(InboundDocument(json).included());
meta.addAll(InboundDocument(json).meta());
links.addAll(InboundDocument(json).links());
ResourceFetched(this.rawResponse) {
final document = InboundDocument(rawResponse.document ??
(throw FormatException('The document must not be empty')));
resource = document.dataAsResource();
included.addAll(document.included());
meta.addAll(document.meta());
links.addAll(document.links());
}

final Response httpResponse;
final Resource resource;
// coverage:ignore-start
/// The raw HTTP response
@Deprecated('Use rawResponse.httpResponse instead')
i.Response get httpResponse => rawResponse.httpResponse;
// coverage:ignore-end

/// The raw JSON:API response
final Response rawResponse;

late final Resource resource;

/// Top-level meta data
final meta = <String, Object?>{};
Expand Down
28 changes: 18 additions & 10 deletions lib/src/client/response/resource_updated.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import 'package:http_interop/http_interop.dart';
import 'package:http_interop/http_interop.dart' as i;
import 'package:json_api/document.dart';
import 'package:json_api/src/client/response.dart';

class ResourceUpdated {
ResourceUpdated(this.httpResponse, Map? json) : resource = _resource(json) {
if (json != null) {
included.addAll(InboundDocument(json).included());
meta.addAll(InboundDocument(json).meta());
links.addAll(InboundDocument(json).links());
ResourceUpdated(this.rawResponse)
: resource = _resource(rawResponse.document) {
final document = rawResponse.document;
if (document != null) {
included.addAll(InboundDocument(document).included());
meta.addAll(InboundDocument(document).meta());
links.addAll(InboundDocument(document).links());
}
}

static Resource? _resource(Map? json) {
if (json != null) {
final doc = InboundDocument(json);
if (doc.hasData) {
return doc.dataAsResource();
}
if (doc.hasData) return doc.dataAsResource();
}
return null;
}

final Response httpResponse;
// coverage:ignore-start
/// The raw HTTP response
@Deprecated('Use rawResponse.httpResponse instead')
i.Response get httpResponse => rawResponse.httpResponse;
// coverage:ignore-end

/// The raw JSON:API response
final Response rawResponse;

/// The created resource. Null for "204 No Content" responses.
late final Resource? resource;
Expand Down
Loading
Loading