Coder Social home page Coder Social logo

matanlurey / seltzer Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 1.0 64 KB

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

Home Page: https://pub.dartlang.org/packages/seltzer

License: BSD 3-Clause "New" or "Revised" License

Dart 97.28% Shell 2.72%
seltzer elegant dart flutter http stream web web-socket

seltzer's Introduction

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);
}

seltzer's People

Contributors

kharland avatar matanlurey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

ronlobo

seltzer's Issues

Seltzer WS tests fail on Chrome

test/runtime/socket/browser_test.dart: SeltzerWebSocket should throw a StateError if data is sent after the socket closes
  InvalidAccessError: Failed to execute 'close' on 'WebSocket': The code must be either 1000, or between 3000 and 4999. 0 is neither.
  dart:html                                   EventTarget.close
  package:seltzer/platform/browser.dart 81:5  <fn>
  ===== asynchronous gap ===========================
  dart:_internal                              Object._asyncHelper
  package:seltzer/platform/browser.dart 81:5  <fn>
  ===== asynchronous gap ===========================
  dart:_internal                              Object._wrapJsFunctionForAsync
  package:seltzer/platform/browser.dart 79:3  BrowserSeltzerWebSocket.close
  package:seltzer/platform/browser.dart 82:3  BrowserSeltzerWebSocket.close
  test/common/ws.dart 39:13                   <fn>
  ===== asynchronous gap ===========================
  dart:_internal                              Object._wrapJsFunctionForAsync
  test/common/ws.dart 38:9                    runSocketTests.<fn>.<fn>

Add record/replay mode to simplify testing

Strawman:

import 'dart:io';

import 'package:seltzer/platform/server.dart';
import 'package:seltzer/platform/testing.dart';

main() {
  var recorder = new SeltzerHttpRecorder(const ServerSeltzerHttp());
  useSeltzerForTesting(http: recorder);
  await runTests();
  await recorder.dump('e2e_recording.json');
}

Usage:

main() async {
  useSeltzerForTesting(http: await SeltzerHttpPlayback.load('e2e_recording.json'));
  await runTests();
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.