Pakete, Repositories und App-Kategorien suchen

GitHub-Repositoryf3ath/json-api-dart

json-api-dart

JSON:API-Client und -Server für Dart/Flutter

Projektcover von json-api-dart
Sterne
79
Forks
23
Letzter Push (UTC)
30.09.2024
Projektstatus
Aktiv

Original-README

Englischer Projektschnappschuss. Aktuelle Inhalte auf GitHub.

JSON:API Client and Server

TL;DR:

import 'package:http/http.dart' as http;
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));

  /// This is the Dart's standard HTTP client.
  /// Do not forget to close it in the end.
  final httpClient = http.Client();

  /// This is the interface which decouples this JSON:API implementation
  /// from the HTTP client.
  /// Learn more: https://pub.dev/packages/http_interop
  final httpHandler = httpClient.handleInterop;

  /// This is the basic JSON:API client. It is flexible but not very convenient
  /// to use, because you would need to remember a lot of JSON:API protocol details.
  /// We will use another wrapper on top of it.
  final jsonApiClient = Client(httpHandler);

  /// The [RoutingClient] is most likely the right choice.
  /// It is called routing because it routes the calls to the correct
  /// URLs depending on the use case. Take a look at its methods, they cover
  /// all the standard scenarios specified by the JSON:API standard.
  final client = RoutingClient(uriDesign, jsonApiClient);

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

    /// The fetched collection allows us to iterate over the resources
    /// and to look into their attributes
    for (final resource in response.collection) {
      final {
      'name': name,
      'red': red,
      'green': green,
      'blue': blue,
      } = resource.attributes;
      print('${resource.type}:${resource.id}');
      print('$name - $red:$green:$blue');
    }
  } on RequestFailure catch (e) {
    /// Catch error response
    for (final error in e.errors) {
      print(error.title);
    }
  }

  /// Free up the resources before exit.
  httpClient.close();
}

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

Alexey GitHub avatar
GitHub-Autor

Alexey ↗

If I had ever learnt, I should have been a great proficient.

Salesforce Silicon Valley, CA
SprachenDart

Kategorien

Von diesem Repository veröffentlichte Pakete

Verwendete Abhängigkeiten

Abhängigkeiten 11 Einträge

  • http_parser^4.0.0
  • http_interop^2.0.0
  • lintsEntwicklung^4.0.0
  • testEntwicklung^1.21.1
  • stream_channelEntwicklung^2.1.0
  • uuidEntwicklung^4.2.0
  • coverageEntwicklung^1.3.0
  • check_coverageEntwicklung^0.0.4
  • httpEntwicklung^1.2.1
  • http_interop_httpEntwicklung^0.9.0
  • http_interop_ioEntwicklung^0.9.0

Wie wähle ich ein passendes Repository?

Nach Anwendungsfall filtern, Aktivität vergleichen und vor der Nutzung das Originalprojekt prüfen.

  1. Nach Name, Autor oder Beschreibung suchen und nach Thema eingrenzen.
  2. Sterne, Forks und letzte Pushes vergleichen. Sterne zeigen Interesse, garantieren aber keine Qualität.
  3. Vor der Nutzung README, Lizenz und Issues auf GitHub prüfen.

Quellen und Pflege

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