rest_dart 0.1.1

rest_dart #

A library for automating much of the minutia of creating a rest interface. This mostly means automatically sending the correct error messages when a request is invalid, and some automatic header handling, such as:

  • Automatic 204 messages when your resource returns no data but is still succesful
  • Automatic 404 messages when a requested resource does not exist.
  • Automatic 405 errors when a method is not valid for a resource.
  • Automatic 406 errors when requested content type is not available (currently handled globally), or is not a valid content type.
  • Automatic handling of the OPTIONS method, and automatic Allow header generation.

###TODO:

  • The error handling outputs the errors as JSON. This needs to be more flexible, and overridable.
  • Per-resource content type support.
  • Add support for variable URL prefixes. Currently assumes running right under the host name/ip.
  • Change rest event handlers to be generic functions instead of classes.
  • Sets VERY generous Access-Control-Allow-Origin and Access-Control-Allow-Methods to allow cross-domain testing (this will be chagned to be mroe secure in the future).

###Usage!

// Import the package
import 'package:rest_dart/rest_dart.dart';

// Create the server object.
RestServer rest = new RestServer();

// If you don't add a content type, the server will throw an error on the first request.
rest.AddDefaultContentType(new ContentType("application", "json", charset: "utf-8"));

// Create a REST resource, along with the regex for recognizing its path.
RestResource resource = new RestResource(r'^/?$');

// Create a method handler
Future DeleteHandler(ContentType type, String path, Map<String, String> args) {
  return new Future.sync(() {
    return "test";
  });
}

// Add the method handler to the resource 
resource.SetMethodHandler("DELETE", DeleteHandler);

// Add the resource to the server,
rest.AddResource(resource);

// Start the server. This handles creating the HTTP server as well.
rest.Start("127.0.0.1",8080);

// Begin requesting from the server!

###Errors

So, if something goes wrong and you need to send an error message back to the user, just throw a RestException!

RestException(this.Code, this.Message, [this.InnerException = null]);

This will cause the server to send back the HTTP code you specify, and be presented with your message. Make sure to throw it through a future, so that it can be caught by the higher-up logic:

Future DeleteHandler(ContentType type, String path, Map<String, String> args) {
  return new Future.sync(() {
    throw new RestException(500,"I don't like you");
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  rest_dart: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:rest_dart/rest_dart.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK 1.3.6