cosmic 1.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 69

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';
  
Version Uploaded Documentation Archive
1.1.1 Apr 10, 2018 Go to the documentation of cosmic 1.1.1 Download cosmic 1.1.1 archive
1.1.0 Apr 10, 2018 Go to the documentation of cosmic 1.1.0 Download cosmic 1.1.0 archive
1.0.1 Apr 4, 2018 Go to the documentation of cosmic 1.0.1 Download cosmic 1.0.1 archive
0.0.5 Apr 3, 2018 Go to the documentation of cosmic 0.0.5 Download cosmic 0.0.5 archive
0.0.4 Apr 3, 2018 Go to the documentation of cosmic 0.0.4 Download cosmic 0.0.4 archive

Analysis

We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
41 / 100
Health:
Code health derived from static analysis. [more]
96 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
69
Learn more about scoring.

Platforms

Detected platforms: other

Primary library: package:cosmic/cosmic.dart with components: io, isolate, build, mirrors.

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 26 hints.

    Run dartfmt to format lib/annotations/client.dart.

    Run dartfmt to format lib/annotations/cosmic_annotations.dart.

    Similar analysis of the following files failed:

    • lib/annotations/data.dart (hint)
    • lib/annotations/http_method.dart (hint)
    • lib/annotations/http_methods.dart (hint)
    • lib/converters/cosmic_converters.dart (hint)
    • lib/converters/json_converter.dart (hint)
    • lib/cosmic.dart (hint)
    • lib/cosmic_lib.dart (hint)
    • lib/generateor.dart (hint)
    • lib/src/builder.dart (hint)
    • lib/src/builder_generator.dart (hint)
    • lib/src/client.dart (hint)
    • lib/src/converter.dart (hint)
    • lib/src/gen.dart (hint)
    • lib/src/http_provider.dart (hint)
    • lib/src/middleware.dart (hint)
    • lib/src/parser.dart (hint)
    • lib/src/type_provider.dart (hint)
    • lib/src/utils.dart (hint)
  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <2.0.0
analyzer ^0.31.1 0.31.1 0.32.1
args ^1.4.1 1.4.3
build ^0.12.0+2 0.12.6
dart_style ^1.0.7 1.0.9+1 1.1.0
http ^0.11.3+14 0.11.3+16
jsonx ^2.0.2 2.0.2
meta ^1.1.2 1.1.5
path ^1.4.2 1.6.1
source_gen ^0.7.4+3 0.7.6 0.8.2
Transitive dependencies
async 2.0.7
charcode 1.1.1
cli_util 0.1.3
collection 1.14.10
convert 2.0.1
crypto 2.0.5
csslib 0.14.4
front_end 0.1.0-alpha.9 0.1.1
glob 1.1.5
html 0.13.3+1
http_parser 3.1.2
isolate 1.1.0 2.0.1
kernel 0.3.0-alpha.9 0.3.1
logging 0.11.3+1
package_config 1.0.3
plugin 0.2.0+2
source_span 1.4.0
string_scanner 1.0.2
typed_data 1.1.5
utf 0.9.0+4
watcher 0.9.7+8
yaml 2.1.14
Dev dependencies
build_runner any
build_test any