From 473b2ce96a87ec2d6ce9d44ddedc3844d2145a98 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Fri, 11 Jan 2019 23:12:50 -0500 Subject: [PATCH] curl: intro shortcut methods `http_get` and `http_download` on `Text` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/curl/curl.nit | 4 ++- lib/curl/extra.nit | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 lib/curl/extra.nit diff --git a/lib/curl/curl.nit b/lib/curl/curl.nit index 63835eb..18f05c8 100644 --- a/lib/curl/curl.nit +++ b/lib/curl/curl.nit @@ -16,7 +16,9 @@ # Data transfer powered by the native curl library # -# Download or upload over HTTP with `CurlHTTPRequest` and send emails with `CurlMail`. +# Download or upload data over HTTP with `CurlHTTPRequest` and send emails +# with `CurlMail`. Scripts can use the easier (but limited) services on `Text`, +# `http_get` and `http_download`, provided by `curl::extra`. module curl import native_curl diff --git a/lib/curl/extra.nit b/lib/curl/extra.nit new file mode 100644 index 0000000..93548d8 --- /dev/null +++ b/lib/curl/extra.nit @@ -0,0 +1,89 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# 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 -- 1.7.9.5