carp_webservices 0.5.0 carp_webservices: ^0.5.0 copied to clipboard
Flutter API for accessing the CARP web services - authentication, file management, data points, and app-specific collections of documents.
CARP Web Service Plugin for Flutter #
A Flutter plugin to access the CARP Web Service API.
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 #
-
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 test, staging, and production servers.
-
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 and Deployment ID is needed.
final String uri = "http://staging.carp.cachet.dk:8080";
final String testDeploymentId = "d246170c-515e";
final String testStudyId = "64c1784d-52d1-4c3d";
CarpApp app;
Study study;
study = new Study(testStudyId, "user@dtu.dk", deploymentId: testDeploymentId, name: "Test study");
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 #
Basic authentication is using username and password.
CarpUser user;
try {
user = await CarpService.instance.authenticate(username: "a_username", password: "the_password");
} catch (excp) {
...;
}
Since the CarpUser can be serialized to JSON, the OAuth token can be stored on the phone. This can then later be used for authentication:
try {
user = await CarpService.instance.authenticateWithToken(username: user.username, token: user.token);
} catch (excp) {
...;
}
Informed Consent Document #
A ConsentDocument can be uploaded and downloaded from CARP.
try {
ConsentDocument uploaded = await CarpService.instance.createConsentDocument({"text": "The original terms text.", "signature": "Image Blob"});
...
ConsentDocument downloaded = await CarpService.instance.getConsentDocument(uploaded.id);
} 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 Documents #
A CollectionReference
is used to manage collections
and documents
on a CARP web service and have methods for:
- creating, updating, and deleting documents
- accessing documents in collections
// access an document
// - if the document id is not specified, a new document (with a new id) is created
// - if the collection (users) don't exist, it is created
DocumentSnapshot document =
await CarpService.instance.collection('users').document().setData({'email': username, 'name': 'Administrator'});
// update the document
DocumentSnapshot updated_document = await CarpService.instance
.collection('/users')
.document(document.name)
.updateData({'email': username, 'name': 'Super User'});
// get the document by its path in collection(s).
DocumentSnapshot new_document = await CarpService.instance.collection('users').document(document.name).get();
// get the document by its unique ID
new_document = await CarpService.instance.documentById(document.id).get();
// delete the document
await CarpService.instance.collection('users').document(document.name).delete();
// get all collections from a document
List<String> collections = new_document.collections;
// get all documents in a collection.
List<DocumentSnapshot> documents = await CarpService.instance.collection("users").documents;
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();
Deployments #
A core notion of CARP is the Deployment subsystem.
This subsystem is used for accessing deployment
configurations, i.e. configurations that describe how
data sampling in a study should take place.
The CARP web service have methods for:
- getting a list of invitation for a specific
accountId
, i.e. a user - default is the user who is authenticated to the CARP Service. - getting a deployment reference, which then can be used to query status, register devices, and get the deployment specification.
// get invitations for this account (user)
List<ActiveParticipationInvitation> invitations = await CarpService.instance.invitations();
// get a deployment reference for this master device
DeploymentReference deploymentReference = CarpService.instance.deployment(masterDeviceRoleName: 'Master');
// get the status of this deployment
StudyDeploymentStatus status = await deploymentReference.status();
// register a device
status = await deploymentReference.registerDevice(deviceRoleName: 'phone');
// get the master device deployment
MasterDeviceDeployment deployment = await deploymentReference.get();
// mark the deployment as a success
status = await deploymentReference.success();
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.