requester 1.0.0

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

requester

Send package:http requests with middleware

example

import 'dart:async';
import 'package:requester/requester.dart';
import 'package:http/http.dart';

Future main() async {
  var client = new Client();
  var requester = new Requester(client);
  requester.addMiddleware(new LoggingMiddleware());
  var request = new HackernewsRequest("dart");
  await requester.send(request);
  print('done!');
}

class HackernewsRequest extends Request {
  HackernewsRequest(String query)
      : super("GET",
            Uri.parse("http://hn.algolia.com/api/v1/search?query=$query")) {
    this.headers['Content-Type'] = 'application/json';
  }
}

// response:
//   ╭--- cURL (http://hn.algolia.com/api/v1/search?query=dart)
//   curl -X GET -H "Content-Type: application/json" http://hn.algolia.com/api/v1/search?query=dart
//   ╰--- (copy and paste the above line to a terminal)
//   Response: 200
//   {"hits":[{"created_at":"2011-10-10T07:03:34.000Z" ...
//   done!

see the full example in the /example directory.

Changelog

0.0.1

  • Initial version, created by Stagehand

example/requester_example.dart

import 'dart:async';
import 'package:requester/requester.dart';
import 'package:http/http.dart';

Future main() async {
  var client = new Client();
  var requester = new Requester(client);
  requester.addMiddleware(new LoggingMiddleware());
  var request = new HackernewsRequest("dart");
  await requester.send(request);
  print('done!');
}

class HackernewsRequest extends Request {
  HackernewsRequest(String query)
      : super("GET",
            Uri.parse("http://hn.algolia.com/api/v1/search?query=$query")) {
    this.headers['Content-Type'] = 'application/json';
  }
}

class LoggingMiddleware extends BaseMiddleware {
  LoggingMiddleware();

  void prepare(BaseRequest request) {
    printCurl(request);
  }

  void handle(Response response) {
    print("Response: ${response.statusCode}");
    print(response.body);
  }

  void printCurl(BaseRequest request) {
    var curlCmd = "curl";
    curlCmd += " -X " + request.method;
    var compressed = false;
    request.headers.forEach((name, value) {
      if (name?.toLowerCase() == "accept-encoding" &&
          value?.toLowerCase() == "gzip") {
        compressed = true;
      }
      curlCmd += " -H \"$name: $value\"";
    });
    if (request.method == 'POST' || request.method == 'PUT') {
      if (request is Request) {
        var body = request.body;
        curlCmd += " --data '$body'";
      }
    }
    curlCmd += (compressed ? " --compressed " : " ") + request.url.toString();
    print("╭--- cURL (${request.url})");
    print(curlCmd);
    print("╰--- (copy and paste the above line to a terminal)");
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  requester: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:requester/requester.dart';
  
Version Uploaded Documentation Archive
1.0.0 Aug 15, 2018 Go to the documentation of requester 1.0.0 Download requester 1.0.0 archive
0.0.2 Jan 18, 2018 Go to the documentation of requester 0.0.2 Download requester 0.0.2 archive
0.0.1 Jan 8, 2018 Go to the documentation of requester 0.0.1 Download requester 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
72
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.11.8

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:requester/requester.dart.

Suggestions

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 <3.0.0
http ^0.11.3 0.11.3+17
Transitive dependencies
async 2.0.8
charcode 1.1.2
collection 1.14.11
http_parser 3.1.3
meta 1.1.6
path 1.6.2
source_span 1.4.1
string_scanner 1.0.4
typed_data 1.1.6