-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvch.v
59 lines (52 loc) · 1.03 KB
/
vch.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module vch
import os
import flag
import net.http
import v.vmod
pub struct Client {
pub:
api string = 'http://localhost:8123'
auth string
format string = 'JSONEachRow'
}
pub fn new_client(api string, auth string) Client {
return Client{
api: api
auth: auth
}
}
fn (d Client) ping() string {
mut url := '$d.api/ping'
if utf8_str_len(d.ch_auth) > 8 {
auth := d.ch_auth.split(':')
tmp := url.split('://')
if tmp[1].len > 0 {
url = url + '&password=' + auth[1]
}
}
resp := http.get(url) or {
println('failed to fetch data from the server')
return ''
}
return resp.text
}
fn (d Client) exec(query string) string {
mut url := '$d.api'
mut q := '$query'
if utf8_str_len(d.format) > 0 {
q = q + ' FORMAT ' + d.format
}
url = url + '/?query=$q'
if utf8_str_len(d.ch_auth) > 8 {
auth := d.ch_auth.split(':')
tmp := url.split('://')
if tmp[1].len > 0 {
url = url + '&password=' + auth[1]
}
}
resp := http.get(url) or {
println('failed to fetch data from the server')
return ''
}
return resp.text
}