API Fetch Activity format=...

.. when retreiving an activity using the API
   you can now specify the format you want the
   data to be returned in.

   it can be one of;
   tcx - garmin training centre xml
   pwx - training peaks xml
   json - goldencheetah json
   csv - all available data (not powertap csv)

.. along the way the file writers for the respective
   formats now accept a NULL context to work standalone.
   this may be useful as a file conversion tool.
This commit is contained in:
Mark Liversedge
2015-09-08 21:13:17 +01:00
parent 1d1508c524
commit d95ec38593
4 changed files with 258 additions and 164 deletions

View File

@@ -22,6 +22,11 @@
#include "GcUpgrade.h"
#include "RideDB.h"
#include "RideFile.h"
#include "CsvRideFile.h"
#include <QTemporaryFile>
void
APIWebService::service(HttpRequest &request, HttpResponse &response)
{
@@ -170,15 +175,98 @@ APIWebService::listActivity(QString athlete, QStringList paths, HttpRequest &req
QFile file(filename);
if (file.exists() && file.open(QFile::ReadOnly | QFile::Text)) {
// read in the whole thing
QTextStream in(&file);
// GC .JSON is stored in UTF-8 with BOM(Byte order mark) for identification
in.setCodec ("UTF-8");
contents = in.readAll();
// close as we will open properly below
file.close();
// write back in one hit
response.write(contents.toLocal8Bit(), true);
// what format to use ?
QString format(request.getParameter("format"));
if (format == "") {
// read in the whole thing
QTextStream in(&file);
// GC .JSON is stored in UTF-8 with BOM(Byte order mark) for identification
in.setCodec ("UTF-8");
contents = in.readAll();
file.close();
// write back in one hit
response.write(contents.toLocal8Bit(), true);
} else {
// lets go with tcx/pwx as xml, full csv (not powertap) and GC json
QStringList formats;
formats << "tcx"; // garmin training centre
formats << "csv"; // full csv list (not powertap)
formats << "json"; // gc json
formats << "pwx"; // gc json
// unsupported format
if (!formats.contains(format)) {
response.setStatus(500);
response.write("unsupported format; we support:");
foreach(QString fmt, formats) {
response.write(" ");
response.write(fmt.toLocal8Bit());
}
response.write("\r\n");
return;
}
// lets read the file in as a ridefile
QStringList errors;
RideFile *f = RideFileFactory::instance().openRideFile(NULL, file, errors);
// error reading (!)
if (f == NULL) {
response.setStatus(500);
foreach(QString error, errors) {
response.write(error.toLocal8Bit());
response.write("\r\n");
}
return;
}
// write out to a temporary file in
// the format requested
bool success;
QTemporaryFile tempfile; // deletes file when goes out of scope
QString tempname;
if (tempfile.open()) tempname = tempfile.fileName();
else {
response.setStatus(500);
response.write("error opening temporary file");
return;
}
QFile out(tempname);
if (format == "csv") {
CsvFileReader writer;
success = writer.writeRideFile(NULL, f, out, CsvFileReader::gc);
} else {
success = RideFileFactory::instance().writeRideFile(NULL, f, out, format);
}
if (success) {
// read in the whole thing
out.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&out);
in.setCodec ("UTF-8");
contents = in.readAll();
out.close();
// write back in one hit
response.write(contents.toLocal8Bit(), true);
return;
} else {
response.setStatus(500);
response.write("unable to write output, internal error.\n");
return;
}
}
} else {