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