From 1d1508c5247fe75fe3c4fa9c1ca5cc2f2e4debab Mon Sep 17 00:00:00 2001 From: Mark Liversedge Date: Tue, 8 Sep 2015 20:17:56 +0100 Subject: [PATCH] API return activity .. just straight up for now, so; http://localhost:12021/athlete/activity/2015_08_09_10_59_09.json will just return the ride unchanged in the format it is stored in. Some parameters will now be added to enable the format to be selected, from those that we can support. --- src/APIWebService.cpp | 51 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/src/APIWebService.cpp b/src/APIWebService.cpp index 9aa3daf74..41ddb48c9 100644 --- a/src/APIWebService.cpp +++ b/src/APIWebService.cpp @@ -57,18 +57,35 @@ APIWebService::athleteData(QStringList &paths, HttpRequest &request, HttpRespons { // LIST ACTIVITIES FOR ATHLETE - if (paths.count() == 2) listRides(paths[0], request, response); - else if (paths.count() > 2) { + if (paths.count() == 2) { + + listRides(paths[0], request, response); + return; + + } else if (paths.count() == 3) { QString athlete = paths[0]; paths.removeFirst(); // GET ACTIVITY - if (paths[0] == "activity") listActivity(athlete, paths, request, response); + if (paths[0] == "activity") { + paths.removeFirst(); + listActivity(athlete, paths, request, response); + return; + } // GET MMP - if (paths[0] == "mmp") listMMP(athlete, paths, request, response); + if (paths[0] == "mmp") { + paths.removeFirst(); + listMMP(athlete, paths, request, response); + return; + } } + + // GET HERE ITS BAD! + response.setStatus(404); // malformed URL + response.setHeader("Content-Type", "text; charset=ISO-8859-1"); + response.write("malformed url"); } void @@ -145,7 +162,31 @@ APIWebService::listActivity(QString athlete, QStringList paths, HttpRequest &req { // list activities and associated metrics response.setHeader("Content-Type", "text; charset=ISO-8859-1"); - response.write("get activity under construction"); + + // does it exist ? + QString filename = QString("%1/%2/activities/%3").arg(home.absolutePath()).arg(athlete).arg(paths[0]); + + QString contents; + 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(); + file.close(); + + // write back in one hit + response.write(contents.toLocal8Bit(), true); + + } else { + + // nope? + response.setStatus(404); + response.write("file not found"); + return; + } } void