post method

Future<int> post(
  1. DataPoint data
)

Uploads data.

Returns the server-generated ID for this data point.

Implementation

Future<int> post(DataPoint data) async {
  final String url = dataEndpointUri;

  // POST the data point to the CARP web service
  http.Response response = await httpr.post(Uri.encodeFull(url),
      headers: headers, body: json.encode(data));

  int httpStatusCode = response.statusCode;
  Map<String, dynamic> responseJson =
      json.decode(response.body) as Map<String, dynamic>;

  if ((httpStatusCode == HttpStatus.ok) ||
      (httpStatusCode == HttpStatus.created)) {
    return responseJson["id"] as int;
  }

  // All other cases are treated as an error.
  throw CarpServiceException(
    httpStatus: HTTPStatus(httpStatusCode, response.reasonPhrase),
    message: responseJson["message"].toString(),
    path: responseJson["path"].toString(),
  );
}