coffee_http 0.2.3 copy "coffee_http: ^0.2.3" to clipboard
coffee_http: ^0.2.3 copied to clipboard

outdatedDart 1 only

Easy http request

coffee_http #

Easy http request for browser and server.

Usage #

A simple usage example: #

import "package:coffee_http/coffee.dart";

main() {

    Get request = new Get("http://localhost/data", middlewares: [
        JSON_CONTENT_TYPE,
        ENCODE_TO_JSON_MIDDLEWARE,
        DECODE_FROM_JSON_MIDDLEWARE,
    ]);
    
    request.execute().then((CoffeeResponse res) => print(res.decodedBody));

}

More complexe example: #

Example

Model

class ResourceModel {
  String name;

  ResourceModel(this.name);

  ResourceModel.fromMap(Map json) {
    name = json["name"];
  }

  Map toMap() => { "name": name };

  String toString() => toMap().toString();

}

Define Requests

CoffeeRequester api = new CoffeeRequester(middlewares: [
        new ResolveApiMiddleware("http://localhost", 9000, subPath: "/api"),
        LOGGER_MIDDLEWARE,
        JSON_CONTENT_TYPE,
        ENCODE_TO_JSON_MIDDLEWARE,
        DECODE_FROM_JSON_MIDDLEWARE,
    ], client: client);

    api["resources"] = new Get("/resources", decoder: ResourceModel.listDecoder);
    api["resources"]["read"] = new Get("/{name}", decoder: ResourceModel.decoder);
    api["resources"]["create"] = new Post("/create",
        encoder: ResourceModel.encoder, decoder: ResourceModel.decoder);

Use it

initApi();

api["resources"].execute().then((CoffeeResponse _res) {
print(_res.decodedBody as List<ResourceModel>);
});

api["resources"]["create"]
  .execute(body: new ResourceModel()
    ..name = "toto"
    ..capacity = 3)
  .then((CoffeeResponse _res) {
if (_res.statusCode == 200) {
  ResourceModel resource = _res.decodedBody as ResourceModel;
  print(resource);
}
});

api["resources"]["read"]
  .execute(parameters: {"name": "toto"}).then((CoffeeResponse _res) {
if (_res.statusCode == 200) {
  ResourceModel resource = _res.decodedBody as ResourceModel;
  print(resource);
}
});
0
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Easy http request

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

convert, http

More

Packages that depend on coffee_http