Skip to content

Commit dba52d9

Browse files
authored
Better naming and docs (#48)
1 parent 7599281 commit dba52d9

File tree

3 files changed

+93
-34
lines changed

3 files changed

+93
-34
lines changed

README.md

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ Other JSON:API packages: [Document](https://pub.dartlang.org/packages/json_api_d
44

55
# JSON:API Client
66

7-
[JSON:API](http://jsonapi.org) is a specification for building APIs in JSON. This library implements
8-
a Client (VM, Flutter, Web).
7+
[JSON:API](http://jsonapi.org) is a specification for building APIs in JSON. This package implements
8+
the Client.
99

1010
## Features
1111
- Fetching single resources, resource collections, related resources
1212
- Fetching/updating relationships
1313
- Creating/updating/deleting resources
1414
- Collection pagination
15-
- Compound documents
15+
- Compound documents (included resources)
1616
- Asynchronous processing
1717

1818
## Usage
@@ -38,24 +38,25 @@ final client = JsonApiClient(factory: () => BrowserClient());
3838
### Making requests
3939
The client provides a set of methods to manipulate resources and relationships.
4040
- Fetching
41-
- `fetchCollection` - resource collection, either primary or related
42-
- `fetchResource` - a single resource, either primary or related
43-
- `fetchRelationship` - a generic relationship (either to-one, to-many or even incomplete)
44-
- `fetchToOne` - a to-one relationship
45-
- `fetchToMany` - a to-many relationship
41+
- [fetchCollection](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/fetchCollection.html) - resource collection, either primary or related
42+
- [fetchResource](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/fetchResource.html) - a single resource, either primary or related
43+
- [fetchRelationship](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/fetchRelationship.html) - a generic relationship (either to-one, to-many or even incomplete)
44+
- [fetchToOne](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/fetchToOne.html) - a to-one relationship
45+
- [fetchToMany](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/fetchToMany.html) - a to-many relationship
4646
- Manipulating resources
47-
- `createResource` - creates a new primary resource
48-
- `updateResource` - updates the existing resource by its type and id
49-
- `deleteResource` - deletes the existing resource
47+
- [createResource](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/createResource.html) - creates a new primary resource
48+
- [updateResource](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/updateResource.html) - updates the existing resource by its type and id
49+
- [deleteResource](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/deleteResource.html) - deletes the existing resource
5050
- Manipulating relationships
51-
- `replaceToOne` - replaces the existing to-one relationship with a new resource identifier
52-
- `deleteToOne` - deletes the existing to-one relationship by setting the resource identifier to null
53-
- `replaceToMany` - replaces the existing to-many relationship with the given set of resource identifiers
54-
- `addToMany` - adds the given identifiers to the existing to-many relationship
51+
- [replaceToOne](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/replaceToOne.html) - replaces the existing to-one relationship with a new resource identifier
52+
- [deleteToOne](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/deleteToOne.html) - deletes the existing to-one relationship by setting the resource identifier to null
53+
- [replaceToMany](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/replaceToMany.html) - replaces the existing to-many relationship with the given set of resource identifiers
54+
- [addToMany](https://pub.dartlang.org/documentation/json_api/latest/json_api/JsonApiClient/addToMany.html) - adds the given identifiers to the existing to-many relationship
5555

5656
These methods accept the target URI and the object to update (except for fetch and delete requests).
5757
You can also pass an optional map of HTTP headers e.g. for authentication. The return value
58-
is `Response` object bearing the HTTP response status and headers and the JSON:API
58+
is [Response](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response-class.html) object bearing the
59+
HTTP response status and headers and the JSON:API
5960
document with the primary data according to the type of the request.
6061

6162
Here's a collection fetching example:
@@ -85,5 +86,60 @@ void main() async {
8586
}
8687
```
8788

88-
89+
### The Response object
90+
The Client always returns a [Response object](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response-class.html)
91+
which indicates either a successful, failed, or async (neither failed nor successful yet, see [here](https://jsonapi.org/recommendations/#asynchronous-processing)) operation.
92+
You can determine which one you have by reading these properties:
93+
- [isSuccessful](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/isSuccessful.html)
94+
- [isFailed](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/isFailed.html)
95+
- [isAsync](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/isAsync.html)
96+
97+
The Response also contains [HTTP status](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/status.html)
98+
and a map of [HTTP headers](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/headers.html).
99+
Two headers used by JSON:API can be accessed directly for your convenience:
100+
- [location](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/location.html) -
101+
the `Location:` header used in creation requests
102+
- [contentLocation](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/contentLocation.html) -
103+
the `Content-Location:` header used for asynchronous processing
104+
105+
### The Response Document
106+
The most important part of the Response is the [document](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/document.html)
107+
property which contains the JSON:API document sent by the server (if any). If the document has Primary Data, you
108+
can use [data](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/data.html)
109+
shortcut to access it directly. Like the Document, the Response is generalized by the expected Primary Data
110+
which depends of the operation. The Document and the rest of the JSON:API object model are parts of [json_api_document](https://pub.dartlang.org/packages/json_api_document)
111+
which is a separate package. Refer to that package for [complete API documentation](https://pub.dartlang.org/documentation/json_api_document/latest/).
112+
This README only present a brief overview.
113+
114+
#### Successful responses
115+
Most of the times when the response is successful, you can read the [data](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/data.html)
116+
property directly. It will be either a [primary resource](https://pub.dartlang.org/documentation/json_api_document/latest/json_api_document/ResourceData-class.html)
117+
, primary [resource collection](https://pub.dartlang.org/documentation/json_api_document/latest/json_api_document/ResourceCollectionData-class.html),
118+
or a relationship: [to-one](https://pub.dartlang.org/documentation/json_api_document/latest/json_api_document/ToOne-class.html)
119+
or [to-many](https://pub.dartlang.org/documentation/json_api_document/latest/json_api_document/ToMany-class.html).
120+
The collection-like data may also contain [pagination links](https://pub.dartlang.org/documentation/json_api_document/latest/json_api_document/Pagination-class.html).
121+
122+
#### Included resources
123+
If you requested related resources to be included in the response (see [Compound Documents](https://jsonapi.org/format/#document-compound-documents)) and the server fulfilled
124+
your request, the [included](https://pub.dartlang.org/documentation/json_api_document/latest/json_api_document/PrimaryData/included.html) property will contain them.
125+
126+
#### Errors
127+
For unsuccessful operations the [data](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/data.html)
128+
property will be null. If the server decided to include the error details in the response, those can be found in the
129+
[errors](https://pub.dartlang.org/documentation/json_api_document/latest/json_api_document/Document/errors.html) property.
130+
131+
132+
#### Async processing
133+
Some servers may to support [Asynchronous Processing](https://jsonapi.org/recommendations/#asynchronous-processing).
134+
When the server responds with `202 Accepted`, the client expects the Primary Data to always be a Resource (usually
135+
representing a job queue). In this case, the [document](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/document.html)
136+
and the [data](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/data.html)
137+
properties of the Response will be null. Instead,
138+
the response document will be placed to [asyncDocument](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/asyncDocument.html)
139+
(and [asyncData](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/asyncData.html)).
140+
Also in this case the [contentLocation](https://pub.dartlang.org/documentation/json_api/latest/json_api/Response/contentLocation.html)
141+
will point to the job queue resource. You can fetch the job queue resource periodically and check
142+
the type of the returned resource. Once the operation is complete, the request will return the created resource.
143+
144+
### Further reading
89145
For more usage examples refer to the [functional tests](https://github.com/f3ath/json-api-dart/tree/master/test/functional).

lib/src/client.dart

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,32 @@ class JsonApiClient {
8181
/// Updates a to-one relationship via PATCH request
8282
///
8383
/// https://jsonapi.org/format/#crud-updating-to-one-relationships
84-
Future<Response<ToOne>> replaceToOne(Uri uri, Identifier id,
84+
Future<Response<ToOne>> replaceToOne(Uri uri, Identifier identifier,
8585
{Map<String, String> headers}) =>
86-
_patch(_parser.parseToOne, uri,
87-
ToOne(nullable(IdentifierObject.fromIdentifier)(id)), headers);
86+
_patch(
87+
_parser.parseToOne,
88+
uri,
89+
ToOne(nullable(IdentifierObject.fromIdentifier)(identifier)),
90+
headers);
8891

8992
/// Removes a to-one relationship. This is equivalent to calling [replaceToOne]
9093
/// with id = null.
9194
Future<Response<ToOne>> deleteToOne(Uri uri, {Map<String, String> headers}) =>
9295
replaceToOne(uri, null, headers: headers);
9396

94-
/// Replaces a to-many relationship with the given set of [ids].
97+
/// Replaces a to-many relationship with the given set of [identifiers].
9598
///
9699
/// The server MUST either completely replace every member of the relationship,
97100
/// return an appropriate error response if some resources can not be found or accessed,
98101
/// or return a 403 Forbidden response if complete replacement is not allowed by the server.
99102
///
100103
/// https://jsonapi.org/format/#crud-updating-to-many-relationships
101-
Future<Response<ToMany>> replaceToMany(Uri uri, List<Identifier> ids,
104+
Future<Response<ToMany>> replaceToMany(Uri uri, List<Identifier> identifiers,
102105
{Map<String, String> headers}) =>
103106
_patch(_parser.parseToMany, uri,
104-
ToMany(ids.map(IdentifierObject.fromIdentifier)), headers);
107+
ToMany(identifiers.map(IdentifierObject.fromIdentifier)), headers);
105108

106-
/// Adds the given set of [ids] to a to-many relationship.
109+
/// Adds the given set of [identifiers] to a to-many relationship.
107110
///
108111
/// The server MUST add the specified members to the relationship
109112
/// unless they are already present.
@@ -121,10 +124,10 @@ class JsonApiClient {
121124
/// caused by multiple clients making the same changes to a relationship.
122125
///
123126
/// https://jsonapi.org/format/#crud-updating-to-many-relationships
124-
Future<Response<ToMany>> addToMany(Uri uri, List<Identifier> ids,
127+
Future<Response<ToMany>> addToMany(Uri uri, List<Identifier> identifiers,
125128
{Map<String, String> headers}) =>
126129
_post(_parser.parseToMany, uri,
127-
ToMany(ids.map(IdentifierObject.fromIdentifier)), headers);
130+
ToMany(identifiers.map(IdentifierObject.fromIdentifier)), headers);
128131

129132
Future<Response<D>> _get<D extends PrimaryData>(
130133
D parse(Object _), uri, Map<String, String> headers) =>
@@ -176,18 +179,18 @@ class JsonApiClient {
176179
Future<http.Response> fn(http.Client client)) async {
177180
final client = _factory();
178181
try {
179-
final httpResponse = await fn(client);
180-
if (httpResponse.body.isEmpty) {
181-
return Response(httpResponse.statusCode, httpResponse.headers);
182+
final response = await fn(client);
183+
if (response.body.isEmpty) {
184+
return Response(response.statusCode, response.headers);
182185
}
183-
final body = json.decode(httpResponse.body);
184-
if (StatusCode(httpResponse.statusCode).isPending) {
185-
return Response(httpResponse.statusCode, httpResponse.headers,
186+
final body = json.decode(response.body);
187+
if (StatusCode(response.statusCode).isPending) {
188+
return Response(response.statusCode, response.headers,
186189
asyncDocument: body == null
187190
? null
188191
: _parser.parseDocument(body, _parser.parseResourceData));
189192
}
190-
return Response(httpResponse.statusCode, httpResponse.headers,
193+
return Response(response.statusCode, response.headers,
191194
document: body == null ? null : _parser.parseDocument(body, parse));
192195
} finally {
193196
client.close();

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ author: "Alexey Karapetov <karapetov@gmail.com>"
22
description: "JSON:API Client for Flutter, Web and VM. Supports JSON:API v1.0 (http://jsonapi.org)"
33
homepage: "https://github.com/f3ath/json-api-dart"
44
name: "json_api"
5-
version: "1.1.0-dev.2"
5+
version: "1.1.0-dev.3"
66
dependencies:
77
json_api_document: "^1.1.0-dev"
88
collection: "^1.14.11"

0 commit comments

Comments
 (0)