carp_webservices 0.1.3

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

CARP Web Service Plugin for Flutter

A Flutter plugin to access the CARP Web Service API.

pub package

For Flutter plugins for other CARP products, see CARP Mobile Sensing in Flutter.

Note: This plugin is still under development, and some APIs might not be available yet. Feedback and Pull Requests are most welcome!

Setup

  1. You need a CARP Web Service host running. See the CARP Web Service API documentation for how to do this. If you're part of the CACHET team, you can use the specified development, staging, and production servers.

  2. Add carp_services as a dependency in your pubspec.yaml file.

Usage

import 'package:carp_webservices/carp_service/carp_service.dart';

Configuration

The CarpService is a singleton and needs to be configured once. Note that a valid Study with a valid study ID is needed.

final String uri = "http://staging.carp.cachet.dk:8080";

CarpApp app;
Study study;

study = new Study(testStudyId, "user@dtu.dk", name: "Test study #$testStudyId");
app = new CarpApp(
      study: study,
      name: "any_display_friendly_name_is_fine",
      uri: Uri.parse(uri),
      oauth: OAuthEndPoint(clientID: "the_client_id", clientSecret: "the_client_secret"));

CarpService.configure(app);

The singleton can then be accessed via CarpService.instance.

Authentication

Authentication is currently only supported using username and password.

CarpUser user;
try {
   user = await CarpService.instance.authenticate(username: "a_username", password: "the_password");
} catch (excp) {
   ...;
}

Data Points

A DataPointReference is used to manage data points on a CARP web service and have CRUD methods for:

  • post a data point
  • batch upload multiple data points
  • get a data point
  • delete data points
// Create a test location datum
LocationDatum datum = LocationDatum.fromMap(<String, dynamic>{
  "latitude": 23454.345,
  "longitude": 23.4,
  "altitude": 43.3,
  "accuracy": 12.4,
  "speed": 2.3,
  "speedAccuracy": 12.3
});

// create a CARP data point
final CARPDataPoint data = CARPDataPoint.fromDatum(study.id, study.userId, datum);
// post it to the CARP server, which returns the ID of the data point
data_point_id = await CarpService.instance.getDataPointReference().postDataPoint(data);

// get the data point back from the server
CARPDataPoint data = await CarpService.instance.getDataPointReference().getDataPoint(data_point_id);

// batch upload a list of raw json data points in a file
final File file = File("test/batch.json");
await CarpService.instance.getDataPointReference().batchPostDataPoint(file);

// delete the data point
await CarpService.instance.getDataPointReference().deleteDataPoint(data_point_id);

Application-specific Collections and Objects

A CollectionReference is used to manage collections on a CARP web service and have methods for:

  • creating, updating, and deleting objects
  • accessing objects in collections
// access an object 
//  - if the object id is not specified, a new object (with a new id) is created
//  - if the collection (users) don't exist, it is created
ObjectSnapshot object = await CarpService.instance
    .collection('/users')
    .object()
    .setData({'email': username, 'name': 'Administrator'});

// update the object
ObjectSnapshot updated_object = await CarpService.instance
    .collection('/users')
    .object(object.id)
    .updateData({'email': username, 'name': 'Super User'});

// get the object
ObjectSnapshot new_object = await CarpService.instance.collection('/users').object(object.id).get();

// delete the object
await CarpService.instance.collection('/users').object(object.id).delete();

// get all collections in the root
List<String> root = await CarpService.instance.collection("").collections;

// get all objects in a collection.
List<ObjectSnapshot> objects = await CarpService.instance.collection("/users").objects;

File Management

A FileStorageReference is used to manage files on a CARP web service and have methods for:

  • uploading a file
  • downloading a file
  • getting a file object
  • getting all file objects
  • deleting a file

When uploading a file, you can add metadata as a Map<String, String>.

// first upload a file
final File myFile = File("test/img.jpg");
final FileUploadTask uploadTask = CarpService.instance
   .getFileStorageReference()
   .upload(myFile, {'content-type': 'image/jpg', 'content-language': 'en', 'activity': 'test'});
CarpFileResponse response = await uploadTask.onComplete;
int id = response.id;

// then get its description back from the server
final CarpFileResponse result = await CarpService.instance.getFileStorageReference(id).get();

// then download the file again
// note that a local file to download is needed
final File myFile = File("test/img-$id.jpg");
final FileDownloadTask downloadTask = CarpService.instance.getFileStorageReference(id).download(myFile);
int response = await downloadTask.onComplete;

// now get references to ALL files in this study
final List<CarpFileResponse> results = await CarpService.instance.getFileStorageReference(id).getAll();

// finally, delete the file
final int result = await CarpService.instance.getFileStorageReference(id).delete();

Features and bugs

Please file feature requests and bug reports at the issue tracker.

License

