mirror of
https://github.com/vlang/v.git
synced 2025-09-10 07:47:20 -04:00
examples: fix poll_coindesk_bitcoin_vs_usd_rate.v, use the new v2 API endpoint
This commit is contained in:
parent
95f00937bb
commit
dea01ffe03
@ -4,7 +4,19 @@ import time
|
|||||||
import x.json2
|
import x.json2
|
||||||
import net.http
|
import net.http
|
||||||
|
|
||||||
const url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
|
const url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot'
|
||||||
|
|
||||||
|
struct JsonResult {
|
||||||
|
mut:
|
||||||
|
data PriceResult
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PriceResult {
|
||||||
|
mut:
|
||||||
|
amount f64
|
||||||
|
base string
|
||||||
|
currency string
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
log.use_stdout()
|
log.use_stdout()
|
||||||
@ -16,13 +28,13 @@ fn main() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if data.status_code == 200 {
|
if data.status_code == 200 {
|
||||||
res := json2.decode[PriceResult](data.body) or {
|
res := json2.decode[JsonResult](data.body) or {
|
||||||
log.error('can not decode data: ${data.body}')
|
log.error('can not decode data: ${data.body}')
|
||||||
time.sleep(10 * time.second)
|
time.sleep(10 * time.second)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
new_rate := res.bpi['USD'].rate_float
|
new_rate := res.data.amount
|
||||||
show_result(i, res, new_rate - old_rate)
|
show_result(i, res.data, new_rate - old_rate)
|
||||||
old_rate = new_rate
|
old_rate = new_rate
|
||||||
}
|
}
|
||||||
time.sleep(3 * time.second)
|
time.sleep(3 * time.second)
|
||||||
@ -42,26 +54,5 @@ fn show_result(i u64, res PriceResult, delta f64) {
|
|||||||
term.red
|
term.red
|
||||||
}
|
}
|
||||||
cdelta := term.colorize(color, sdelta)
|
cdelta := term.colorize(color, sdelta)
|
||||||
log.info('${cdelta}, ${res.bpi['USD'].rate_float:10.3f} USD/BTC, ${res.time.updated_iso}, cycle: ${i:5}')
|
log.info('${cdelta}, ${res.amount:10.3f} USD/BTC, cycle: ${i:5}')
|
||||||
}
|
|
||||||
|
|
||||||
struct PriceTime {
|
|
||||||
updated_iso time.Time @[json: 'updatedISO']
|
|
||||||
updated string
|
|
||||||
updateduk string
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CurrencyResult {
|
|
||||||
code string
|
|
||||||
symbol string
|
|
||||||
rate string
|
|
||||||
description string
|
|
||||||
rate_float f64
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PriceResult {
|
|
||||||
time PriceTime
|
|
||||||
disclaimer string
|
|
||||||
chart_name string @[json: 'chartName']
|
|
||||||
bpi map[string]CurrencyResult
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user