1 /** 2 This module implements utility functions for Influx API using vibe-d 3 4 Authors: Atila Neves (Kaleidic Associates Advisory Limited) 5 6 Generated documentation: 7 http://influxdb.code.kaleidic.io/influxdb.html 8 9 */ 10 module influxdb.vibe; 11 12 13 /// 14 void manage(in string url, in string str) { 15 vibePostQuery(url, "q=" ~ str); 16 } 17 18 /// 19 string query(in string url, in string db, in string query) { 20 return vibeGet(url, db, query); 21 } 22 23 /// 24 void write(in string url, in string db, in string line) { 25 vibePostWrite(url, db, line); 26 } 27 28 private string vibePostQuery(in string url, 29 in string query, 30 in string file = __FILE__, 31 in size_t line = __LINE__) { 32 return vibeHttpRequest(url ~ "/query", 33 cast(ubyte[])query.urlEncode, 34 file, 35 line); 36 } 37 38 private string vibePostWrite(in string url, in string db, in string str, 39 in string file = __FILE__, in size_t line = __LINE__) { 40 return vibeHttpRequest(url ~ "/write?db=" ~ db, cast(ubyte[])str, file, line); 41 } 42 43 44 private string vibeGet(in string url, in string db, in string arg, 45 in string file = __FILE__, in size_t line = __LINE__) { 46 47 import std.algorithm: map; 48 import std.array: array, join; 49 import std.range: chain; 50 51 const fullUrl = url ~ "/query?" ~ ["db=" ~ db, "q=" ~ arg].map!urlEncode.array.join("&"); 52 return vibeHttpRequest(fullUrl, [], file, line); 53 } 54 55 private string vibeHttpRequest(in string url, 56 in ubyte[] data, 57 in string file = __FILE__, 58 in size_t line = __LINE__) { 59 60 import vibe.http.client: requestHTTP, HTTPMethod; 61 import vibe.stream.operations: readAllUTF8; 62 63 string ret; 64 65 requestHTTP(url, 66 (scope req) { 67 68 req.contentType= "application/x-www-form-urlencoded"; 69 if(data.length) { 70 req.method = HTTPMethod.POST; 71 req.writeBody(data); 72 } 73 74 }, 75 (scope res) { 76 ret = res.bodyReader.readAllUTF8; 77 if(res.statusCode < 200 || res.statusCode > 299) 78 throw new Exception(ret, file, line); 79 } 80 ); 81 82 return ret; 83 84 } 85 86 /// 87 string urlEncode(in string str) { 88 import vibe.textfilter.urlencode: filterURLEncode; 89 import std.array: appender; 90 91 auto output = appender!(char[]); 92 const allowedChars = "="; 93 filterURLEncode(output, str, allowedChars); 94 return cast(string)output.data; 95 }