This software is copyright (c) 2018 Copenhagen Center for Health Technology (CACHET) at the Technical University of Denmark (DTU). This software is made available 'as-is' in a MIT license.

0.1.3

  • Adjustment to refactoring of carp_mobile_sensing library to version 0.3.0

0.1.2

  • Small refactor and removal of debug print.
  • Adding an example
  • Added support for batch upload of data points as a file.

0.1.1

  • Documentation update.

0.1.0

  • Initial release.

example/example.dart

import 'package:carp_webservices/carp_auth/carp_auth.dart';
import 'package:carp_webservices/carp_service/carp_service.dart';
import 'package:carp_mobile_sensing/carp_mobile_sensing.dart';
import 'package:carp_mobile_sensing/core/core.dart';
import 'dart:io';

void main() async {
  final String username = "researcher";
  final String password = "password";
  final String uri = "http://staging.carp.cachet.dk:8080";
  final String clientID = "carp";
  final String clientSecret = "carp";
  final String testStudyId = "8";

  CarpApp app;
  Study study;

  study = new Study(testStudyId, "user@dtu.dk", name: "Test study #$testStudyId");
  app = new CarpApp(
      study: study,
      name: "any_display_friendly_name_is_fine",
      uri: Uri.parse(uri),
      oauth: OAuthEndPoint(clientID: "the_client_id", clientSecret: "the_client_secret"));

  // Configure the CARP Service with this app.
  CarpService.configure(app);

  // ------------------- AUTHENTICATION --------------------------------

  // Try to authenticate
  CarpUser user;
  try {
    user = await CarpService.instance.authenticate(username: "a_username", password: "the_password");
  } catch (excp) {
    print(excp);
  }

  // ------------------- FILE MANAGEMENT --------------------------------

  // first upload a file
  final File uploadFile = File("test/img.jpg");
  final FileUploadTask uploadTask = CarpService.instance
      .getFileStorageReference()
      .upload(uploadFile, {'content-type': 'image/jpg', 'content-language': 'en', 'activity': 'test'});
  CarpFileResponse response = await uploadTask.onComplete;
  int id = response.id;

  // then get its description back from the server
  final CarpFileResponse result = await CarpService.instance.getFileStorageReference(id).get();

  // then download the file again
  // note that a local file to download is needed
  final File downloadFile = File("test/img-$id.jpg");
  final FileDownloadTask downloadTask = CarpService.instance.getFileStorageReference(id).download(downloadFile);
  int responseCode = await downloadTask.onComplete;

  // now get references to ALL files in this study
  final List<CarpFileResponse> results = await CarpService.instance.getFileStorageReference(id).getAll();

  // finally, delete the file
  responseCode = await CarpService.instance.getFileStorageReference(id).delete();

  // ------------------- DATA POINTS --------------------------------

  // Create a test location datum
  LocationDatum datum = LocationDatum.fromMap(<String, dynamic>{
    "latitude": 23454.345,
    "longitude": 23.4,
    "altitude": 43.3,
    "accuracy": 12.4,
    "speed": 2.3,
    "speedAccuracy": 12.3
  });

  // create a CARP data point
  final CARPDataPoint data = CARPDataPoint.fromDatum(study.id, study.userId, datum);
  // post it to the CARP server, which returns the ID of the data point
  String data_point_id = await CarpService.instance.getDataPointReference().postDataPoint(data);

  // get the data point back from the server
  CARPDataPoint data_point = await CarpService.instance.getDataPointReference().getDataPoint(data_point_id);

  // batch upload a list of raw json data points in a file
  final File file = File("test/batch.json");
  await CarpService.instance.getDataPointReference().batchPostDataPoint(file);

  // delete the data point
  await CarpService.instance.getDataPointReference().deleteDataPoint(data_point_id);

  // ------------------- COLLECTIONS AND OBJECTS --------------------------------

  // access an object
  //  - if the object id is not specified, a new object (with a new id) is created
  //  - if the collection (users) don't exist, it is created
  ObjectSnapshot object =
      await CarpService.instance.collection('/users').object().setData({'email': username, 'name': 'Administrator'});

  // update the object
  ObjectSnapshot updated_object = await CarpService.instance
      .collection('/users')
      .object(object.id)
      .updateData({'email': username, 'name': 'Super User'});

  // get the object
  ObjectSnapshot new_object = await CarpService.instance.collection('/users').object(object.id).get();

  // delete the object
  await CarpService.instance.collection('/users').object(object.id).delete();

  // get all collections in the root
  List<String> root = await CarpService.instance.collection("").collections;

  // get all objects in a collection.
  List<ObjectSnapshot> objects = await CarpService.instance.collection("/users").objects;
}

Use this package as a library

1. Depend on it

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


dependencies:
  carp_webservices: ^0.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support 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:carp_webservices/carp_auth/carp_auth.dart';
