*: remove newly superfluous static types on attributes
[nit.git] / lib / curl / curl.nit
index cb3a132..57a2423 100644 (file)
@@ -14,7 +14,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Curl services: `CurlHTTPRequest` and `CurlMail`
+# Data transfer with URL syntax
+#
+# Download or upload over HTTP with `CurlHTTPRequest` and send emails with `CurlMail`.
 module curl
 
 import native_curl
@@ -71,20 +73,53 @@ class CurlRequest
        end
 end
 
-# CURL HTTP Request
+# HTTP request builder
+#
+# The request itself is sent by either `execute` or `download_to_file`.
+# The attributes of this class must be set before calling either of these two methods.
+#
+# ## Minimal usage example
+#
+# ~~~
+# var request = new CurlHTTPRequest("http://example.org/")
+# var response = request.execute
+# if response isa CurlResponseSuccess then
+#     print "Response status code: {response.status_code}"
+#     print response.body_str
+# else if response isa CurlResponseFailed then
+#     print_error response.error_msg
+# end
+# ~~~
 class CurlHTTPRequest
        super CurlRequest
        super NativeCurlCallbacks
 
+       # Address of the remote resource to request
        var url: String
-       var datas: nullable HeaderMap = null is writable
-       var headers: nullable HeaderMap = null is writable
-       var delegate: nullable CurlCallbacks = null is writable
+
+       # Data for the body of a POST request
+       var data: nullable HeaderMap is writable
+
+       # Raw body string
+       #
+       # Set this value to send raw data instead of the POST formatted `data`.
+       #
+       # If `data` is set, the body will not be sent.
+       var body: nullable String is writable
+
+       # Header content of the request
+       var headers: nullable HeaderMap is writable
+
+       # Delegates to customize the behavior when running `execute`
+       var delegate: nullable CurlCallbacks is writable
 
        # Set the user agent for all following HTTP requests
-       var user_agent: nullable String = null is writable
+       var user_agent: nullable String is writable
 
-       # Execute HTTP request with settings configured through attribute
+       # Execute HTTP request
+       #
+       # By default, the response body is returned in an instance of `CurlResponse`.
+       # This behavior can be customized by setting a custom `delegate`.
        fun execute: CurlResponse
        do
                if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
@@ -123,11 +158,14 @@ class CurlHTTPRequest
                end
 
                # Datas
-               var datas = self.datas
-               if datas != null then
-                       var postdatas = datas.to_url_encoded(self.curl)
+               var data = self.data
+               if data != null then
+                       var postdatas = data.to_url_encoded(self.curl)
                        err = self.curl.native.easy_setopt(new CURLOption.postfields, postdatas)
                        if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+               else if body != null then
+                       err = self.curl.native.easy_setopt(new CURLOption.postfields, body.as(not null))
+                       if not err.is_ok then return answer_failure(err.to_i, err.to_s)
                end
 
                var err_resp = perform
@@ -165,7 +203,7 @@ class CurlHTTPRequest
                if not output_file_name == null then
                        opt_name = output_file_name
                else if not self.url.substring(self.url.length-1, self.url.length) == "/" then
-                       opt_name = self.url.basename("")
+                       opt_name = self.url.basename
                else
                        return answer_failure(0, "Unable to extract file name, please specify one")
                end
@@ -255,7 +293,7 @@ class CurlMail
        # Protocols supported to send mail to a server
        #
        # Default value at `["smtp", "smtps"]`
-       var supported_outgoing_protocol: Array[String] = ["smtp", "smtps"]
+       var supported_outgoing_protocol = ["smtp", "smtps"]
 
        # Helper method to add pair values to mail content while building it (ex: "To:", "address@mail.com")
        private fun add_pair_to_content(str: String, att: String, val: nullable String): String