curl :: native_curl
Binding of C libCurl which allow us to interact with network.core :: union_find
union–find algorithm using an efficient disjoint-set data structure
# Shortcut services for scripts: `http_get` and `http_download`
module extra
import curl
redef class Text
# Execute a simple HTTP GET request to the URL `self`
#
# Set `accept_status_code` to the expected response HTTP code, defaults to 200.
# If a different status code is received, the return code is printed to stderr.
#
# Returns the response body on success and `null` on error. Prints the error
# message to stderr.
#
# For more control, set HTTP request headers, keep the response status code
# and much more, use `CurlHTTPRequest`.
#
# ~~~nitish
# assert "http://example.com/".http_get != null
# ~~~
fun http_get(accept_status_code: nullable Int): nullable String
do
var req = new CurlHTTPRequest(self.to_s)
var resp = req.execute
req.close
if resp isa CurlResponseSuccess then
if resp.status_code == (accept_status_code or else 200) then
return resp.body_str
else
print_error "HTTP request failed: server returned {resp.status_code}"
end
else if resp isa CurlResponseFailed then
print_error "HTTP request failed: {resp.error_msg}"
else abort
return null
end
# Download the file at URL `self` to `output_path` with a simple HTTP request
#
# If not set, `output_path` defaults to `self.basename`.
#
# Set `accept_status_code` to the expected response HTTP code, defaults to 200.
# If a different status code is received, the return code is printed to stderr.
#
# Returns the path to the downloaded file on success and `null` on errors.
# Prints the error message to stderr.
#
# For more control, set HTTP request headers, keep the response status code
# and much more, use `CurlHTTPRequest`.
#
# ~~~nitish
# assert "http://example.com/".http_download("index.html") == "example.com"
# ~~~
fun http_download(output_path: nullable Text, accept_status_code: nullable Int): nullable String
do
var path = (output_path or else self.basename).to_s
var req = new CurlHTTPRequest(self.to_s)
var resp = req.download_to_file(path)
req.close
if resp isa CurlFileResponseSuccess then
if resp.status_code == (accept_status_code or else 200) then
return path
else
print_error "HTTP request failed: server returned {resp.status_code}"
end
else if resp isa CurlResponseFailed then
print_error "HTTP request failed: {resp.error_msg}"
else abort
return null
end
end
lib/curl/extra.nit:15,1--89,3