net.http: simplify fetch()
parent
19abe8c750
commit
f8174c381c
|
@ -14,6 +14,7 @@ const (
|
||||||
// FetchConfig holds configurations of fetch
|
// FetchConfig holds configurations of fetch
|
||||||
pub struct FetchConfig {
|
pub struct FetchConfig {
|
||||||
pub mut:
|
pub mut:
|
||||||
|
url string
|
||||||
method Method
|
method Method
|
||||||
header Header
|
header Header
|
||||||
data string
|
data string
|
||||||
|
@ -40,12 +41,14 @@ pub fn new_request(method Method, url_ string, data string) ?Request {
|
||||||
|
|
||||||
// get sends a GET HTTP request to the URL
|
// get sends a GET HTTP request to the URL
|
||||||
pub fn get(url string) ?Response {
|
pub fn get(url string) ?Response {
|
||||||
return fetch_with_method(.get, url, FetchConfig{})
|
return fetch(method: .get, url: url)
|
||||||
}
|
}
|
||||||
|
|
||||||
// post sends a POST HTTP request to the URL with a string data
|
// post sends a POST HTTP request to the URL with a string data
|
||||||
pub fn post(url string, data string) ?Response {
|
pub fn post(url string, data string) ?Response {
|
||||||
return fetch_with_method(.post, url,
|
return fetch(
|
||||||
|
method: .post
|
||||||
|
url: url
|
||||||
data: data
|
data: data
|
||||||
header: new_header(key: .content_type, value: http.content_type_default)
|
header: new_header(key: .content_type, value: http.content_type_default)
|
||||||
)
|
)
|
||||||
|
@ -53,7 +56,9 @@ pub fn post(url string, data string) ?Response {
|
||||||
|
|
||||||
// post_json sends a POST HTTP request to the URL with a JSON data
|
// post_json sends a POST HTTP request to the URL with a JSON data
|
||||||
pub fn post_json(url string, data string) ?Response {
|
pub fn post_json(url string, data string) ?Response {
|
||||||
return fetch_with_method(.post, url,
|
return fetch(
|
||||||
|
method: .post
|
||||||
|
url: url
|
||||||
data: data
|
data: data
|
||||||
header: new_header(key: .content_type, value: 'application/json')
|
header: new_header(key: .content_type, value: 'application/json')
|
||||||
)
|
)
|
||||||
|
@ -61,7 +66,9 @@ pub fn post_json(url string, data string) ?Response {
|
||||||
|
|
||||||
// post_form sends a POST HTTP request to the URL with X-WWW-FORM-URLENCODED data
|
// post_form sends a POST HTTP request to the URL with X-WWW-FORM-URLENCODED data
|
||||||
pub fn post_form(url string, data map[string]string) ?Response {
|
pub fn post_form(url string, data map[string]string) ?Response {
|
||||||
return fetch_with_method(.post, url,
|
return fetch(
|
||||||
|
method: .post
|
||||||
|
url: url
|
||||||
header: new_header(key: .content_type, value: 'application/x-www-form-urlencoded')
|
header: new_header(key: .content_type, value: 'application/x-www-form-urlencoded')
|
||||||
data: url_encode_form_data(data)
|
data: url_encode_form_data(data)
|
||||||
)
|
)
|
||||||
|
@ -69,7 +76,9 @@ pub fn post_form(url string, data map[string]string) ?Response {
|
||||||
|
|
||||||
// put sends a PUT HTTP request to the URL with a string data
|
// put sends a PUT HTTP request to the URL with a string data
|
||||||
pub fn put(url string, data string) ?Response {
|
pub fn put(url string, data string) ?Response {
|
||||||
return fetch_with_method(.put, url,
|
return fetch(
|
||||||
|
method: .put
|
||||||
|
url: url
|
||||||
data: data
|
data: data
|
||||||
header: new_header(key: .content_type, value: http.content_type_default)
|
header: new_header(key: .content_type, value: http.content_type_default)
|
||||||
)
|
)
|
||||||
|
@ -77,7 +86,9 @@ pub fn put(url string, data string) ?Response {
|
||||||
|
|
||||||
// patch sends a PATCH HTTP request to the URL with a string data
|
// patch sends a PATCH HTTP request to the URL with a string data
|
||||||
pub fn patch(url string, data string) ?Response {
|
pub fn patch(url string, data string) ?Response {
|
||||||
return fetch_with_method(.patch, url,
|
return fetch(
|
||||||
|
method: .patch
|
||||||
|
url: url
|
||||||
data: data
|
data: data
|
||||||
header: new_header(key: .content_type, value: http.content_type_default)
|
header: new_header(key: .content_type, value: http.content_type_default)
|
||||||
)
|
)
|
||||||
|
@ -85,25 +96,24 @@ pub fn patch(url string, data string) ?Response {
|
||||||
|
|
||||||
// head sends a HEAD HTTP request to the URL
|
// head sends a HEAD HTTP request to the URL
|
||||||
pub fn head(url string) ?Response {
|
pub fn head(url string) ?Response {
|
||||||
return fetch_with_method(.head, url, FetchConfig{})
|
return fetch(method: .head, url: url)
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete sends a DELETE HTTP request to the URL
|
// delete sends a DELETE HTTP request to the URL
|
||||||
pub fn delete(url string) ?Response {
|
pub fn delete(url string) ?Response {
|
||||||
return fetch_with_method(.delete, url, FetchConfig{})
|
return fetch(method: .delete, url: url)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch sends an HTTP request to the URL with the given method and configurations
|
// fetch sends an HTTP request to the URL with the given method and configurations
|
||||||
pub fn fetch(_url string, config FetchConfig) ?Response {
|
pub fn fetch(config FetchConfig) ?Response {
|
||||||
if _url == '' {
|
if config.url == '' {
|
||||||
return error('http.fetch: empty url')
|
return error('http.fetch: empty url')
|
||||||
}
|
}
|
||||||
url := build_url_from_fetch(_url, config) or { return error('http.fetch: invalid url $_url') }
|
url := build_url_from_fetch(config) or { return error('http.fetch: invalid url $config.url') }
|
||||||
data := config.data
|
|
||||||
req := Request{
|
req := Request{
|
||||||
method: config.method
|
method: config.method
|
||||||
url: url
|
url: url
|
||||||
data: data
|
data: config.data
|
||||||
header: config.header
|
header: config.header
|
||||||
cookies: config.cookies
|
cookies: config.cookies
|
||||||
user_agent: config.user_agent
|
user_agent: config.user_agent
|
||||||
|
@ -116,7 +126,7 @@ pub fn fetch(_url string, config FetchConfig) ?Response {
|
||||||
|
|
||||||
// get_text sends a GET HTTP request to the URL and returns the text content of the response
|
// get_text sends a GET HTTP request to the URL and returns the text content of the response
|
||||||
pub fn get_text(url string) string {
|
pub fn get_text(url string) string {
|
||||||
resp := fetch(url, method: .get) or { return '' }
|
resp := fetch(url: url, method: .get) or { return '' }
|
||||||
return resp.text
|
return resp.text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,14 +141,15 @@ pub fn url_encode_form_data(data map[string]string) string {
|
||||||
return pieces.join('&')
|
return pieces.join('&')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_with_method(method Method, url string, _config FetchConfig) ?Response {
|
[deprecated: 'use fetch()']
|
||||||
|
fn fetch_with_method(method Method, _config FetchConfig) ?Response {
|
||||||
mut config := _config
|
mut config := _config
|
||||||
config.method = method
|
config.method = method
|
||||||
return fetch(url, config)
|
return fetch(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_url_from_fetch(_url string, config FetchConfig) ?string {
|
fn build_url_from_fetch(config FetchConfig) ?string {
|
||||||
mut url := urllib.parse(_url) ?
|
mut url := urllib.parse(config.url) ?
|
||||||
if config.params.len == 0 {
|
if config.params.len == 0 {
|
||||||
return url.str()
|
return url.str()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue