GithubHelp home page GithubHelp logo

f3ath / json-api-dart Goto Github PK

View Code? Open in Web Editor NEW
79.0 3.0 24.0 409 KB

JSON:API client and server for Dart/Flutter

Home Page: https://pub.dartlang.org/packages/json_api

License: MIT License

Dart 100.00%
json-api json-api-client dart dartlang http http-client json rest-api rest jsonapi

json-api-dart's Introduction

JSON:API Client and Server

TL;DR:

import 'package:http_interop_http/http_interop_http.dart';
import 'package:json_api/client.dart';
import 'package:json_api/routing.dart';

void main() async {
  /// Define the server's base URL
  final baseUri = 'http://localhost:8080';

  /// Use the standard recommended URL structure or implement your own
  final uriDesign = StandardUriDesign(Uri.parse(baseUri));

  /// The [RoutingClient] is most likely the right choice.
  /// It has methods covering many standard use cases.
  final client = RoutingClient(uriDesign, Client(OneOffHandler()));

  try {
    /// Fetch the collection.
    /// See other methods to query and manipulate resources.
    final response = await client.fetchCollection('colors');

    final resources = response.collection;
    resources.map((resource) => resource.attributes).forEach((attr) {
      final name = attr['name'];
      final red = attr['red'];
      final green = attr['green'];
      final blue = attr['blue'];
      print('$name - $red:$green:$blue');
    });
  } on RequestFailure catch (e) {
    /// Catch error response
    for (var error in e.errors) {
      print(error.title);
    }
  }
}

This is a work-in-progress. You can help it by submitting a PR with a feature or documentation improvements.

json-api-dart's People

Contributors

dan12411 avatar f3ath avatar inobtenio avatar kszczek avatar sanekyy avatar shrop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

json-api-dart's Issues

Meta members have incorrect type

Hi,
i upgraded json_api library from 1.0.1 to 2.0.1+1 and library failed on parsing meta object in relationships.

The library expects Map<String, String> json object but official specification allows
unrestricted json object (see https://jsonapi.org/format/#document-meta)

Stacktrace:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, String>'
#0 IdentifierObject.decodeJson (package:json_api/src/document/identifier_object.dart:16:67)
#1 MappedListIterable.elementAt (dart:_internal/iterable.dart:414:29)
#2 ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
#3 List.addAll (dart:core-patch/growable_array.dart:193:13)
#4 new ToMany (package:json_api/src/document/relationship.dart:121:18)
#5 ToMany.decodeJson (package:json_api/src/document/relationship.dart:131:18)
#6 Relationship.decodeJson (package:json_api/src/document/relationship.dart:38:25)
#7 Relationship.decodeJsonMap. (package:json_api/src/document/relationship.dart:53:62)
#8 __InternalLinkedHashMap&_HashVMBase&MapMixin.map (dart:collection/maps.dart:163:28)
#9 Relationship.decodeJsonMap (package:json_api/src/document/relationship.dart:53:12)
#10 ResourceObject.decodeJson (package:json_api/src/document/resource_object.dart:42:41)
#11 MappedListIterable.elementAt (dart:_internal/iterable.dart:414:29)
#12 ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
#13 List.addAll (dart:core-patch/growable_array.dart:193:13)
#14 new ResourceCollectionData (package:json_api/src/document/resource_collection_data.dart:19:21)
#15 ResourceCollectionData.decodeJson (package:json_api/src/document/resource_collection_data.dart:27:16)
#16 Document.decodeJson (package:json_api/src/document/document.dart:43:42)
#17 JsonApiClient._call (package:json_api/src/client/client.dart:177:44)

#18 JsonApiClient.fetchCollection (package:json_api/src/client/client.dart:31:7)

Thanks for your reply
(And sorry for creepy english)

Can't get "set-cookie" headers

Hello.
Your headers positional argument in any request is type of Map<Stirng, String>. But set-cookie can be used twice or more in one request. So it is not valid type for it. Can you change it to Map<String, List<String>> ?

Cannot handle server error

error_object.dart cannot handle server error like one in the screenshot below.

image

The real server response is 402 Unprocessable Entity, but, if we try to catch the exception, we got _CastError (type 'int' is not a subtype of type 'String' in type cast) instead

Parse non-standard links

when i'm trying to parse response that looks like following

{
data:{
           type:token,
           id: baseurl/path1/uuid,
           attributes:{
                             label1:value...
           },
           links:{
           link1label: link1
           link2label: link2
           }
       }
}

i cannot get links value parsed and stored in response.data.links or response.document.data.links .. what m i doing wrong? I get attributes parsed correctly got both data and data in document ResourceData objects. The links don't have self and i want check the other values only

EDIT : I found that on Line 170 of client.dart has the links section but after that it is missing.

Validate Documents

  • Naming
  • Structure (including compound documents and sparse field sets)

Correct documentation in JsonApiClient class

Please correct the following code from your documentation:

final httpClient = Client();
final jsonApiClient = JsonApiClient(httpClient);
final url = Uri.parse('http://localhost:8080/companies/2');
final response = await jsonApiClient.fetchCollection(url);
httpClient.close(); // Don't forget to close the http client
...

When you just copy the code in a test to get some experience how the library works, you get the following two problems:

  • the construction of Client() doesn't work (anymore)
  • it is also not possible to call httpClient.close()

Can not decode related resource which is null

https://jsonapi.org/format/#fetching-resources-responses

A server MUST respond to a successful request to fetch an individual resource with a resource object or null provided as the response document’s primary data.

null is only an appropriate response when the requested URL is one that might correspond to a single resource, but doesn’t currently.

Note: Consider, for example, a request to fetch a to-one related resource link. This request would respond with null when the relationship is empty (such that the link is corresponding to no resources) but with the single related resource’s resource object otherwise.

How do I access "included"

The JSON:API specification as implemented in the Drupal JSON:API module attaches images files to the included key in the JSON:API document. However, I have not seen how to access it using the methods available in the API.

I saw it mentioned by @mglaman in #1 (comment) but it didn't seem like it made it into the client.

Is there a way to do this with the current client?

Decode/Encode only Usage

Greetings! First of all, thanks for caring the community and making your library open source!

My question is, is there a way to use decoding/encoding functionality without using http request functions?

Since example code is not defining this future, i wanted to ask for help :)

Thanks!

Do not send id null in JSON

When I call

jsonApiClient.createResource(uri, Resource('customers', null, attributes: {'name': 'Prabhakar'}));

It sends following JSON.

{
  "data":{
    "type":"customers",
    "id":null,
    "attributes":{
      "name":"Prabhakar"
    }
  }
}

In turn, the server throws following error:

{
  "errors":[
    {
      "status":"400",
      "title":"Non-Compliant JSON API Document",
      "detail":"The member id must be a string.",
      "source":{
        "pointer":"/data/id"
      }
    },
    {
      "status":"403",
      "title":"Not Supported",
      "detail":"Resource type customers does not support client-generated IDs.",
      "source":{
        "pointer":"/data/id"
      }
    }
  ]
}

Is it possible to prevent ID field from being sent?

Export dart_http

Hello.

I started use your package. Thanks for your work.

I use version 4.

I use default Dart HttpClient and wrap it with your DartHttp to pass it into JsonApiClient.

DartHttp is not export from your package. So I have warning of lint.

Fix it please:)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.