lib/curl: implement `CurlResponseFailed::to_s`
[nit.git] / lib / curl / curl.nit
index b77b0f1..7b1918c 100644 (file)
@@ -19,11 +19,20 @@ module curl
 
 import native_curl
 
+redef class Sys
+       # Shared Curl library handle
+       #
+       # Usually, you do not have to use this attribute, it instancied by `CurlHTTPRequest` and `CurlMail`.
+       # But in some cases you may want to finalize it to free some small resources.
+       # However, if Curl services are needed once again, this attribute must be manually set.
+       var curl: Curl = new Curl is lazy, writable
+end
+
 # Curl library handle, it is initialized and released with this class
 class Curl
        super FinalizableOnce
 
-       protected var native = new NativeCurl.easy_init
+       private var native = new NativeCurl.easy_init
 
        # Check for correct initialization
        fun is_ok: Bool do return self.native.is_init
@@ -34,14 +43,11 @@ end
 # CURL Request
 class CurlRequest
 
-       private var curl: Curl
+       private var curl: Curl = sys.curl
 
        # Shall this request be verbose?
        var verbose: Bool = false is writable
 
-       # Launch request method
-       fun execute: CurlResponse is abstract
-
        # Intern perform method, lowest level of request launching
        private fun perform: nullable CurlResponseFailed
        do
@@ -82,7 +88,7 @@ class CurlHTTPRequest
        end
 
        # Execute HTTP request with settings configured through attribute
-       redef fun execute
+       fun execute: CurlResponse
        do
                if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
 
@@ -268,7 +274,7 @@ class CurlMail
        end
 
        # Execute Mail request with settings configured through attribute
-       redef fun execute
+       fun execute: nullable CurlResponseFailed
        do
                if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
 
@@ -343,7 +349,7 @@ class CurlMail
                var err_resp = perform
                if err_resp != null then return err_resp
 
-               return new CurlMailResponseSuccess
+               return null
        end
 end
 
@@ -362,6 +368,8 @@ class CurlResponseFailed
 
        var error_code: Int
        var error_msg: String
+
+       redef fun to_s do return "{error_msg} ({error_code})"
 end
 
 # Success Abstract Response Success Class
@@ -395,11 +403,6 @@ class CurlResponseSuccess
        end
 end
 
-# Success Response Class of mail request
-class CurlMailResponseSuccess
-       super CurlResponseSuccessIntern
-end
-
 # Success Response Class of a downloaded File
 class CurlFileResponseSuccess
        super CurlResponseSuccessIntern