|
2 | 2 | [JSON:API](http://jsonapi.org) is a specification for building APIs in JSON. This library implements |
3 | 3 | a Client (VM, Flutter, Web), and a Server (VM only). |
4 | 4 |
|
5 | | -## Supported features |
6 | | -- Fetching single resources and resource collections |
| 5 | +## Features |
| 6 | +- Fetching single resources, resource collections, related resources |
| 7 | +- Fetching/updating relationships |
| 8 | +- Creating/updating/deleting resources |
7 | 9 | - Collection pagination |
8 | | -- Fetching relationships and related resources and collections |
9 | | -- Fetching single resources |
10 | | -- Creating resources |
11 | | -- Deleting resources |
12 | | -- Updating resource's attributes |
13 | | -- Updating resource's relationships |
14 | | -- Updating relationships |
15 | 10 | - Compound documents |
16 | | -- Related collection pagination |
17 | 11 | - Asynchronous processing |
18 | 12 |
|
19 | 13 | ## Usage |
20 | | -In the VM: |
| 14 | +### Creating a client instance |
| 15 | +JSON:API Client uses the Dart's native HttpClient. Depending on the platform, |
| 16 | +you may want to use either the one which comes from `dart:io` or the `BrowserClient`. |
| 17 | + |
| 18 | +In the VM/Flutter you don't need to provide any dependencies: |
21 | 19 | ```dart |
22 | 20 | import 'package:json_api/client.dart'; |
23 | 21 |
|
24 | 22 | final client = JsonApiClient(); |
25 | 23 | ``` |
26 | 24 |
|
27 | | -In a browser: |
| 25 | +In a browser use the `BrowserClient`: |
28 | 26 | ```dart |
29 | 27 | import 'package:json_api/client.dart'; |
30 | 28 | import 'package:http/browser_client.dart'; |
31 | 29 |
|
32 | 30 | final client = JsonApiClient(factory: () => BrowserClient()); |
33 | 31 | ``` |
34 | 32 |
|
35 | | -For usage examples see the [functional tests](https://github.com/f3ath/json-api-dart/tree/master/test/functional). |
| 33 | +### Making requests |
| 34 | +The client provides a set of methods to manipulate resources and relationships. |
| 35 | +- Fetching |
| 36 | + - `fetchCollection` - resource collection, either primary or related |
| 37 | + - `fetchResource` - a single resource, either primary or related |
| 38 | + - `fetchRelationship` - a generic relationship (either to-one, to-many or even incomplete) |
| 39 | + - `fetchToOne` - a to-one relationship |
| 40 | + - `fetchToMany` - a to-many relationship |
| 41 | +- Manipulating resources |
| 42 | + - `createResource` - creates a new primary resource |
| 43 | + - `updateResource` - updates the existing resource by its type and id |
| 44 | + - `deleteResource` - deletes the existing resource |
| 45 | +- Manipulating relationships |
| 46 | + - `replaceToOne` - replaces the existing to-one relationship with a new resource identifier |
| 47 | + - `deleteToOne` - deletes the existing to-one relationship by setting the resrouce identifier to null |
| 48 | + - `replaceToMany` - replaces the existing to-many relationship with the given set of resource identifiers |
| 49 | + - `addToMany` - adds the given identifiers to the existing to-many relationship |
| 50 | + |
| 51 | +These methods accept the target URI and the object to update (except for fetch and delete requests). |
| 52 | +You can also pass an optional map of HTTP headers e.g. for authentication. The return value |
| 53 | +is `Response` object bearing the HTTP response status and headers and the JSON:API |
| 54 | +document with the primary data according to the type of the request. |
| 55 | + |
| 56 | +Here's a collection fetching example: |
| 57 | + |
| 58 | +```dart |
| 59 | +import 'package:json_api/client.dart'; |
| 60 | +
|
| 61 | +void main() async { |
| 62 | + final client = JsonApiClient(); |
| 63 | + final companiesUri = Uri.parse('http://localhost:8080/companies'); |
| 64 | + final response = await client.fetchCollection(companiesUri); |
| 65 | +
|
| 66 | + print('Status: ${response.status}'); |
| 67 | + print('Headers: ${response.headers}'); |
| 68 | +
|
| 69 | + print('The collection page size is ${response.data.collection.length}'); |
| 70 | +
|
| 71 | + final resource = response.data.collection.first.toResource(); |
| 72 | + print('The first element is ${resource}'); |
| 73 | +
|
| 74 | + print('Attributes:'); |
| 75 | + resource.attributes.forEach((k, v) => print('$k=$v')); |
| 76 | +
|
| 77 | + print('Relationships:'); |
| 78 | + resource.toOne.forEach((k, v) => print('$k=$v')); |
| 79 | + resource.toMany.forEach((k, v) => print('$k=$v')); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | +For more usage examples refer to the [functional tests](https://github.com/f3ath/json-api-dart/tree/master/test/functional). |
0 commit comments