Pachube

Dependents:   Pachube_TestProgram _DearMrJeffMourich StarBoardOrangeExpansion2 Toragi_2011_04_Pachube ... more

Files at this revision

API Documentation at this revision

Comitter:
shintamainjp
Date:
Thu Sep 30 22:18:37 2010 +0000
Commit message:

Changed in this revision

Pachube.cpp Show annotated file Show diff for this revision Revisions of this file
Pachube.h Show annotated file Show diff for this revision Revisions of this file
PachubeV2CSV.cpp Show annotated file Show diff for this revision Revisions of this file
PachubeV2CSV.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 19accbe9a05e Pachube.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pachube.cpp	Thu Sep 30 22:18:37 2010 +0000
@@ -0,0 +1,17 @@
+/**
+ * Pachube API interface driver. (Version 0.0.1)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+
+#include "Pachube.h"
+
+const std::string Pachube::REQUESTHEADER = "X-PachubeApiKey";
+const std::string Pachube::URLBASE_V2 = "http://api.pachube.com/v2";
+
+Pachube::Pachube(std::string APIKEY) : APIKEY(APIKEY) {
+}
+
+Pachube::~Pachube() {
+}
diff -r 000000000000 -r 19accbe9a05e Pachube.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pachube.h	Thu Sep 30 22:18:37 2010 +0000
@@ -0,0 +1,168 @@
+/**
+ * Pachube API interface driver. (Version 0.0.1)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+
+#ifndef _PACHUBE_H_
+#define _PACHUBE_H_
+
+#include <string>
+
+class Pachube {
+public:
+
+    explicit Pachube(std::string APIKEY);
+
+    virtual ~Pachube();
+
+    /*
+     * =================================================================
+     *  Environments (feeds)
+     * =================================================================
+     */
+
+    /**
+     * List all available feeds: GET /v2/feeds
+     *
+     * @param page Integer indicating which page of results you are requesting.
+     * @param per_page Integer defining how many results to return per page (1 to 1000).
+     * @param content String parameter ('full' or 'summary') describing whether we want full or summary results. Full results means all datastream values are returned, summary just returns the environment meta data for each feed.
+     * @param q Full text search parameter. Should return any feeds matching this string.
+     * @param tag Returns feeds containing datastreams tagged with the search query.
+     * @param user Returns feeds created by the user specified.
+     * @param units Returns feeds containing datastreams with units specified by the search query.
+     * @param status Possible values ('live', 'frozen', or 'all'). Whether to search for only live feeds, only frozen feeds, or all feeds. Defaults to all.
+     * @param order Order of returned feeds. Possible values ('created_at', 'retrieved_at', or 'relevance').
+     */
+    virtual int listAllAvailableFeeds(
+        int page,
+        int per_page,
+        std::string content,
+        std::string q,
+        std::string tag,
+        std::string user,
+        std::string units,
+        std::string status,
+        std::string order,
+        std::string &datatext) = 0;
+
+    /**
+     * Create new feed: POST /v2/feeds
+     */
+    virtual int createNewFeed(void) = 0;
+
+    /**
+     * Read feed: GET /v2/feeds/<feed_id>
+     */
+    virtual int readFeed(int feed_id, std::string &datatext) = 0;
+
+    /**
+     * Update feed: PUT /v2/feeds/<feed_id>
+     */
+    virtual int updateFeed(int feed_id) = 0;
+
+    /**
+     * Delete feed: DELETE /v2/feeds/<feed_id>
+     */
+    virtual int deleteFeed(int feed_id) = 0;
+
+    /*
+     * =================================================================
+     *  Datastreams
+     * =================================================================
+     */
+
+    /**
+     * Create new datastream: POST /v2/feeds/<feed_id>/datastreams
+     *
+     * @param feed_id Feed ID.
+     * @param stream_id Stream ID text.
+     * @param value value.
+     *
+     * @return Return code from a web server.
+     */
+    virtual int createNewDataStream(int feed_id, std::string stream_id, std::string value) = 0;
+
+    /**
+     * Read datastream - GET /v2/feeds/<feed_id>/datastreams/<datastream_id>
+     *
+     * @param feed_id Feed ID.
+     * @param stream_id Stream ID text.
+     *
+     * @return Return code from a web server.
+     */
+    virtual int readDataStream(int feed_id, std::string stream_id, std::string &datatext) = 0;
+
+    /**
+     * Update datastream: PUT /v2/feeds/<feed_id>/datastreams/<datastream_id>
+     *
+     * @param feed_id Feed ID.
+     * @param stream_id Stream ID text.
+     * @param value value.
+     *
+     * @return Return code from a web server.
+     */
+    virtual int updateDataStream(int feed_id, std::string stream_id, std::string value) = 0;
+
+    /**
+     * Delete datastream: DELETE /v2/feeds/<feed_id>/datastreams/<datastream_id>
+     *
+     * @param feed_id Feed ID.
+     * @param stream_id Stream ID text.
+     *
+     * @return Return code from a web server.
+     */
+    virtual int deleteDataStream(int feed_id, std::string stream_id) = 0;
+
+protected:
+    const std::string APIKEY;
+    static const std::string REQUESTHEADER;
+    static const std::string URLBASE_V2;
+};
+
+#endif
+
+/*
+Pachube v2 API Documentation
+http://api.pachube.com/v2/
+
+# Environments (feeds)
+
+o   * List all available feeds: GET /v2/feeds
+o   * Create new feed: POST /v2/feeds
+o   * Read feed: GET /v2/feeds/<feed_id>
+o   * Update feed: PUT /v2/feeds/<feed_id>
+o   * Delete feed: DELETE /v2/feeds/<feed_id>
+
+# Datastreams
+
+o   * Create new datastream: POST /v2/feeds/<feed_id>/datastreams
+o   * Read datastream - GET /v2/feeds/<feed_id>/datastreams/<datastream_id>
+o   * Update datastream: PUT /v2/feeds/<feed_id>/datastreams/<datastream_id>
+o   * Delete datastream: DELETE /v2/feeds/<feed_id>/datastreams/<datastream_id>
+
+# Datapoints
+
+    * Create datapoint: POST /v2/feeds/<feed_id>/datastreams/<datastream_id>/datapoints
+    * Read datapoint: GET /v2/feeds/<feed_id>/datastreams/<datastream_id>/datapoints/<timestamp>
+    * Update datapoint: PUT /v2/feeds/<feed_id>/datastreams/<datastream_id>/datapoints/<timestamp>
+    * Delete datapoint: DELETE /v2/feeds/<feed_id>/datastreams/<datastream_id>/datapoints/<timestamp>
+
+# Triggers
+
+    * List triggers: GET /v2/triggers
+    * Create trigger: POST /v2/triggers
+    * Read trigger: GET /v2/triggers/<trigger_id>
+    * Update trigger: PUT /v2/triggers/<trigger_id>
+    * Delete trigger: DELETE /v2/triggers/<trigger_id>
+
+# Users
+
+    * List all users: GET /v2/users
+    * Create user: POST /v2/users
+    * Read user: GET /v2/users/<user_id>
+    * Update user: PUT /v2/users/<user_id>
+    * Delete user: DELETE /v2/users/<user_id>
+*/
\ No newline at end of file
diff -r 000000000000 -r 19accbe9a05e PachubeV2CSV.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PachubeV2CSV.cpp	Thu Sep 30 22:18:37 2010 +0000
@@ -0,0 +1,266 @@
+/**
+ * Pachube API interface driver. (Version 0.0.1)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+
+#include "PachubeV2CSV.h"
+#include "EthernetNetIf.h"
+#include "HTTPClient.h"
+
+PachubeV2CSV::PachubeV2CSV(std::string APIKEY) : Pachube(APIKEY) {
+}
+
+PachubeV2CSV::~PachubeV2CSV() {
+}
+
+/*
+ * =================================================================
+ *  Environments (feeds)
+ * =================================================================
+ */
+
+/**
+ * List all available feeds: GET /v2/feeds
+ */
+int PachubeV2CSV::listAllAvailableFeeds(
+        int page,
+        int per_page,
+        std::string content,
+        std::string q,
+        std::string tag,
+        std::string user,
+        std::string units,
+        std::string status,
+        std::string order,
+        std::string &datatext) {
+    HTTPClient client;
+    client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
+
+    HTTPText text("text/csv");
+    text.set("");
+    HTTPText data;
+    
+    /*
+     * Example: http://api.pachube.com/v2/feeds
+     */
+    char tmp[32];
+    sprintf(tmp, "?page=%d&per_page=%d", page, per_page);
+    string URL = Pachube::URLBASE_V2 + "/feeds.csv" + std::string(tmp);
+    
+    if (content.length() > 0) {
+        URL = URL + "&content=" + content;
+    }
+    if (q.length() > 0) {
+        URL = URL + "&q=" + q;
+    }
+    if (tag.length() > 0) {
+        URL = URL + "&tag=" + tag;
+    }
+    if (user.length() > 0) {
+        URL = URL + "&user=" + user;
+    }
+    if (units.length() > 0) {
+        URL = URL + "&units=" + units;
+    }
+    if (status.length() > 0) {
+        URL = URL + "&status=" + status;
+    }
+    if (order.length() > 0) {
+        URL = URL + "&order=" + order;
+    }
+
+    HTTPResult result = client.get(URL.c_str(), &data);
+    datatext = data.get();
+    
+    return client.getHTTPResponseCode();
+}
+
+/**
+ * Create new feed: POST /v2/feeds
+ */
+int PachubeV2CSV::createNewFeed(void) {
+    error("CSV is not supported for creating Environments because it cannot represent the required parameters due to its very simple format.");
+    return 404;
+}
+
+/**
+ * Read feed: GET /v2/feeds/<feed_id>
+ */
+int PachubeV2CSV::readFeed(int feed_id, std::string &datatext) {
+    HTTPClient client;
+    client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
+
+    HTTPText text("text/csv");
+    text.set("");
+    HTTPText data;
+
+    /*
+     * Example: http://api.pachube.com/v2/feeds/1977
+     */
+    char feed_id_char[32];
+    sprintf(feed_id_char, "%d", feed_id);
+    const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + ".csv";
+
+    HTTPResult result = client.get(URL.c_str(), &data);
+    datatext = data.get();
+    return client.getHTTPResponseCode();
+}
+
+/**
+ * Update feed: PUT /v2/feeds/<feed_id>
+ */
+int PachubeV2CSV::updateFeed(int feed_id) {
+    HTTPClient client;
+    client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
+
+    HTTPText text("text/csv");
+    text.set("");
+
+    /*
+     * Example: http://api.pachube.com/v2/feeds/1977?_method=put
+     */
+    char feed_id_char[32];
+    sprintf(feed_id_char, "%d", feed_id);
+    const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "?_method=put";
+
+    HTTPResult result = client.post(URL.c_str(), text, NULL);
+    return client.getHTTPResponseCode();
+}
+
+/**
+ * Delete feed: DELETE /v2/feeds/<feed_id>
+ */
+int PachubeV2CSV::deleteFeed(int feed_id) {
+    HTTPClient client;
+    client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
+
+    HTTPText text("text/csv");
+    text.set("");
+
+    /*
+     * Example: http://api.pachube.com/v2/feeds/1977?_method=delete
+     */
+    char feed_id_char[32];
+    sprintf(feed_id_char, "%d", feed_id);
+    const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "?_method=delete";
+
+    HTTPResult result = client.post(URL.c_str(), text, NULL);
+    return client.getHTTPResponseCode();
+}
+
+/*
+ * =================================================================
+ *  Datastreams
+ * =================================================================
+ */
+
+/**
+ * Create new datastream: POST /v2/feeds/<feed_id>/datastreams
+ *
+ * @param feed_id Feed ID.
+ * @param stream_id Stream ID text.
+ * @param value value.
+ *
+ * @return Return code from a web server.
+ */
+int PachubeV2CSV::createNewDataStream(int feed_id, std::string stream_id, std::string value) {
+    HTTPClient client;
+    client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
+
+    std::string data = stream_id + "," + value;
+    HTTPText text("text/csv");
+    text.set(data);
+
+    /*
+     * Example: http://api.pachube.com/v2/feeds/1977/datastreams
+     */
+    char feed_id_char[32];
+    sprintf(feed_id_char, "%d", feed_id);
+    const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams";
+
+    HTTPResult result = client.post(URL.c_str(), text, NULL);
+    return client.getHTTPResponseCode();
+}
+
+/**
+ * Read datastream - GET /v2/feeds/<feed_id>/datastreams/<datastream_id>
+ *
+ * @param feed_id Feed ID.
+ * @param stream_id Stream ID text.
+ *
+ * @return Return code from a web server.
+ */
+int PachubeV2CSV::readDataStream(int feed_id, std::string stream_id, std::string &datatext) {
+    HTTPClient client;
+    client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
+
+    HTTPText text("text/csv");
+    text.set("");
+    HTTPText data;
+
+    /*
+     * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy
+     */
+    char feed_id_char[32];
+    sprintf(feed_id_char, "%d", feed_id);
+    const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + ".csv";
+
+    HTTPResult result = client.get(URL.c_str(), &data);
+    datatext = data.get();
+    return client.getHTTPResponseCode();
+}
+
+/**
+ * Update datastream: PUT /v2/feeds/<feed_id>/datastreams/<datastream_id>
+ *
+ * @param feed_id Feed ID.
+ * @param stream_id Stream ID text.
+ * @param value value.
+ *
+ * @return Return code from a web server.
+ */
+int PachubeV2CSV::updateDataStream(int feed_id, std::string stream_id, std::string value) {
+    HTTPClient client;
+    client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
+
+    HTTPText text("text/csv");
+    text.set(value);
+
+    /*
+     * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy?_method=put
+     */
+    char feed_id_char[32];
+    sprintf(feed_id_char, "%d", feed_id);
+    const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + "?_method=put";
+
+    HTTPResult result = client.post(URL.c_str(), text, NULL);
+    return client.getHTTPResponseCode();
+}
+
+/**
+ * Delete datastream: DELETE /v2/feeds/<feed_id>/datastreams/<datastream_id>
+ *
+ * @param feed_id Feed ID.
+ * @param stream_id Stream ID text.
+ *
+ * @return Return code from a web server.
+ */
+int PachubeV2CSV::deleteDataStream(int feed_id, std::string stream_id) {
+    HTTPClient client;
+    client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
+
+    HTTPText text("text/csv");
+    text.set("");
+
+    /*
+     * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy?_method=delete
+     */
+    char feed_id_char[32];
+    sprintf(feed_id_char, "%d", feed_id);
+    const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + "?_method=delete";
+
+    HTTPResult result = client.post(URL.c_str(), text, NULL);
+    return client.getHTTPResponseCode();
+}
diff -r 000000000000 -r 19accbe9a05e PachubeV2CSV.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PachubeV2CSV.h	Thu Sep 30 22:18:37 2010 +0000
@@ -0,0 +1,121 @@
+/**
+ * Pachube API interface driver. (Version 0.0.1)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+
+#ifndef _PACHUBE_V2_CSV_H_
+#define _PACHUBE_V2_CSV_H_
+
+#include "Pachube.h"
+
+class PachubeV2CSV : public Pachube {
+public:
+
+    explicit PachubeV2CSV(std::string APIKEY);
+
+    virtual ~PachubeV2CSV();
+
+    /*
+     * =================================================================
+     *  Environments (feeds)
+     * =================================================================
+     */
+
+    /**
+     * List all available feeds: GET /v2/feeds
+     *
+     * @param page Integer indicating which page of results you are requesting.
+     * @param per_page Integer defining how many results to return per page (1 to 1000).
+     * @param content String parameter ('full' or 'summary') describing whether we want full or summary results. Full results means all datastream values are returned, summary just returns the environment meta data for each feed.
+     * @param q Full text search parameter. Should return any feeds matching this string.
+     * @param tag Returns feeds containing datastreams tagged with the search query.
+     * @param user Returns feeds created by the user specified.
+     * @param units Returns feeds containing datastreams with units specified by the search query.
+     * @param status Possible values ('live', 'frozen', or 'all'). Whether to search for only live feeds, only frozen feeds, or all feeds. Defaults to all.
+     * @param order Order of returned feeds. Possible values ('created_at', 'retrieved_at', or 'relevance').
+     */
+    virtual int listAllAvailableFeeds(
+        int page,
+        int per_page,
+        std::string content,
+        std::string q,
+        std::string tag,
+        std::string user,
+        std::string units,
+        std::string status,
+        std::string order,
+        std::string &datatext);
+
+    /**
+     * Create new feed: POST /v2/feeds
+     */
+    virtual int createNewFeed(void);
+
+    /**
+     * Read feed: GET /v2/feeds/<feed_id>
+     */
+    virtual int readFeed(int feed_id, std::string &datatext);
+
+    /**
+     * Update feed: PUT /v2/feeds/<feed_id>
+     */
+    virtual int updateFeed(int feed_id);
+
+    /**
+     * Delete feed: DELETE /v2/feeds/<feed_id>
+     */
+    virtual int deleteFeed(int feed_id);
+
+    /*
+     * =================================================================
+     *  Datastreams
+     * =================================================================
+     */
+
+    /**
+     * Create new datastream: POST /v2/feeds/<feed_id>/datastreams
+     *
+     * @param feed_id Feed ID.
+     * @param stream_id Stream ID text.
+     * @param value value.
+     *
+     * @return Return code from a web server.
+     */
+    virtual int createNewDataStream(int feed_id, std::string stream_id, std::string value);
+
+    /**
+     * Read datastream - GET /v2/feeds/<feed_id>/datastreams/<datastream_id>
+     *
+     * @param feed_id Feed ID.
+     * @param stream_id Stream ID text.
+     *
+     * @return Return code from a web server.
+     */
+    virtual int readDataStream(int feed_id, std::string stream_id, std::string &datatext);
+
+    /**
+     * Update datastream: PUT /v2/feeds/<feed_id>/datastreams/<datastream_id>
+     *
+     * @param feed_id Feed ID.
+     * @param stream_id Stream ID text.
+     * @param value value.
+     *
+     * @return Return code from a web server.
+     */
+    virtual int updateDataStream(int feed_id, std::string stream_id, std::string value);
+
+    /**
+     * Delete datastream: DELETE /v2/feeds/<feed_id>/datastreams/<datastream_id>
+     *
+     * @param feed_id Feed ID.
+     * @param stream_id Stream ID text.
+     *
+     * @return Return code from a web server.
+     */
+    virtual int deleteDataStream(int feed_id, std::string stream_id);
+
+};
+
+#endif