8000 GitHub - IdanAizikNissim/Cosmic: More Fun http rest API work for Flutter
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

IdanAizikNissim/Cosmic

Repository files navigation

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.

About

More Fun http rest API work for Flutter

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages

0