example: intro an emscripten wrapper around hello world
[nit.git] / lib / curl / curl.nit
index d3d3ca5..33b56a9 100644 (file)
@@ -34,29 +34,6 @@ class Curl
        # Check for correct initialization
        fun is_ok: Bool do return self.prim_curl.is_init
 
-       # Get an HTTP Request object to perform your own
-       fun http_request(url: String): nullable CurlRequest
-       do
-               var err
-               err = self.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
-               if not err.is_ok then return null
-
-               err = self.prim_curl.easy_setopt(new CURLOption.url, url)
-               if not err.is_ok then return null
-
-               return new CurlHTTPRequest(url, self)
-       end
-
-       # Get a MAIL Request Object
-       fun mail_request: nullable CurlMailRequest
-       do
-               var err: CURLCode
-               err = self.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
-               if not err.is_ok then return null
-
-               return new CurlMailRequest(self)
-       end
-
        # Release Curl instance
        fun destroy do self.prim_curl.easy_clean
 end
@@ -103,6 +80,12 @@ class CurlHTTPRequest
        var datas: nullable HeaderMap writable = null
        var headers: nullable HeaderMap writable = null
 
+       # Set the user agent for all following HTTP requests
+       fun user_agent=(name: String)
+       do
+               curl.prim_curl.easy_setopt(new CURLOption.user_agent, name)
+       end
+
        init (url: String, curl: nullable Curl)
        do
                self.url = url
@@ -120,6 +103,12 @@ class CurlHTTPRequest
 
                var err
 
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.url, url)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
                # Callbacks
                err = self.curl.prim_curl.register_callback(callback_receiver, new CURLCallbackType.header)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
@@ -159,6 +148,13 @@ class CurlHTTPRequest
                if self.delegate != null then callback_receiver = self.delegate.as(not null)
 
                var err
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.url, url)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
                err = self.curl.prim_curl.register_callback(callback_receiver, new CURLCallbackType.header)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
 
@@ -214,12 +210,11 @@ class CurlMailRequest
        var bcc: nullable Array[String] writable = null
        var subject: nullable String writable = ""
        var body: nullable String writable = ""
-       private var supported_outgoing_protocol: Array[String]
+       private var supported_outgoing_protocol: Array[String] = ["smtp", "smtps"]
 
        init (curl: nullable Curl)
        do
                self.curl = curl
-               self.supported_outgoing_protocol = once ["smtp", "smtps"]
        end
 
        # Helper method to add conventional space while building entire mail
@@ -299,6 +294,10 @@ class CurlMailRequest
                if g_rec.length < 1 then return answer_failure(0, "The mail recipients can not be empty")
 
                var err
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
                err = self.curl.prim_curl.easy_setopt(new CURLOption.mail_rcpt, g_rec.to_curlslist)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)