1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

http: return ?string

This commit is contained in:
Delyan Angelov 2019-07-29 20:18:26 +03:00 committed by Alexander Medvednikov
parent ba6bcdb469
commit 4a1970a322
3 changed files with 32 additions and 20 deletions

View File

@ -5,7 +5,10 @@
import http import http
fn main() { fn main() {
html := http.get('https://news.ycombinator.com') html := http.get('https://news.ycombinator.com') or {
println('Failed fetching from URL')
return
}
mut pos := 0 mut pos := 0
for { for {
pos = html.index_after('https://', pos + 1) pos = html.index_after('https://', pos + 1)

View File

@ -33,7 +33,10 @@ fn (f mut Fetcher) fetch() {
id := f.ids[f.cursor] id := f.ids[f.cursor]
f.cursor++ f.cursor++
f.mu.unlock() f.mu.unlock()
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') or {
println('failed to fetch data from /v0/item/${id}.json')
exit(1)
}
story := json.decode(Story, resp) or { story := json.decode(Story, resp) or {
println('failed to decode a story') println('failed to decode a story')
exit(1) exit(1)
@ -48,9 +51,12 @@ fn (f mut Fetcher) fetch() {
// Fetches top HN stories in 8 coroutines // Fetches top HN stories in 8 coroutines
fn main() { fn main() {
resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json') resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json') or {
println('failed to fetch data from /v0/topstories.json')
return
}
ids := json.decode( []int, resp) or { ids := json.decode( []int, resp) or {
println('failed to fetch topstories.json') println('failed to decode topstories.json')
return return
} }
fetcher := &Fetcher{ids: ids} fetcher := &Fetcher{ids: ids}

View File

@ -28,23 +28,32 @@ pub:
} }
// embed 'http' // embed 'http'
pub fn get(url string) string { pub fn fetch(typ, url, data string) ?Response {
if url == '' { req := new_request('GET', url, '') or {
println('http: empty get url') return error(err)
return ''
} }
mut req := new_request('GET', url, '')
resp := req.do() resp := req.do()
return resp
}
pub fn get(url string) ?string {
resp := fetch('GET', url, '') or {
return error(err)
}
return resp.body return resp.body
} }
pub fn post(url, data string) string { pub fn post(url, data string) ?string {
req := new_request('POST', url, data) resp := fetch('POST', url, data) or {
resp := req.do() return error(err)
}
return resp.body return resp.body
} }
pub fn new_request(typ, _url, _data string) *Request { pub fn new_request(typ, _url, _data string) ?Request {
if _url == '' {
return error('http: empty url')
}
mut url := _url mut url := _url
mut data := _data mut data := _data
// req.headers['User-Agent'] = 'V $VERSION' // req.headers['User-Agent'] = 'V $VERSION'
@ -54,7 +63,7 @@ pub fn new_request(typ, _url, _data string) *Request {
data = '' data = ''
} }
// req.headers = new_map(0, sizeof(string))// []string{} // req.headers = new_map(0, sizeof(string))// []string{}
return &Request { return Request {
typ: typ typ: typ
url: url url: url
data: data data: data
@ -64,12 +73,6 @@ pub fn new_request(typ, _url, _data string) *Request {
} }
} }
/*
fn (req &Request) do() Response {
mut resp := Response{}
return resp
}
*/
fn (req mut Request) free() { fn (req mut Request) free() {
req.headers.free() req.headers.free()
} }