SenseClient is an API to interact with Sen.se platform. Sen.se is the place where Humans, Machines, Objects, Environments, Information, Physical and Virtual spaces mix up, talk, intertwine, interact, enrich and empower each other.
Dependencies: NetServicesProxy
SenseClient.cpp@0:ed7287a3edbf, 2011-09-06 (annotated)
- Committer:
- mimil
- Date:
- Tue Sep 06 13:31:10 2011 +0000
- Revision:
- 0:ed7287a3edbf
- Child:
- 1:0249701444ee
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mimil | 0:ed7287a3edbf | 1 | /* |
mimil | 0:ed7287a3edbf | 2 | Copyright [2011] [mimilowns@gmail.com] |
mimil | 0:ed7287a3edbf | 3 | |
mimil | 0:ed7287a3edbf | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
mimil | 0:ed7287a3edbf | 5 | you may not use this file except in compliance with the License. |
mimil | 0:ed7287a3edbf | 6 | You may obtain a copy of the License at |
mimil | 0:ed7287a3edbf | 7 | |
mimil | 0:ed7287a3edbf | 8 | http://www.apache.org/licenses/LICENSE-2.0 |
mimil | 0:ed7287a3edbf | 9 | |
mimil | 0:ed7287a3edbf | 10 | Unless required by applicable law or agreed to in writing, software |
mimil | 0:ed7287a3edbf | 11 | distributed under the License is distributed on an "AS IS" BASIS, |
mimil | 0:ed7287a3edbf | 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mimil | 0:ed7287a3edbf | 13 | See the License for the specific language governing permissions and |
mimil | 0:ed7287a3edbf | 14 | limitations under the License. |
mimil | 0:ed7287a3edbf | 15 | */ |
mimil | 0:ed7287a3edbf | 16 | #include "SenseClient.h" |
mimil | 0:ed7287a3edbf | 17 | #include "HTTPClient.h" |
mimil | 0:ed7287a3edbf | 18 | |
mimil | 0:ed7287a3edbf | 19 | |
mimil | 0:ed7287a3edbf | 20 | SenseClient::SenseClient(const string& senseKey, const string& httpproxy) : _jsonContent("application/json"), _jsonRespContent("application/json") { |
mimil | 0:ed7287a3edbf | 21 | _key = senseKey; |
mimil | 0:ed7287a3edbf | 22 | _proxy = httpproxy; |
mimil | 0:ed7287a3edbf | 23 | } |
mimil | 0:ed7287a3edbf | 24 | |
mimil | 0:ed7287a3edbf | 25 | SenseClient::~SenseClient() { |
mimil | 0:ed7287a3edbf | 26 | } |
mimil | 0:ed7287a3edbf | 27 | |
mimil | 0:ed7287a3edbf | 28 | void SenseClient::PostEvent(const string& feedID, const string& value) { |
mimil | 0:ed7287a3edbf | 29 | _jsonContent.set("{\"feed_id\":"+feedID+", \"value\":"+value+"}"); |
mimil | 0:ed7287a3edbf | 30 | |
mimil | 0:ed7287a3edbf | 31 | string uri = "http://api.sen.se/event_api/events/"; |
mimil | 0:ed7287a3edbf | 32 | HTTPClient _client; |
mimil | 0:ed7287a3edbf | 33 | _client.setRequestHeader("sense_key", _key); |
mimil | 0:ed7287a3edbf | 34 | if(!_proxy.empty()) { |
mimil | 0:ed7287a3edbf | 35 | _client.setProxy(_proxy.c_str()); |
mimil | 0:ed7287a3edbf | 36 | } |
mimil | 0:ed7287a3edbf | 37 | |
mimil | 0:ed7287a3edbf | 38 | _result = _client.post(uri.c_str(), _jsonContent, &_jsonRespContent); |
mimil | 0:ed7287a3edbf | 39 | _response = _client.getHTTPResponseCode(); |
mimil | 0:ed7287a3edbf | 40 | } |
mimil | 0:ed7287a3edbf | 41 | |
mimil | 0:ed7287a3edbf | 42 | void SenseClient::GetLastFeedEvent(const string& feedID) { |
mimil | 0:ed7287a3edbf | 43 | string uri = "http://api.sen.se/event_api/feeds/"+feedID+"/last_event/"; |
mimil | 0:ed7287a3edbf | 44 | |
mimil | 0:ed7287a3edbf | 45 | HTTPClient _client; |
mimil | 0:ed7287a3edbf | 46 | _client.setRequestHeader("sense_key", _key); |
mimil | 0:ed7287a3edbf | 47 | if(!_proxy.empty()) { |
mimil | 0:ed7287a3edbf | 48 | _client.setProxy(_proxy.c_str()); |
mimil | 0:ed7287a3edbf | 49 | } |
mimil | 0:ed7287a3edbf | 50 | |
mimil | 0:ed7287a3edbf | 51 | _result = _client.get(uri.c_str(), &_jsonRespContent); |
mimil | 0:ed7287a3edbf | 52 | _response = _client.getHTTPResponseCode(); |
mimil | 0:ed7287a3edbf | 53 | } |
mimil | 0:ed7287a3edbf | 54 | |
mimil | 0:ed7287a3edbf | 55 | void SenseClient::GetDeviceLastEvent(const string& deviceID) { |
mimil | 0:ed7287a3edbf | 56 | string uri = "http://api.sen.se/devices/"+deviceID+"/last_event/"; |
mimil | 0:ed7287a3edbf | 57 | |
mimil | 0:ed7287a3edbf | 58 | HTTPClient _client; |
mimil | 0:ed7287a3edbf | 59 | _client.setRequestHeader("sense_key", _key); |
mimil | 0:ed7287a3edbf | 60 | if(!_proxy.empty()) { |
mimil | 0:ed7287a3edbf | 61 | _client.setProxy(_proxy.c_str()); |
mimil | 0:ed7287a3edbf | 62 | } |
mimil | 0:ed7287a3edbf | 63 | |
mimil | 0:ed7287a3edbf | 64 | _result = _client.get(uri.c_str(), &_jsonRespContent); |
mimil | 0:ed7287a3edbf | 65 | _response = _client.getHTTPResponseCode(); |
mimil | 0:ed7287a3edbf | 66 | } |
mimil | 0:ed7287a3edbf | 67 | |
mimil | 0:ed7287a3edbf | 68 | // http result and response |
mimil | 0:ed7287a3edbf | 69 | HTTPResult SenseClient::Result() { |
mimil | 0:ed7287a3edbf | 70 | return _result; |
mimil | 0:ed7287a3edbf | 71 | } |
mimil | 0:ed7287a3edbf | 72 | int SenseClient::Response() { |
mimil | 0:ed7287a3edbf | 73 | return _response; |
mimil | 0:ed7287a3edbf | 74 | } |
mimil | 0:ed7287a3edbf | 75 | |
mimil | 0:ed7287a3edbf | 76 | HTTPText SenseClient::ResponseContent() { |
mimil | 0:ed7287a3edbf | 77 | return _jsonRespContent; |
mimil | 0:ed7287a3edbf | 78 | } |