Dependents: xively-jumpstart-demo
This is Xively C library, the code lives on GitHub.
See our example program and the tutorial, documentation can bee found here.
src/libxively/http_transport_layer.c@0:53753690a8bf, 2013-05-13 (annotated)
- Committer:
- xively
- Date:
- Mon May 13 19:28:22 2013 +0000
- Revision:
- 0:53753690a8bf
libxively v0.1.1-rc0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xively | 0:53753690a8bf | 1 | // Copyright (c) 2003-2013, LogMeIn, Inc. All rights reserved. |
xively | 0:53753690a8bf | 2 | // This is part of Xively C library, it is under the BSD 3-Clause license. |
xively | 0:53753690a8bf | 3 | |
xively | 0:53753690a8bf | 4 | /** |
xively | 0:53753690a8bf | 5 | * \file http_transport_layer.c |
xively | 0:53753690a8bf | 6 | * \author Olgierd Humenczuk |
xively | 0:53753690a8bf | 7 | * \brief Implements HTTP _transport layer_ encoders and decoders specific to Xively REST/HTTP API [see http_transport_layer.h] |
xively | 0:53753690a8bf | 8 | */ |
xively | 0:53753690a8bf | 9 | |
xively | 0:53753690a8bf | 10 | #include <string.h> |
xively | 0:53753690a8bf | 11 | #include <assert.h> |
xively | 0:53753690a8bf | 12 | #include <stdio.h> |
xively | 0:53753690a8bf | 13 | |
xively | 0:53753690a8bf | 14 | #include "http_transport.h" |
xively | 0:53753690a8bf | 15 | #include "http_layer_queries.h" |
xively | 0:53753690a8bf | 16 | #include "http_consts.h" |
xively | 0:53753690a8bf | 17 | #include "http_layer_parser.h" |
xively | 0:53753690a8bf | 18 | #include "xi_macros.h" |
xively | 0:53753690a8bf | 19 | #include "xi_helpers.h" |
xively | 0:53753690a8bf | 20 | #include "xi_err.h" |
xively | 0:53753690a8bf | 21 | |
xively | 0:53753690a8bf | 22 | static char XI_HTTP_QUERY_BUFFER[ XI_QUERY_BUFFER_SIZE + XI_CONTENT_BUFFER_SIZE ]; |
xively | 0:53753690a8bf | 23 | static char XI_HTTP_QUERY_DATA[ XI_CONTENT_BUFFER_SIZE ]; |
xively | 0:53753690a8bf | 24 | |
xively | 0:53753690a8bf | 25 | |
xively | 0:53753690a8bf | 26 | inline static char* http_encode_concat( char* buffer, size_t buffer_size |
xively | 0:53753690a8bf | 27 | , const char* query, const char* content, const char* data ) |
xively | 0:53753690a8bf | 28 | { |
xively | 0:53753690a8bf | 29 | int offset = 0; |
xively | 0:53753690a8bf | 30 | int size = ( int ) buffer_size; |
xively | 0:53753690a8bf | 31 | int s = 0; |
xively | 0:53753690a8bf | 32 | |
xively | 0:53753690a8bf | 33 | s = snprintf( buffer, size, "%s", query ); |
xively | 0:53753690a8bf | 34 | XI_CHECK_S( s, size, offset, XI_HTTP_ENCODE_CREATE_DATASTREAM ); |
xively | 0:53753690a8bf | 35 | |
xively | 0:53753690a8bf | 36 | if( content != 0 ) |
xively | 0:53753690a8bf | 37 | { |
xively | 0:53753690a8bf | 38 | s = snprintf( buffer + offset |
xively | 0:53753690a8bf | 39 | , XI_MAX( size - offset, 0 ) |
xively | 0:53753690a8bf | 40 | , "%s", content ); |
xively | 0:53753690a8bf | 41 | XI_CHECK_S( s, size, offset, XI_HTTP_ENCODE_CREATE_DATASTREAM ); |
xively | 0:53753690a8bf | 42 | } |
xively | 0:53753690a8bf | 43 | |
xively | 0:53753690a8bf | 44 | s = snprintf( buffer + offset |
xively | 0:53753690a8bf | 45 | , XI_MAX( size - offset, 0 ) |
xively | 0:53753690a8bf | 46 | , "%s", XI_HTTP_CRLF ); |
xively | 0:53753690a8bf | 47 | XI_CHECK_S( s, size, offset, XI_HTTP_ENCODE_CREATE_DATASTREAM ); |
xively | 0:53753690a8bf | 48 | |
xively | 0:53753690a8bf | 49 | |
xively | 0:53753690a8bf | 50 | if( content != 0 && data != 0 ) |
xively | 0:53753690a8bf | 51 | { |
xively | 0:53753690a8bf | 52 | s = snprintf( buffer + offset |
xively | 0:53753690a8bf | 53 | , XI_MAX( size - offset, 0 ) |
xively | 0:53753690a8bf | 54 | , "%s", data ); |
xively | 0:53753690a8bf | 55 | XI_CHECK_S( s, size, offset, XI_HTTP_ENCODE_CREATE_DATASTREAM ); |
xively | 0:53753690a8bf | 56 | } |
xively | 0:53753690a8bf | 57 | |
xively | 0:53753690a8bf | 58 | s = snprintf( buffer + offset |
xively | 0:53753690a8bf | 59 | , XI_MAX( size - offset, 0 ) |
xively | 0:53753690a8bf | 60 | , "%s", XI_HTTP_CRLF ); |
xively | 0:53753690a8bf | 61 | |
xively | 0:53753690a8bf | 62 | XI_CHECK_S( s, size, offset, XI_HTTP_ENCODE_CREATE_DATASTREAM ); |
xively | 0:53753690a8bf | 63 | |
xively | 0:53753690a8bf | 64 | return buffer; |
xively | 0:53753690a8bf | 65 | |
xively | 0:53753690a8bf | 66 | err_handling: |
xively | 0:53753690a8bf | 67 | return 0; |
xively | 0:53753690a8bf | 68 | } |
xively | 0:53753690a8bf | 69 | |
xively | 0:53753690a8bf | 70 | const char* http_encode_create_datastream( |
xively | 0:53753690a8bf | 71 | const data_layer_t* data_transport |
xively | 0:53753690a8bf | 72 | , const char* x_api_key |
xively | 0:53753690a8bf | 73 | , int32_t feed_id |
xively | 0:53753690a8bf | 74 | , const char *datastream_id |
xively | 0:53753690a8bf | 75 | , const xi_datapoint_t* datapoint ) |
xively | 0:53753690a8bf | 76 | { |
xively | 0:53753690a8bf | 77 | // prepare parts |
xively | 0:53753690a8bf | 78 | const char* data = data_transport->encode_create_datastream( |
xively | 0:53753690a8bf | 79 | datastream_id, datapoint ); |
xively | 0:53753690a8bf | 80 | |
xively | 0:53753690a8bf | 81 | if( data == 0 ) { return 0; } |
xively | 0:53753690a8bf | 82 | |
xively | 0:53753690a8bf | 83 | const char* query = http_construct_request_datastream( |
xively | 0:53753690a8bf | 84 | XI_HTTP_QUERY_POST, &feed_id |
xively | 0:53753690a8bf | 85 | , 0, x_api_key |
xively | 0:53753690a8bf | 86 | ); |
xively | 0:53753690a8bf | 87 | |
xively | 0:53753690a8bf | 88 | if( query == 0 ) { return 0; } |
xively | 0:53753690a8bf | 89 | |
xively | 0:53753690a8bf | 90 | const char* content = http_construct_content( strlen( data ) ); |
xively | 0:53753690a8bf | 91 | |
xively | 0:53753690a8bf | 92 | return http_encode_concat( XI_HTTP_QUERY_BUFFER, sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 93 | , query, content, data ); |
xively | 0:53753690a8bf | 94 | } |
xively | 0:53753690a8bf | 95 | |
xively | 0:53753690a8bf | 96 | const char* http_encode_update_datastream( |
xively | 0:53753690a8bf | 97 | const data_layer_t* data_layer |
xively | 0:53753690a8bf | 98 | , const char* x_api_key |
xively | 0:53753690a8bf | 99 | , int32_t feed_id |
xively | 0:53753690a8bf | 100 | , const char *datastream_id |
xively | 0:53753690a8bf | 101 | , const xi_datapoint_t* datapoint ) |
xively | 0:53753690a8bf | 102 | { |
xively | 0:53753690a8bf | 103 | // prepare parts |
xively | 0:53753690a8bf | 104 | const char* data = data_layer->encode_datapoint( datapoint ); |
xively | 0:53753690a8bf | 105 | |
xively | 0:53753690a8bf | 106 | if( data == 0 ) { return 0; } |
xively | 0:53753690a8bf | 107 | |
xively | 0:53753690a8bf | 108 | const char* query = http_construct_request_datastream( |
xively | 0:53753690a8bf | 109 | XI_HTTP_QUERY_PUT |
xively | 0:53753690a8bf | 110 | , &feed_id |
xively | 0:53753690a8bf | 111 | , datastream_id |
xively | 0:53753690a8bf | 112 | , x_api_key |
xively | 0:53753690a8bf | 113 | ); |
xively | 0:53753690a8bf | 114 | |
xively | 0:53753690a8bf | 115 | if( query == 0 ) { return 0; } |
xively | 0:53753690a8bf | 116 | |
xively | 0:53753690a8bf | 117 | const char* content = http_construct_content( strlen( data ) ); |
xively | 0:53753690a8bf | 118 | |
xively | 0:53753690a8bf | 119 | return http_encode_concat( XI_HTTP_QUERY_BUFFER, sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 120 | , query, content, data ); |
xively | 0:53753690a8bf | 121 | } |
xively | 0:53753690a8bf | 122 | |
xively | 0:53753690a8bf | 123 | const char* http_encode_get_datastream( |
xively | 0:53753690a8bf | 124 | const data_layer_t* data_layer |
xively | 0:53753690a8bf | 125 | , const char* x_api_key |
xively | 0:53753690a8bf | 126 | , int32_t feed_id |
xively | 0:53753690a8bf | 127 | , const char *datastream_id ) |
xively | 0:53753690a8bf | 128 | { |
xively | 0:53753690a8bf | 129 | XI_UNUSED( data_layer ); |
xively | 0:53753690a8bf | 130 | |
xively | 0:53753690a8bf | 131 | // prepare parts |
xively | 0:53753690a8bf | 132 | const char* query = http_construct_request_datastream( |
xively | 0:53753690a8bf | 133 | XI_HTTP_QUERY_GET |
xively | 0:53753690a8bf | 134 | , &feed_id |
xively | 0:53753690a8bf | 135 | , datastream_id |
xively | 0:53753690a8bf | 136 | , x_api_key ); |
xively | 0:53753690a8bf | 137 | |
xively | 0:53753690a8bf | 138 | if( query == 0 ) { return 0; } |
xively | 0:53753690a8bf | 139 | |
xively | 0:53753690a8bf | 140 | return http_encode_concat( XI_HTTP_QUERY_BUFFER, sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 141 | , query, 0, 0 ); |
xively | 0:53753690a8bf | 142 | } |
xively | 0:53753690a8bf | 143 | |
xively | 0:53753690a8bf | 144 | const char* http_encode_delete_datastream( |
xively | 0:53753690a8bf | 145 | const data_layer_t* data_layer |
xively | 0:53753690a8bf | 146 | , const char* x_api_key |
xively | 0:53753690a8bf | 147 | , int32_t feed_id |
xively | 0:53753690a8bf | 148 | , const char *datastream_id ) |
xively | 0:53753690a8bf | 149 | { |
xively | 0:53753690a8bf | 150 | XI_UNUSED( data_layer ); |
xively | 0:53753690a8bf | 151 | |
xively | 0:53753690a8bf | 152 | // prepare parts |
xively | 0:53753690a8bf | 153 | const char* query = http_construct_request_datastream( |
xively | 0:53753690a8bf | 154 | XI_HTTP_QUERY_DELETE |
xively | 0:53753690a8bf | 155 | , &feed_id |
xively | 0:53753690a8bf | 156 | , datastream_id |
xively | 0:53753690a8bf | 157 | , x_api_key ); |
xively | 0:53753690a8bf | 158 | |
xively | 0:53753690a8bf | 159 | if( query == 0 ) { return 0; } |
xively | 0:53753690a8bf | 160 | |
xively | 0:53753690a8bf | 161 | return http_encode_concat( XI_HTTP_QUERY_BUFFER, sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 162 | , query, 0, 0 ); |
xively | 0:53753690a8bf | 163 | } |
xively | 0:53753690a8bf | 164 | |
xively | 0:53753690a8bf | 165 | const char* http_encode_delete_datapoint( |
xively | 0:53753690a8bf | 166 | const data_layer_t* data_transport |
xively | 0:53753690a8bf | 167 | , const char* x_api_key |
xively | 0:53753690a8bf | 168 | , int32_t feed_id |
xively | 0:53753690a8bf | 169 | , const char *datastream_id |
xively | 0:53753690a8bf | 170 | , const xi_datapoint_t* o ) |
xively | 0:53753690a8bf | 171 | { |
xively | 0:53753690a8bf | 172 | XI_UNUSED( data_transport ); |
xively | 0:53753690a8bf | 173 | |
xively | 0:53753690a8bf | 174 | struct tm* ptm = xi_gmtime( |
xively | 0:53753690a8bf | 175 | ( time_t* ) &o->timestamp.timestamp ); |
xively | 0:53753690a8bf | 176 | |
xively | 0:53753690a8bf | 177 | int s = snprintf( XI_HTTP_QUERY_BUFFER |
xively | 0:53753690a8bf | 178 | , sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 179 | , "%s/datapoints/%04d-%02d-%02dT%02d:%02d:%02d.%06ldZ" |
xively | 0:53753690a8bf | 180 | , datastream_id |
xively | 0:53753690a8bf | 181 | , ptm->tm_year + 1900 |
xively | 0:53753690a8bf | 182 | , ptm->tm_mon + 1 |
xively | 0:53753690a8bf | 183 | , ptm->tm_mday |
xively | 0:53753690a8bf | 184 | , ptm->tm_hour |
xively | 0:53753690a8bf | 185 | , ptm->tm_min |
xively | 0:53753690a8bf | 186 | , ptm->tm_sec |
xively | 0:53753690a8bf | 187 | , o->timestamp.micro ); |
xively | 0:53753690a8bf | 188 | |
xively | 0:53753690a8bf | 189 | XI_CHECK_SIZE( s, ( int ) sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 190 | , XI_HTTP_ENCODE_DELETE_DATAPOINT ); |
xively | 0:53753690a8bf | 191 | |
xively | 0:53753690a8bf | 192 | { |
xively | 0:53753690a8bf | 193 | // prepare parts |
xively | 0:53753690a8bf | 194 | const char* query = http_construct_request_datastream( |
xively | 0:53753690a8bf | 195 | XI_HTTP_QUERY_DELETE |
xively | 0:53753690a8bf | 196 | , &feed_id |
xively | 0:53753690a8bf | 197 | , XI_HTTP_QUERY_BUFFER |
xively | 0:53753690a8bf | 198 | , x_api_key ); |
xively | 0:53753690a8bf | 199 | |
xively | 0:53753690a8bf | 200 | if( query == 0 ) { return 0; } |
xively | 0:53753690a8bf | 201 | |
xively | 0:53753690a8bf | 202 | return http_encode_concat( XI_HTTP_QUERY_BUFFER, sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 203 | , query, 0, 0 ); |
xively | 0:53753690a8bf | 204 | } |
xively | 0:53753690a8bf | 205 | |
xively | 0:53753690a8bf | 206 | err_handling: |
xively | 0:53753690a8bf | 207 | return 0; |
xively | 0:53753690a8bf | 208 | } |
xively | 0:53753690a8bf | 209 | |
xively | 0:53753690a8bf | 210 | const char* http_encode_update_feed( |
xively | 0:53753690a8bf | 211 | const data_layer_t* data_layer |
xively | 0:53753690a8bf | 212 | , const char* x_api_key |
xively | 0:53753690a8bf | 213 | , const xi_feed_t* feed ) |
xively | 0:53753690a8bf | 214 | { |
xively | 0:53753690a8bf | 215 | // prepare buffer |
xively | 0:53753690a8bf | 216 | XI_CLEAR_STATIC_BUFFER( XI_HTTP_QUERY_DATA ); |
xively | 0:53753690a8bf | 217 | |
xively | 0:53753690a8bf | 218 | // PRECONDITIONS |
xively | 0:53753690a8bf | 219 | assert( data_layer != 0 ); |
xively | 0:53753690a8bf | 220 | assert( x_api_key != 0 ); |
xively | 0:53753690a8bf | 221 | assert( feed != 0 ); |
xively | 0:53753690a8bf | 222 | |
xively | 0:53753690a8bf | 223 | // variables initialization |
xively | 0:53753690a8bf | 224 | const char* content = 0; |
xively | 0:53753690a8bf | 225 | const char* query = 0; |
xively | 0:53753690a8bf | 226 | |
xively | 0:53753690a8bf | 227 | { // data part preparation |
xively | 0:53753690a8bf | 228 | int offset = 0; |
xively | 0:53753690a8bf | 229 | int size = sizeof( XI_HTTP_QUERY_DATA ); |
xively | 0:53753690a8bf | 230 | int s = 0; |
xively | 0:53753690a8bf | 231 | |
xively | 0:53753690a8bf | 232 | // for each datastream |
xively | 0:53753690a8bf | 233 | // generate the list of datapoints that you want to update |
xively | 0:53753690a8bf | 234 | for( size_t i = 0; i < feed->datastream_count; ++i ) |
xively | 0:53753690a8bf | 235 | { |
xively | 0:53753690a8bf | 236 | const xi_datastream_t* curr_datastream = &feed->datastreams[ i ]; |
xively | 0:53753690a8bf | 237 | |
xively | 0:53753690a8bf | 238 | // for each datapoint |
xively | 0:53753690a8bf | 239 | for( size_t j = 0; j < curr_datastream->datapoint_count; ++j ) |
xively | 0:53753690a8bf | 240 | { |
xively | 0:53753690a8bf | 241 | const xi_datapoint_t* curr_datapoint |
xively | 0:53753690a8bf | 242 | = &curr_datastream->datapoints[ j ]; |
xively | 0:53753690a8bf | 243 | |
xively | 0:53753690a8bf | 244 | // add the datastream id to the buffer |
xively | 0:53753690a8bf | 245 | s = snprintf( |
xively | 0:53753690a8bf | 246 | XI_HTTP_QUERY_DATA + offset, XI_MAX( size - offset, 0 ), "%s," |
xively | 0:53753690a8bf | 247 | , curr_datastream->datastream_id ); |
xively | 0:53753690a8bf | 248 | |
xively | 0:53753690a8bf | 249 | XI_CHECK_S( s, size, offset, XI_HTTP_ENCODE_UPDATE_FEED ); |
xively | 0:53753690a8bf | 250 | |
xively | 0:53753690a8bf | 251 | // add the datapoint data to the buffer |
xively | 0:53753690a8bf | 252 | s = data_layer->encode_datapoint_in_place( |
xively | 0:53753690a8bf | 253 | XI_HTTP_QUERY_DATA + offset, XI_MAX( size - offset, 0 ) |
xively | 0:53753690a8bf | 254 | , curr_datapoint ); |
xively | 0:53753690a8bf | 255 | |
xively | 0:53753690a8bf | 256 | XI_CHECK_S( s, size, offset, XI_HTTP_ENCODE_UPDATE_FEED ); |
xively | 0:53753690a8bf | 257 | } |
xively | 0:53753690a8bf | 258 | } |
xively | 0:53753690a8bf | 259 | } |
xively | 0:53753690a8bf | 260 | |
xively | 0:53753690a8bf | 261 | query = http_construct_request_feed( |
xively | 0:53753690a8bf | 262 | XI_HTTP_QUERY_PUT |
xively | 0:53753690a8bf | 263 | , &feed->feed_id |
xively | 0:53753690a8bf | 264 | , x_api_key |
xively | 0:53753690a8bf | 265 | , 0 |
xively | 0:53753690a8bf | 266 | ); |
xively | 0:53753690a8bf | 267 | |
xively | 0:53753690a8bf | 268 | if( query == 0 ) { goto err_handling; } |
xively | 0:53753690a8bf | 269 | |
xively | 0:53753690a8bf | 270 | content = http_construct_content( strlen( XI_HTTP_QUERY_DATA ) ); |
xively | 0:53753690a8bf | 271 | |
xively | 0:53753690a8bf | 272 | return http_encode_concat( XI_HTTP_QUERY_BUFFER, sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 273 | , query, content, XI_HTTP_QUERY_DATA ); |
xively | 0:53753690a8bf | 274 | |
xively | 0:53753690a8bf | 275 | err_handling: |
xively | 0:53753690a8bf | 276 | return 0; |
xively | 0:53753690a8bf | 277 | } |
xively | 0:53753690a8bf | 278 | |
xively | 0:53753690a8bf | 279 | const char* http_encode_get_feed( |
xively | 0:53753690a8bf | 280 | const data_layer_t* data_layer |
xively | 0:53753690a8bf | 281 | , const char* x_api_key |
xively | 0:53753690a8bf | 282 | , const xi_feed_t* feed ) |
xively | 0:53753690a8bf | 283 | { |
xively | 0:53753690a8bf | 284 | // prepare buffer |
xively | 0:53753690a8bf | 285 | XI_CLEAR_STATIC_BUFFER( XI_HTTP_QUERY_DATA ); |
xively | 0:53753690a8bf | 286 | |
xively | 0:53753690a8bf | 287 | // PRECONDITIONS |
xively | 0:53753690a8bf | 288 | assert( data_layer != 0 ); |
xively | 0:53753690a8bf | 289 | assert( x_api_key != 0 ); |
xively | 0:53753690a8bf | 290 | assert( feed != 0 ); |
xively | 0:53753690a8bf | 291 | |
xively | 0:53753690a8bf | 292 | // variables initialization |
xively | 0:53753690a8bf | 293 | const char* query = 0; |
xively | 0:53753690a8bf | 294 | |
xively | 0:53753690a8bf | 295 | { // data part preparation |
xively | 0:53753690a8bf | 296 | int offset = 0; |
xively | 0:53753690a8bf | 297 | int size = sizeof( XI_HTTP_QUERY_DATA ); |
xively | 0:53753690a8bf | 298 | int s = 0; |
xively | 0:53753690a8bf | 299 | |
xively | 0:53753690a8bf | 300 | // for each datastream |
xively | 0:53753690a8bf | 301 | // generate the list of datastreams that you want to get |
xively | 0:53753690a8bf | 302 | for( size_t i = 0; i < feed->datastream_count; ++i ) |
xively | 0:53753690a8bf | 303 | { |
xively | 0:53753690a8bf | 304 | const xi_datastream_t* curr_datastream = &feed->datastreams[ i ]; |
xively | 0:53753690a8bf | 305 | |
xively | 0:53753690a8bf | 306 | // add the datastream id to the buffer |
xively | 0:53753690a8bf | 307 | s = snprintf( |
xively | 0:53753690a8bf | 308 | XI_HTTP_QUERY_DATA + offset, XI_MAX( size - offset, 0 ), i == 0 ? "?datastreams=%s" : ",%s" |
xively | 0:53753690a8bf | 309 | , curr_datastream->datastream_id ); |
xively | 0:53753690a8bf | 310 | |
xively | 0:53753690a8bf | 311 | XI_CHECK_S( s, size, offset, XI_HTTP_ENCODE_UPDATE_FEED ); |
xively | 0:53753690a8bf | 312 | } |
xively | 0:53753690a8bf | 313 | } |
xively | 0:53753690a8bf | 314 | |
xively | 0:53753690a8bf | 315 | query = http_construct_request_feed( |
xively | 0:53753690a8bf | 316 | XI_HTTP_QUERY_GET |
xively | 0:53753690a8bf | 317 | , &feed->feed_id |
xively | 0:53753690a8bf | 318 | , x_api_key |
xively | 0:53753690a8bf | 319 | , XI_HTTP_QUERY_DATA |
xively | 0:53753690a8bf | 320 | ); |
xively | 0:53753690a8bf | 321 | |
xively | 0:53753690a8bf | 322 | if( query == 0 ) { goto err_handling; } |
xively | 0:53753690a8bf | 323 | |
xively | 0:53753690a8bf | 324 | return http_encode_concat( XI_HTTP_QUERY_BUFFER, sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 325 | , query, 0, 0 ); |
xively | 0:53753690a8bf | 326 | |
xively | 0:53753690a8bf | 327 | err_handling: |
xively | 0:53753690a8bf | 328 | return 0; |
xively | 0:53753690a8bf | 329 | } |
xively | 0:53753690a8bf | 330 | |
xively | 0:53753690a8bf | 331 | const char* http_encode_datapoint_delete_range( |
xively | 0:53753690a8bf | 332 | const data_layer_t* data_layer |
xively | 0:53753690a8bf | 333 | , const char* x_api_key |
xively | 0:53753690a8bf | 334 | , int32_t feed_id |
xively | 0:53753690a8bf | 335 | , const char* datastream_id |
xively | 0:53753690a8bf | 336 | , const xi_timestamp_t* start |
xively | 0:53753690a8bf | 337 | , const xi_timestamp_t* end ) |
xively | 0:53753690a8bf | 338 | { |
xively | 0:53753690a8bf | 339 | XI_UNUSED( data_layer ); |
xively | 0:53753690a8bf | 340 | |
xively | 0:53753690a8bf | 341 | struct tm stm; |
xively | 0:53753690a8bf | 342 | struct tm etm; |
xively | 0:53753690a8bf | 343 | |
xively | 0:53753690a8bf | 344 | { |
xively | 0:53753690a8bf | 345 | struct tm* tmp = start ? xi_gmtime( |
xively | 0:53753690a8bf | 346 | ( time_t* ) &start->timestamp ) : 0; |
xively | 0:53753690a8bf | 347 | memcpy( &stm, tmp, sizeof( struct tm ) ); |
xively | 0:53753690a8bf | 348 | } |
xively | 0:53753690a8bf | 349 | |
xively | 0:53753690a8bf | 350 | { |
xively | 0:53753690a8bf | 351 | struct tm* tmp = end ? xi_gmtime( |
xively | 0:53753690a8bf | 352 | ( time_t* ) &end->timestamp ) : 0; |
xively | 0:53753690a8bf | 353 | memcpy( &etm, tmp, sizeof( struct tm ) ); |
xively | 0:53753690a8bf | 354 | } |
xively | 0:53753690a8bf | 355 | |
xively | 0:53753690a8bf | 356 | int s = 0; |
xively | 0:53753690a8bf | 357 | |
xively | 0:53753690a8bf | 358 | if( start && end ) |
xively | 0:53753690a8bf | 359 | { |
xively | 0:53753690a8bf | 360 | s = snprintf( XI_HTTP_QUERY_BUFFER |
xively | 0:53753690a8bf | 361 | , sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 362 | , "%s/datapoints?start=%04d-%02d-%02dT%02d:%02d:%02d.%06ldZ&end=%04d-%02d-%02dT%02d:%02d:%02d.%06ldZ" |
xively | 0:53753690a8bf | 363 | , datastream_id |
xively | 0:53753690a8bf | 364 | , stm.tm_year + 1900, stm.tm_mon + 1, stm.tm_mday |
xively | 0:53753690a8bf | 365 | , stm.tm_hour, stm.tm_min, stm.tm_sec, start->micro |
xively | 0:53753690a8bf | 366 | , etm.tm_year + 1900, etm.tm_mon + 1, etm.tm_mday |
xively | 0:53753690a8bf | 367 | , etm.tm_hour, etm.tm_min, etm.tm_sec, end->micro ); |
xively | 0:53753690a8bf | 368 | } |
xively | 0:53753690a8bf | 369 | else if( start ) |
xively | 0:53753690a8bf | 370 | { |
xively | 0:53753690a8bf | 371 | s = snprintf( XI_HTTP_QUERY_BUFFER |
xively | 0:53753690a8bf | 372 | , sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 373 | , "%s/datapoints?start=%04d-%02d-%02dT%02d:%02d:%02d.%06ldZ" |
xively | 0:53753690a8bf | 374 | , datastream_id |
xively | 0:53753690a8bf | 375 | , stm.tm_year + 1900, stm.tm_mon + 1, stm.tm_mday |
xively | 0:53753690a8bf | 376 | , stm.tm_hour, stm.tm_min, stm.tm_sec, start->micro ); |
xively | 0:53753690a8bf | 377 | } |
xively | 0:53753690a8bf | 378 | else if( end ) |
xively | 0:53753690a8bf | 379 | { |
xively | 0:53753690a8bf | 380 | s = snprintf( XI_HTTP_QUERY_BUFFER |
xively | 0:53753690a8bf | 381 | , sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 382 | , "%s/datapoints?end=%04d-%02d-%02dT%02d:%02d:%02d.%06ldZ" |
xively | 0:53753690a8bf | 383 | , datastream_id |
xively | 0:53753690a8bf | 384 | , etm.tm_year + 1900, etm.tm_mon + 1, etm.tm_mday |
xively | 0:53753690a8bf | 385 | , etm.tm_hour, etm.tm_min, etm.tm_sec, end->micro ); |
xively | 0:53753690a8bf | 386 | } |
xively | 0:53753690a8bf | 387 | else |
xively | 0:53753690a8bf | 388 | { |
xively | 0:53753690a8bf | 389 | // just set an error |
xively | 0:53753690a8bf | 390 | XI_CHECK_CND( 1 == 0, XI_HTTP_ENCODE_DELETE_RANGE_DATAPOINT ); |
xively | 0:53753690a8bf | 391 | } |
xively | 0:53753690a8bf | 392 | |
xively | 0:53753690a8bf | 393 | XI_CHECK_SIZE( s, ( int ) sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 394 | , XI_HTTP_ENCODE_DELETE_RANGE_DATAPOINT ); |
xively | 0:53753690a8bf | 395 | |
xively | 0:53753690a8bf | 396 | { |
xively | 0:53753690a8bf | 397 | // prepare parts |
xively | 0:53753690a8bf | 398 | const char* query = http_construct_request_datastream( |
xively | 0:53753690a8bf | 399 | XI_HTTP_QUERY_DELETE |
xively | 0:53753690a8bf | 400 | , &feed_id |
xively | 0:53753690a8bf | 401 | , XI_HTTP_QUERY_BUFFER |
xively | 0:53753690a8bf | 402 | , x_api_key ); |
xively | 0:53753690a8bf | 403 | |
xively | 0:53753690a8bf | 404 | if( query == 0 ) { return 0; } |
xively | 0:53753690a8bf | 405 | |
xively | 0:53753690a8bf | 406 | return http_encode_concat( XI_HTTP_QUERY_BUFFER, sizeof( XI_HTTP_QUERY_BUFFER ) |
xively | 0:53753690a8bf | 407 | , query, 0, 0 ); |
xively | 0:53753690a8bf | 408 | } |
xively | 0:53753690a8bf | 409 | |
xively | 0:53753690a8bf | 410 | err_handling: |
xively | 0:53753690a8bf | 411 | return 0; |
xively | 0:53753690a8bf | 412 | } |
xively | 0:53753690a8bf | 413 | |
xively | 0:53753690a8bf | 414 | |
xively | 0:53753690a8bf | 415 | const xi_response_t* http_decode_reply( |
xively | 0:53753690a8bf | 416 | const data_layer_t* data_layer |
xively | 0:53753690a8bf | 417 | , const char* response ) |
xively | 0:53753690a8bf | 418 | { |
xively | 0:53753690a8bf | 419 | XI_UNUSED( data_layer ); |
xively | 0:53753690a8bf | 420 | |
xively | 0:53753690a8bf | 421 | static xi_response_t __tmp; |
xively | 0:53753690a8bf | 422 | static http_response_t* __response = &__tmp.http; |
xively | 0:53753690a8bf | 423 | |
xively | 0:53753690a8bf | 424 | // just pass it further |
xively | 0:53753690a8bf | 425 | if( parse_http( __response, response ) == 0 ) |
xively | 0:53753690a8bf | 426 | { |
xively | 0:53753690a8bf | 427 | return 0; |
xively | 0:53753690a8bf | 428 | } |
xively | 0:53753690a8bf | 429 | |
xively | 0:53753690a8bf | 430 | // pass it to the data_layer |
xively | 0:53753690a8bf | 431 | return &__tmp; |
xively | 0:53753690a8bf | 432 | } |