Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: xively-jumpstart-demo
http_layer_queries.c
00001 // Copyright (c) 2003-2013, LogMeIn, Inc. All rights reserved. 00002 // This is part of Xively C library, it is under the BSD 3-Clause license. 00003 00004 /** 00005 * \file http_layer_queries.h 00006 * \author Olgierd Humenczuk 00007 * \brief Helpers for making HTTP requests (specific to Xively REST/HTTP API) [see http_layer_queries.h] 00008 */ 00009 00010 #include <stdio.h> 00011 #include <assert.h> 00012 00013 #include "http_layer_queries.h" 00014 #include "xi_macros.h" 00015 #include "xi_err.h" 00016 #include "xi_consts.h" 00017 00018 00019 static const char XI_HTTP_TEMPLATE_FEED[] = "%s /v2/feeds%s.csv%s HTTP/1.1\r\n" 00020 "Host: %s\r\n" 00021 "User-Agent: %s\r\n" 00022 "Accept: */*\r\n" 00023 "X-ApiKey: %s\r\n"; 00024 00025 static const char XI_HTTP_ID_TEMPLATE[] = "/%s"; 00026 static const char XI_HTTP_ID_TEMPLATE_D[] = "/%d"; 00027 00028 static const char XI_HTTP_CONTENT_TEMPLATE[] = "Content-Type: text/plain\r\n" 00029 "Content-Length: %d\r\n"; 00030 00031 static char XI_QUERY_BUFFER[ XI_QUERY_BUFFER_SIZE ]; 00032 static char XI_CONTENT_BUFFER[ XI_CONTENT_BUFFER_SIZE ]; 00033 static char XI_ID_BUFFER[ XI_ID_BUFFER_SIZE ]; 00034 00035 inline static int http_construct_string( 00036 char* buffer, size_t buffer_size 00037 , const char* string_id ) 00038 { 00039 // PRECONDITIONS 00040 assert( buffer != 0 ); 00041 assert( buffer_size != 0 ); 00042 00043 int s = 0; 00044 int size = buffer_size; 00045 00046 if( string_id ) 00047 { 00048 s = snprintf( buffer, size 00049 , XI_HTTP_ID_TEMPLATE, string_id ); 00050 00051 XI_CHECK_SIZE( s, size, XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN ); 00052 } 00053 00054 return s; 00055 00056 err_handling: 00057 return -1; 00058 } 00059 00060 inline static int http_construct_int( 00061 char* buffer, size_t buffer_size 00062 , const int32_t* int_id ) 00063 { 00064 // PRECONDITIONS 00065 assert( buffer != 0 ); 00066 assert( buffer_size != 0 ); 00067 00068 int s = 0; 00069 int size = buffer_size; 00070 00071 if( int_id ) 00072 { 00073 s = snprintf( buffer, size 00074 , XI_HTTP_ID_TEMPLATE_D, *int_id ); 00075 00076 XI_CHECK_SIZE( s, size, XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN ) 00077 } 00078 00079 return s; 00080 00081 err_handling: 00082 return -1; 00083 } 00084 00085 inline static int http_construct_feed( 00086 char* buffer, size_t buffer_size 00087 , const int32_t* feed_id ) 00088 { 00089 // PRECONDITIONS 00090 assert( buffer != 0 ); 00091 assert( buffer_size != 0 ); 00092 00093 if( feed_id ) 00094 { 00095 return http_construct_int( buffer 00096 , buffer_size, feed_id ); 00097 } 00098 00099 return 0; 00100 } 00101 00102 inline static int http_construct_datastream( 00103 char* buffer, size_t buffer_size 00104 , const int32_t* feed_id 00105 , const char* datastream_id ) 00106 { 00107 // PRECONDITIONS 00108 assert( buffer != 0 ); 00109 assert( buffer_size != 0 ); 00110 00111 int s = http_construct_feed( buffer, buffer_size, feed_id ); 00112 int offset = 0; 00113 int size = buffer_size; 00114 00115 XI_CHECK_S( s, size, offset, XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN ); 00116 00117 s = http_construct_string( buffer + offset, buffer_size - offset 00118 , "datastreams" ); 00119 00120 XI_CHECK_S( s, size, offset, XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN ); 00121 00122 if( datastream_id ) 00123 { 00124 return http_construct_string( buffer + offset, buffer_size - offset 00125 , datastream_id == 0 ? "" : datastream_id ); 00126 } 00127 00128 return offset; 00129 00130 err_handling: 00131 return -1; 00132 } 00133 00134 inline static int http_construct_datapoint( 00135 char* buffer, size_t buffer_size 00136 , const int32_t* feed_id 00137 , const char* datastream_id 00138 , const char* datapoint ) 00139 { 00140 // PRECONDITIONS 00141 assert( buffer != 0 ); 00142 assert( buffer_size != 0 ); 00143 00144 int s = http_construct_datastream( buffer, buffer_size 00145 , feed_id, datastream_id ); 00146 int offset = 0; 00147 int size = buffer_size; 00148 00149 XI_CHECK_S( s, size, offset 00150 , XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN ); 00151 00152 s = http_construct_string( buffer + offset, buffer_size - offset 00153 , "datapoints" ); 00154 00155 XI_CHECK_S( s, size, offset 00156 , XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN ); 00157 00158 if( datapoint ) 00159 { 00160 return http_construct_string( buffer + offset, buffer_size - offset 00161 , datapoint == 0 ? "" : datapoint ); 00162 } 00163 00164 return offset; 00165 00166 err_handling: 00167 return -1; 00168 } 00169 00170 inline static const char* http_construct_http_query( 00171 const char* http_method 00172 , const char* id 00173 , const char* query_suffix 00174 , const char* x_api_key ) 00175 { 00176 // PRECONDITIONS 00177 assert( http_method != 0 ); 00178 assert( x_api_key != 0 ); 00179 00180 int s = snprintf( XI_QUERY_BUFFER, XI_QUERY_BUFFER_SIZE, XI_HTTP_TEMPLATE_FEED 00181 , http_method, id == 0 ? "" : id, query_suffix == 0 ? "" : query_suffix 00182 , XI_HOST, XI_USER_AGENT, x_api_key ); 00183 00184 XI_CHECK_SIZE( s, XI_QUERY_BUFFER_SIZE 00185 , XI_HTTP_CONSTRUCT_REQUEST_BUFFER_OVERRUN ); 00186 00187 return XI_QUERY_BUFFER; 00188 00189 err_handling: 00190 return 0; 00191 } 00192 00193 const char* http_construct_request_datapoint( 00194 const char* http_method 00195 , const int32_t* feed_id 00196 , const char* datastream 00197 , const char* datapoint 00198 , const char* x_api_key ) 00199 { 00200 // PRECONDITIONS 00201 assert( http_method != 0 ); 00202 assert( feed_id != 0 ); 00203 assert( datastream != 0 ); 00204 assert( x_api_key != 0 ); 00205 00206 int s = http_construct_datapoint( XI_ID_BUFFER, XI_ID_BUFFER_SIZE 00207 , feed_id, datastream, datapoint ); 00208 00209 XI_CHECK_SIZE( s, XI_ID_BUFFER_SIZE 00210 , XI_HTTP_CONSTRUCT_CONTENT_BUFFER_OVERRUN ); 00211 00212 return http_construct_http_query( http_method, XI_ID_BUFFER, 0, x_api_key ); 00213 00214 err_handling: 00215 return 0; 00216 } 00217 00218 const char* http_construct_request_datastream( 00219 const char* http_method 00220 , const int32_t* feed_id 00221 , const char* datastream 00222 , const char* x_api_key ) 00223 { 00224 // PRECONDITIONS 00225 assert( http_method != 0 ); 00226 assert( feed_id != 0 ); 00227 assert( x_api_key != 0 ); 00228 00229 int s = http_construct_datastream( XI_ID_BUFFER, XI_ID_BUFFER_SIZE 00230 , feed_id, datastream ); 00231 00232 XI_CHECK_SIZE( s, XI_ID_BUFFER_SIZE 00233 , XI_HTTP_CONSTRUCT_CONTENT_BUFFER_OVERRUN ) 00234 00235 return http_construct_http_query( http_method, XI_ID_BUFFER, 0, x_api_key ); 00236 00237 err_handling: 00238 return 0; 00239 } 00240 00241 const char* http_construct_request_feed( 00242 const char* http_method 00243 , const int32_t* feed_id 00244 , const char* x_api_key 00245 , const char* query_suffix ) 00246 { 00247 // PRECONDITIONS 00248 assert( http_method != 0 ); 00249 assert( x_api_key != 0 ); 00250 00251 int s = http_construct_feed( XI_ID_BUFFER, XI_ID_BUFFER_SIZE, feed_id ); 00252 00253 XI_CHECK_SIZE( s, XI_ID_BUFFER_SIZE 00254 , XI_HTTP_CONSTRUCT_CONTENT_BUFFER_OVERRUN ); 00255 00256 return http_construct_http_query( http_method, XI_ID_BUFFER, query_suffix, x_api_key ); 00257 00258 err_handling: 00259 return 0; 00260 } 00261 00262 const char* http_construct_content( 00263 int32_t content_size ) 00264 { 00265 00266 int s = snprintf( XI_CONTENT_BUFFER, XI_CONTENT_BUFFER_SIZE 00267 , XI_HTTP_CONTENT_TEMPLATE, content_size ); 00268 00269 XI_CHECK_SIZE( s, XI_CONTENT_BUFFER_SIZE 00270 , XI_HTTP_CONSTRUCT_CONTENT_BUFFER_OVERRUN ); 00271 00272 return XI_CONTENT_BUFFER; 00273 00274 err_handling: 00275 return 0; 00276 }
Generated on Wed Jul 13 2022 17:00:32 by
1.7.2