cookie_jar 0.0.6

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

CookieJar

build statud Pub coverage support

A cookie manager for http requests in Dart, by which you can deal with the complex cookie policy and persist cookies easily.

Add dependency

dependencies:
  dio: ^0.0.2

Usage

A simple usage example:

import 'package:cookie_jar/cookie_jar.dart';
void main() async {
  List<Cookie> cookies = [new Cookie("name", "wendux"),new Cookie("location", "china")];
  var cj = new CookieJar();
  //Save cookies   
  cj.saveFromResponse(Uri.parse("https://www.baidu.com/"), cookies);
  //Get cookies  
  List<Cookie> results = cj.loadForRequest(Uri.parse("https://www.baidu.com/xx"));
  print(results);  
}    
       

Classes

SerializableCookie

This class is a wrapper for Cookie class. Because the Cookie class doesn't support Json serialization, for the sake of persistence, we use this class instead of it.

CookieJar

CookieJar is a default cookie manager which implements the standard cookie policy declared in RFC. CookieJar saves the cookies in RAM, so if the application exit, all cookies will be cleared. A example as follow:

var cj= new CookieJar();

PersistCookieJar

PersistCookieJar is a cookie manager which implements the standard cookie policy declared in RFC. PersistCookieJar persists the cookies in files, so if the application exit, the cookies always exist unless call delete explicitly. A example as follows:

// Cookie files will be saved in "./cookies"
var cj=new PersistCookieJar("./cookies");

Note: In Flutter, File system is different from PC, you can use path_provider package to get the path :

// API `getTemporaryDirectory` is from "path_provider" package.
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
CookieJar cj=new PersistCookieJar(tempPath);

APIs

void saveFromResponse(Uri uri, List<Cookie> cookies);

Save the cookies for specified uri.

List<Cookie> loadForRequest(Uri uri);

Load the cookies for specified uri.

delete(Uri uri,[bool withDomainSharedCookie = false] )

Delete cookies for specified uri. This API will delete all cookies for the uri.host, it will ignored the uri.path.

If withDomainSharedCookie is true , will delete the domain-shared cookies.

Note: This API is only available in PersistCookieJar class.

Working with HttpClient

Using CookieJar or PersistCookieJar manages HttpClient 's request/response cookies is very easy:

var cj=new CookieJar();
...
request= await httpClient.openUrl(options.method, uri);
request.cookies.addAll(cj.loadForRequest(uri));
response= await request.close();
cj.saveFromResponse(uri, response.cookies);

Working with dio

dio is a powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, File downloading, Timeout etc. And dio supports to manage cookies with cookie_jar, the simple example is:

var dio = new Dio();
dio.cookieJar=new PersistCookieJar("./cookies");
Response<String>  response = await dio.get("https://www.baidu.com");

More details about dio see : https://github.com/flutterchina/dio .

This open source project authorized by https://flutterchina.club , and the license is MIT.

Features and bugs

Please file feature requests and bugs at the issue tracker.

0.0.1

  • Initial version, created by Stagehand

example/cookie_jar_example.dart

import 'dart:async';
import 'dart:io';

import 'package:cookie_jar/cookie_jar.dart';

void main() async {
  final List<Cookie> cookies = <Cookie>[
    new Cookie('name', 'wendux'),
    new Cookie('location', 'china'),
  ];
  final List<Cookie> cookiesExpired = <Cookie>[
    new Cookie('name', 'wendux')..maxAge = 1,
    new Cookie('location', 'china')..expires = new DateTime.now().add(const Duration(hours: 1)),
  ];
  final Directory dir = new Directory('./example/.cookies');
  await dir.create(recursive: true);
  final CookieJar cj = new CookieJar();
  //var cj=new PersistCookieJar('./example/.cookies');
  cj.saveFromResponse(Uri.parse('https://www.baidu.com/xx'), cookies);
  List<Cookie> results = cj.loadForRequest(Uri.parse('https://www.baidu.com/xx'));
  assert(results.length == 2);
  results = cj.loadForRequest(Uri.parse('https://www.baidu.com/xx/dd'));
  assert(results.length == 2);
  results = cj.loadForRequest(Uri.parse('https://www.baidu.com/'));
  assert(results.isEmpty);
  cj.saveFromResponse(Uri.parse('https://google.com'), cookiesExpired);
  results = cj.loadForRequest(Uri.parse('https://google.com'));
  assert(results.length == 2);
  await new Future<void>.delayed(const Duration(seconds: 2), () {
    results = cj.loadForRequest(Uri.parse('https://google.com'));
    assert(results.length == 1);
  });
}

Use this package as a library

1. Depend on it

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


dependencies:
  cookie_jar: ^0.0.6

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:cookie_jar/cookie_jar.dart';
  
Version Uploaded Documentation Archive
0.0.6 Aug 20, 2018 Go to the documentation of cookie_jar 0.0.6 Download cookie_jar 0.0.6 archive
0.0.5 Aug 20, 2018 Go to the documentation of cookie_jar 0.0.5 Download cookie_jar 0.0.5 archive
0.0.4 May 23, 2018 Go to the documentation of cookie_jar 0.0.4 Download cookie_jar 0.0.4 archive
0.0.3 Apr 27, 2018 Go to the documentation of cookie_jar 0.0.3 Download cookie_jar 0.0.3 archive
0.0.2 Apr 26, 2018 Go to the documentation of cookie_jar 0.0.2 Download cookie_jar 0.0.2 archive
0.0.1 Apr 26, 2018 Go to the documentation of cookie_jar 0.0.1 Download cookie_jar 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.12.5

Platforms

Detected platforms: Flutter, other

Primary library: package:cookie_jar/cookie_jar.dart with components: io.

Health suggestions

Format lib/src/default_cookie_jar.dart.

Run dartfmt to format lib/src/default_cookie_jar.dart.

Format lib/src/persist_cookie_jar.dart.

Run dartfmt to format lib/src/persist_cookie_jar.dart.

Format lib/src/serializable_cookie.dart.

Run dartfmt to format lib/src/serializable_cookie.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While there is nothing inherently wrong with versions of 0.0.*, it usually means that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <4.0.0
Dev dependencies
coverage ^0.9.3
dart_codecov_generator
test ^0.12.0