Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
/ seltzer Public archive

An elegant and rich cross-platform HTTP library for Dart.

License

Notifications You must be signed in to change notification settings

matanlurey/seltzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Seltzer

pub package Build Status

An elegant and rich cross-platform HTTP library for Dart.

Getting Started

You can use Seltzer as an object-oriented HTTP service or simply use top-level convenience methods like get and post directly.

Using HTTP the service

If you are using Seltzer with dependency injection:

import 'dart:async';

import 'package:seltzer/seltzer.dart';
import 'package:seltzer/platform/vm.dart';

void main() {
  new MyTwitterService(const VmSeltzerHttp()).tweet('Hello World!');
}

class MyTwitterService {
  final SeltzerHttp _http;
  
  MyTwitterService(this._http);
  
  // Uses the SeltzerHttp service to send a tweet.
  //
  // This means if we are in the browser or the VM we can expect
  // our http service to work about the same.
  Future<Null> tweet(String message) => ...
}

Using top-level methods

For simpler applications or scripts, Seltzer also provides a series of top-level convenience methods that automatically use a singleton instance of SeltzerHttp.

In your main() function, you just need to configure what platform you are expecting once:

import 'package:seltzer/seltzer.dart' as seltzer;
import 'package:seltzer/platform/browser.dart';

main() async {
  useSeltzerInTheBrowser();
  final response = await seltzer.get('some/url.json').send().first;
  print('Retrieved: ${await response.readAsString()}');
}

Using the WebSocket service

import 'dart:async';

import 'package:seltzer/seltzer.dart';
import 'package:seltzer/platform/vm.dart';

void main() {
  var service = new MyMessageService(connect('ws://127.0.0.1'));
  service.sendMessage('Hello World!');
}

class MyMessageService {
  final SeltzerWebSocket _webSocket;
   
  MyMessageService(this._webSocket);
  
  // Uses a SeltzerWebSocket to send a string message to a peer.
  //
  // This means if we are in the browser or the server we can expect
  // our WebSocket to work about the same.
  Future<Null> sendMessage(String message) => _webSocket.sendString(message);
}