import 'package:carp_webservices/carp_auth/carp_user.dart';
import 'package:carp_webservices/carp_auth/oauth.dart';
import 'package:carp_webservices/carp_service/carp_app.dart';
import 'package:carp_webservices/carp_service/carp_service.dart';
import 'package:carp_webservices/carp_service/carp_tasks.dart';
import 'package:carp_webservices/carp_service/collection_reference.dart';
import 'package:carp_webservices/carp_service/datapoint_reference.dart';
import 'package:carp_webservices/carp_service/file_reference.dart';
  
Version Uploaded Documentation Archive
0.1.3 Jan 21, 2019 Go to the documentation of carp_webservices 0.1.3 Download carp_webservices 0.1.3 archive
0.1.2 Dec 17, 2018 Go to the documentation of carp_webservices 0.1.2 Download carp_webservices 0.1.2 archive
0.1.1 Dec 11, 2018 Go to the documentation of carp_webservices 0.1.1 Download carp_webservices 0.1.1 archive
0.1.0 Dec 10, 2018 Go to the documentation of carp_webservices 0.1.0 Download carp_webservices 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
24
Health:
Code health derived from static analysis. [more]
87
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
58
Learn more about scoring.

We analyzed this package on Jan 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.10
  • Flutter: 1.1.7

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/carp_service/collection_reference.dart. (-3.93 points)

Analysis of lib/carp_service/collection_reference.dart reported 8 hints, including:

line 39 col 11: Name non-constant identifiers using lowerCamelCase.

line 50 col 25: Name non-constant identifiers using lowerCamelCase.

line 69 col 11: Name non-constant identifiers using lowerCamelCase.

line 78 col 25: Name non-constant identifiers using lowerCamelCase.

line 154 col 13: Name non-constant identifiers using lowerCamelCase.

Fix lib/carp_service/datapoint_reference.dart. (-3.93 points)

Analysis of lib/carp_service/datapoint_reference.dart reported 8 hints, including:

line 23 col 25: Avoid using braces in interpolation when not needed.

line 24 col 11: Name non-constant identifiers using lowerCamelCase.

line 57 col 25: Avoid using braces in interpolation when not needed.

line 58 col 11: Name non-constant identifiers using lowerCamelCase.

line 94 col 19: Avoid using braces in interpolation when not needed.

Fix lib/carp_service/file_reference.dart. (-3.45 points)

Analysis of lib/carp_service/file_reference.dart reported 7 hints, including:

line 47 col 25: Avoid using braces in interpolation when not needed.

line 48 col 11: Name non-constant identifiers using lowerCamelCase.

line 72 col 25: Avoid using braces in interpolation when not needed.

line 73 col 11: Name non-constant identifiers using lowerCamelCase.

line 82 col 34: Name non-constant identifiers using lowerCamelCase.

Fix additional 5 files with analysis or formatting issues. (-1.99 points)

Additional issues in the following files:

  • lib/carp_service/carp_tasks.dart (3 hints)
  • lib/carp_service/carp_app.dart (1 hint)
  • lib/carp_auth/carp_user.dart (Run flutter format to format lib/carp_auth/carp_user.dart.)
  • lib/carp_auth/oauth.dart (Run flutter format to format lib/carp_auth/oauth.dart.)
  • lib/carp_service/carp_service.dart (Run flutter format to format lib/carp_service/carp_service.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
carp_mobile_sensing ^0.3.0 0.3.0
flutter 0.0.0
Transitive dependencies
activity_recognition 0.1.3
app_usage 0.0.2
archive 2.0.7
args 1.5.1
async 2.0.8
battery 0.2.3
call_log 1.0.5
charcode 1.1.2
collection 1.14.11
connectivity 0.3.2
convert 2.1.1
crypto 2.0.6
device_apps 1.0.4
device_info 0.2.1 0.3.0
file_utils 0.1.1
fixnum 0.10.9
flutter_blue 0.4.2
flutter_sound 1.2.2
globbing 0.2.0
http 0.12.0+1
http_parser 3.1.3
json_annotation 1.2.0 2.0.0
light 0.0.4
location 1.4.1
meta 1.1.6 1.1.7
noise_meter 0.0.3
path 1.6.2
path_provider 0.4.1
pedometer 0.0.5
protobuf 0.10.8 0.12.0
screen_state 0.0.1
sensors 0.3.5
simple_permissions 0.1.9
sky_engine 0.0.99
sms 0.2.4
source_span 1.5.2
stats 0.2.0+2
string_scanner 1.0.4
system_info 0.1.0
term_glyph 1.1.0
typed_data 1.1.6
uuid 1.0.3 2.0.0-rc1
vector_math 2.0.8
weather 0.1.03
Dev dependencies
flutter_test