test

Fork of mbed-libxively-6eca970 by Xively Official

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?

UserRevisionLine numberNew 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_layer_queries.h
xively 0:82702e998d3f 6 * \author Olgierd Humenczuk
xively 0:82702e998d3f 7 * \brief Helpers for making HTTP requests (specific to Xively REST/HTTP API) [see http_layer_queries.h]
xively 0:82702e998d3f 8 */
xively 0:82702e998d3f 9
xively 0:82702e998d3f 10 #include <stdio.h>
xively 0:82702e998d3f 11 #include <assert.h>
xively 0:82702e998d3f 12
xively 0:82702e998d3f 13 #include "http_layer_queries.h"
xively 0:82702e998d3f 14 #include "xi_macros.h"
xively 0:82702e998d3f 15 #include "xi_err.h"
xively 0:82702e998d3f 16 #include "xi_consts.h"
xively 0:82702e998d3f 17
xively 0:82702e998d3f 18
xively 0:82702e998d3f 19 static const char XI_HTTP_TEMPLATE_FEED[] = "%s /v2/feeds%s.csv%s HTTP/1.1\r\n"
xively 0:82702e998d3f 20 "Host: %s\r\n"
xively 0:82702e998d3f 21 "User-Agent: %s\r\n"
xively 0:82702e998d3f 22 "Accept: */*\r\n"
xively 0:82702e998d3f 23 "X-ApiKey: %s\r\n";
xively 0:82702e998d3f 24
xively 0:82702e998d3f 25 static const char XI_HTTP_ID_TEMPLATE[] = "/%s";
xively 0:82702e998d3f 26 static const char XI_HTTP_ID_TEMPLATE_D[] = "/%d";
xively 0:82702e998d3f 27
xively 0:82702e998d3f 28 static const char XI_HTTP_CONTENT_TEMPLATE[] = "Content-Type: text/plain\r\n"
xively 0:82702e998d3f 29 "Content-Length: %d\r\n";
xively 0:82702e998d3f 30
xively 0:82702e998d3f 31 static char XI_QUERY_BUFFER[ XI_QUERY_BUFFER_SIZE ];
xively 0:82702e998d3f 32 static char XI_CONTENT_BUFFER[ XI_CONTENT_BUFFER_SIZE ];
xively 0:82702e998d3f 33 static char XI_ID_BUFFER[ XI_ID_BUFFER_SIZE ];
xively 0:82702e998d3f 34
xively 0:82702e998d3f 35 inline static int http_construct_string(
xively 0:82702e998d3f 36 char* buffer, size_t buffer_size
xively 0:82702e998d3f 37 , const char* string_id )
xively 0:82702e998d3f 38 {
xively 0:82702e998d3f 39 // PRECONDITIONS
xively 0:82702e998d3f 40 assert( buffer != 0 );
xively 0:82702e998d3f 41 assert( buffer_size != 0 );
xively 0:82702e998d3f 42
xively 0:82702e998d3f 43 int s = 0;
xively 0:82702e998d3f 44 int size = buffer_size;
xively 0:82702e998d3f 45
xively 0:82702e998d3f 46 if( string_id )
xively 0:82702e998d3f 47 {
xively 0:82702e998d3f 48 s = snprintf( buffer, size
xively 0:82702e998d3f 49 , XI_HTTP_ID_TEMPLATE, string_id );
xively 0:82702e998d3f 50
xively 0:82702e998d3f 51 XI_CHECK_SIZE( s, size, XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN );
xively 0:82702e998d3f 52 }
xively 0:82702e998d3f 53
xively 0:82702e998d3f 54 return s;
xively 0:82702e998d3f 55
xively 0:82702e998d3f 56 err_handling:
xively 0:82702e998d3f 57 return -1;
xively 0:82702e998d3f 58 }
xively 0:82702e998d3f 59
xively 0:82702e998d3f 60 inline static int http_construct_int(
xively 0:82702e998d3f 61 char* buffer, size_t buffer_size
xively 0:82702e998d3f 62 , const int32_t* int_id )
xively 0:82702e998d3f 63 {
xively 0:82702e998d3f 64 // PRECONDITIONS
xively 0:82702e998d3f 65 assert( buffer != 0 );
xively 0:82702e998d3f 66 assert( buffer_size != 0 );
xively 0:82702e998d3f 67
xively 0:82702e998d3f 68 int s = 0;
xively 0:82702e998d3f 69 int size = buffer_size;
xively 0:82702e998d3f 70
xively 0:82702e998d3f 71 if( int_id )
xively 0:82702e998d3f 72 {
xively 0:82702e998d3f 73 s = snprintf( buffer, size
xively 0:82702e998d3f 74 , XI_HTTP_ID_TEMPLATE_D, *int_id );
xively 0:82702e998d3f 75
xively 0:82702e998d3f 76 XI_CHECK_SIZE( s, size, XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN )
xively 0:82702e998d3f 77 }
xively 0:82702e998d3f 78
xively 0:82702e998d3f 79 return s;
xively 0:82702e998d3f 80
xively 0:82702e998d3f 81 err_handling:
xively 0:82702e998d3f 82 return -1;
xively 0:82702e998d3f 83 }
xively 0:82702e998d3f 84
xively 0:82702e998d3f 85 inline static int http_construct_feed(
xively 0:82702e998d3f 86 char* buffer, size_t buffer_size
xively 0:82702e998d3f 87 , const int32_t* feed_id )
xively 0:82702e998d3f 88 {
xively 0:82702e998d3f 89 // PRECONDITIONS
xively 0:82702e998d3f 90 assert( buffer != 0 );
xively 0:82702e998d3f 91 assert( buffer_size != 0 );
xively 0:82702e998d3f 92
xively 0:82702e998d3f 93 if( feed_id )
xively 0:82702e998d3f 94 {
xively 0:82702e998d3f 95 return http_construct_int( buffer
xively 0:82702e998d3f 96 , buffer_size, feed_id );
xively 0:82702e998d3f 97 }
xively 0:82702e998d3f 98
xively 0:82702e998d3f 99 return 0;
xively 0:82702e998d3f 100 }
xively 0:82702e998d3f 101
xively 0:82702e998d3f 102 inline static int http_construct_datastream(
xively 0:82702e998d3f 103 char* buffer, size_t buffer_size
xively 0:82702e998d3f 104 , const int32_t* feed_id
xively 0:82702e998d3f 105 , const char* datastream_id )
xively 0:82702e998d3f 106 {
xively 0:82702e998d3f 107 // PRECONDITIONS
xively 0:82702e998d3f 108 assert( buffer != 0 );
xively 0:82702e998d3f 109 assert( buffer_size != 0 );
xively 0:82702e998d3f 110
xively 0:82702e998d3f 111 int s = http_construct_feed( buffer, buffer_size, feed_id );
xively 0:82702e998d3f 112 int offset = 0;
xively 0:82702e998d3f 113 int size = buffer_size;
xively 0:82702e998d3f 114
xively 0:82702e998d3f 115 XI_CHECK_S( s, size, offset, XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN );
xively 0:82702e998d3f 116
xively 0:82702e998d3f 117 s = http_construct_string( buffer + offset, buffer_size - offset
xively 0:82702e998d3f 118 , "datastreams" );
xively 0:82702e998d3f 119
xively 0:82702e998d3f 120 XI_CHECK_S( s, size, offset, XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN );
xively 0:82702e998d3f 121
xively 0:82702e998d3f 122 if( datastream_id )
xively 0:82702e998d3f 123 {
xively 0:82702e998d3f 124 return http_construct_string( buffer + offset, buffer_size - offset
xively 0:82702e998d3f 125 , datastream_id == 0 ? "" : datastream_id );
xively 0:82702e998d3f 126 }
xively 0:82702e998d3f 127
xively 0:82702e998d3f 128 return offset;
xively 0:82702e998d3f 129
xively 0:82702e998d3f 130 err_handling:
xively 0:82702e998d3f 131 return -1;
xively 0:82702e998d3f 132 }
xively 0:82702e998d3f 133
xively 0:82702e998d3f 134 inline static int http_construct_datapoint(
xively 0:82702e998d3f 135 char* buffer, size_t buffer_size
xively 0:82702e998d3f 136 , const int32_t* feed_id
xively 0:82702e998d3f 137 , const char* datastream_id
xively 0:82702e998d3f 138 , const char* datapoint )
xively 0:82702e998d3f 139 {
xively 0:82702e998d3f 140 // PRECONDITIONS
xively 0:82702e998d3f 141 assert( buffer != 0 );
xively 0:82702e998d3f 142 assert( buffer_size != 0 );
xively 0:82702e998d3f 143
xively 0:82702e998d3f 144 int s = http_construct_datastream( buffer, buffer_size
xively 0:82702e998d3f 145 , feed_id, datastream_id );
xively 0:82702e998d3f 146 int offset = 0;
xively 0:82702e998d3f 147 int size = buffer_size;
xively 0:82702e998d3f 148
xively 0:82702e998d3f 149 XI_CHECK_S( s, size, offset
xively 0:82702e998d3f 150 , XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN );
xively 0:82702e998d3f 151
xively 0:82702e998d3f 152 s = http_construct_string( buffer + offset, buffer_size - offset
xively 0:82702e998d3f 153 , "datapoints" );
xively 0:82702e998d3f 154
xively 0:82702e998d3f 155 XI_CHECK_S( s, size, offset
xively 0:82702e998d3f 156 , XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN );
xively 0:82702e998d3f 157
xively 0:82702e998d3f 158 if( datapoint )
xively 0:82702e998d3f 159 {
xively 0:82702e998d3f 160 return http_construct_string( buffer + offset, buffer_size - offset
xively 0:82702e998d3f 161 , datapoint == 0 ? "" : datapoint );
xively 0:82702e998d3f 162 }
xively 0:82702e998d3f 163
xively 0:82702e998d3f 164 return offset;
xively 0:82702e998d3f 165
xively 0:82702e998d3f 166 err_handling:
xively 0:82702e998d3f 167 return -1;
xively 0:82702e998d3f 168 }
xively 0:82702e998d3f 169
xively 0:82702e998d3f 170 inline static const char* http_construct_http_query(
xively 0:82702e998d3f 171 const char* http_method
xively 0:82702e998d3f 172 , const char* id
xively 0:82702e998d3f 173 , const char* query_suffix
xively 0:82702e998d3f 174 , const char* x_api_key )
xively 0:82702e998d3f 175 {
xively 0:82702e998d3f 176 // PRECONDITIONS
xively 0:82702e998d3f 177 assert( http_method != 0 );
xively 0:82702e998d3f 178 assert( x_api_key != 0 );
xively 0:82702e998d3f 179
xively 0:82702e998d3f 180 int s = snprintf( XI_QUERY_BUFFER, XI_QUERY_BUFFER_SIZE, XI_HTTP_TEMPLATE_FEED
xively 0:82702e998d3f 181 , http_method, id == 0 ? "" : id, query_suffix == 0 ? "" : query_suffix
xively 0:82702e998d3f 182 , XI_HOST, XI_USER_AGENT, x_api_key );
xively 0:82702e998d3f 183
xively 0:82702e998d3f 184 XI_CHECK_SIZE( s, XI_QUERY_BUFFER_SIZE
xively 0:82702e998d3f 185 , XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN );
xively 0:82702e998d3f 186
xively 0:82702e998d3f 187 return XI_QUERY_BUFFER;
xively 0:82702e998d3f 188
xively 0:82702e998d3f 189 err_handling:
xively 0:82702e998d3f 190 return 0;
xively 0:82702e998d3f 191 }
xively 0:82702e998d3f 192
xively 0:82702e998d3f 193 const char* http_construct_request_datapoint(
xively 0:82702e998d3f 194 const char* http_method
xively 0:82702e998d3f 195 , const int32_t* feed_id
xively 0:82702e998d3f 196 , const char* datastream
xively 0:82702e998d3f 197 , const char* datapoint
xively 0:82702e998d3f 198 , const char* x_api_key )
xively 0:82702e998d3f 199 {
xively 0:82702e998d3f 200 // PRECONDITIONS
xively 0:82702e998d3f 201 assert( http_method != 0 );
xively 0:82702e998d3f 202 assert( feed_id != 0 );
xively 0:82702e998d3f 203 assert( datastream != 0 );
xively 0:82702e998d3f 204 assert( x_api_key != 0 );
xively 0:82702e998d3f 205
xively 0:82702e998d3f 206 int s = http_construct_datapoint( XI_ID_BUFFER, XI_ID_BUFFER_SIZE
xively 0:82702e998d3f 207 , feed_id, datastream, datapoint );
xively 0:82702e998d3f 208
xively 0:82702e998d3f 209 XI_CHECK_SIZE( s, XI_ID_BUFFER_SIZE
xively 0:82702e998d3f 210 , XI_HTTP_CONSTRUCT_CONTENT_BUFFER_OVERRUN );
xively 0:82702e998d3f 211
xively 0:82702e998d3f 212 return http_construct_http_query( http_method, XI_ID_BUFFER, 0, x_api_key );
xively 0:82702e998d3f 213
xively 0:82702e998d3f 214 err_handling:
xively 0:82702e998d3f 215 return 0;
xively 0:82702e998d3f 216 }
xively 0:82702e998d3f 217
xively 0:82702e998d3f 218 const char* http_construct_request_datastream(
xively 0:82702e998d3f 219 const char* http_method
xively 0:82702e998d3f 220 , const int32_t* feed_id
xively 0:82702e998d3f 221 , const char* datastream
xively 0:82702e998d3f 222 , const char* x_api_key )
xively 0:82702e998d3f 223 {
xively 0:82702e998d3f 224 // PRECONDITIONS
xively 0:82702e998d3f 225 assert( http_method != 0 );
xively 0:82702e998d3f 226 assert( feed_id != 0 );
xively 0:82702e998d3f 227 assert( x_api_key != 0 );
xively 0:82702e998d3f 228
xively 0:82702e998d3f 229 int s = http_construct_datastream( XI_ID_BUFFER, XI_ID_BUFFER_SIZE
xively 0:82702e998d3f 230 , feed_id, datastream );
xively 0:82702e998d3f 231
xively 0:82702e998d3f 232 XI_CHECK_SIZE( s, XI_ID_BUFFER_SIZE
xively 0:82702e998d3f 233 , XI_HTTP_CONSTRUCT_CONTENT_BUFFER_OVERRUN )
xively 0:82702e998d3f 234
xively 0:82702e998d3f 235 return http_construct_http_query( http_method, XI_ID_BUFFER, 0, x_api_key );
xively 0:82702e998d3f 236
xively 0:82702e998d3f 237 err_handling:
xively 0:82702e998d3f 238 return 0;
xively 0:82702e998d3f 239 }
xively 0:82702e998d3f 240
xively 0:82702e998d3f 241 const char* http_construct_request_feed(
xively 0:82702e998d3f 242 const char* http_method
xively 0:82702e998d3f 243 , const int32_t* feed_id
xively 0:82702e998d3f 244 , const char* x_api_key
xively 0:82702e998d3f 245 , const char* query_suffix )
xively 0:82702e998d3f 246 {
xively 0:82702e998d3f 247 // PRECONDITIONS
xively 0:82702e998d3f 248 assert( http_method != 0 );
xively 0:82702e998d3f 249 assert( x_api_key != 0 );
xively 0:82702e998d3f 250
xively 0:82702e998d3f 251 int s = http_construct_feed( XI_ID_BUFFER, XI_ID_BUFFER_SIZE, feed_id );
xively 0:82702e998d3f 252
xively 0:82702e998d3f 253 XI_CHECK_SIZE( s, XI_ID_BUFFER_SIZE
xively 0:82702e998d3f 254 , XI_HTTP_CONSTRUCT_CONTENT_BUFFER_OVERRUN );
xively 0:82702e998d3f 255
xively 0:82702e998d3f 256 return http_construct_http_query( http_method, XI_ID_BUFFER, query_suffix, x_api_key );
xively 0:82702e998d3f 257
xively 0:82702e998d3f 258 err_handling:
xively 0:82702e998d3f 259 return 0;
xively 0:82702e998d3f 260 }
xively 0:82702e998d3f 261
xively 0:82702e998d3f 262 const char* http_construct_content(
xively 0:82702e998d3f 263 int32_t content_size )
xively 0:82702e998d3f 264 {
xively 0:82702e998d3f 265
xively 0:82702e998d3f 266 int s = snprintf( XI_CONTENT_BUFFER, XI_CONTENT_BUFFER_SIZE
xively 0:82702e998d3f 267 , XI_HTTP_CONTENT_TEMPLATE, content_size );
xively 0:82702e998d3f 268
xively 0:82702e998d3f 269 XI_CHECK_SIZE( s, XI_CONTENT_BUFFER_SIZE
xively 0:82702e998d3f 270 , XI_HTTP_CONSTRUCT_CONTENT_BUFFER_OVERRUN );
xively 0:82702e998d3f 271
xively 0:82702e998d3f 272 return XI_CONTENT_BUFFER;
xively 0:82702e998d3f 273
xively 0:82702e998d3f 274 err_handling:
xively 0:82702e998d3f 275 return 0;
xively 0:82702e998d3f 276 }