cosmic 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 0

Cosmic #

Simple work with REST JSON API for DART

Usage #

A simple usage example:

import 'package:http/http.dart' as http;
import 'package:cosmic/converters/cosmic_converters.dart';
import 'package:cosmic/annotations/cosmic_annotations.dart';
import 'package:cosmic/cosmic.dart' as Cosmic;   
      
class Fixer {
  String base;
  String date;
  Map<String, double> rates;
}

part 'fixer.g.dart';
      
@Client(
  "http://api.fixer.io", 
  converter: const JsonConverter()
)
class FixerClient extends Cosmic.Client with _$FixerClient {
  @Get("/latest")
  Future<Fixer> latest({@Query("base") String base = "USD"});
}   
      
main() async {
  Fixer latest = await fixerClient.latest();
  print(latest.date);
}

A middleware:

FixerClient client = new FixerClient()
  ..use((request, next) {
    print("${request.httpMethod} ${request.url}");
    request.headers["token"] = "12345";
    next();
  });

A converter:

import 'package:jsonx/jsonx.dart' as jsonx;
    
class JsonConverter extends Converter {
  const JsonConverter();
    
  @override
  dynamic decode(String data, {Type type}) => jsonx.decode(data, type: type);
    
  @override
  String encode(object) => jsonx.encode(object);
}

(check example/placeholder_client.dart for more)

Generate #

dart:mirror is absent from flutter
use cosmic:generate in order to generate the api impl pre runtime

$ pub run cosmic:generate -i example/clients/def/placeholder.dart -c PlaceholderClient -o example/clients/impl -w true

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

1.1.0 #

  • Change $name.g client to be part of the client clients must include part '$name.g.dart' and with _$NameClient (after gen)

0.0.3 #

  • Request's middlewares

0.0.2 #

  • Separate encode/decode to converters

0.0.1 #

  • Initial version, created by Stagehand

example/cosmic_example.dart

import 'clients/placeholder.dart';
import 'package:cosmic/cosmic.dart';
import 'entities/post_entity.dart';

main() async {
  PlaceholderClient client = new PlaceholderClient()
    ..use((request, next) {
      print("${request.httpMethod} ${request.url}");
      request.headers["hello"] = "world";
      next();
    });

  client = Cosmic.create(client);

  // Get post with id: 1
  Map<String, String> headers = {
    "auth": "13325353453"
  };
  var post_1 = await client.getPost(id: 1, headers: headers);
  print("${post_1.id} - ${post_1.title}");

  // Get all posts
  var posts = await client.getPosts();
  posts.forEach((post) => print("${post.id} - ${post.title}"));

  // Create a new post
  PostEntity post = new PostEntity()
    ..title = "Hello From Dart Cosmic"
    ..body = "foo"
    ..userId = 1;

  var postResponse = await client.create(post: post);
  post.id = postResponse.id;
  print("${post.id} - ${post.title}");

  // Update post
  post.id = 1;
  post.body = "Hakuna matata!";
  post = await client.update(id: post.id, post: post);
  print("${post.id} - ${post.body}");

  // Delete
  var resp = await client.delete(id: 1);
  print(resp.body);
}

Use this package as a library

1. Depend on it

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


dependencies:
  cosmic: ^1.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:cosmic/cosmic.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.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <2.0.0