Permalink
Browse files

Callbacks are now in the form (err, data).

Changed the signature of the callbacks. Now they adhere to the Node.js
standard (err, ...)

fixes #941
  • Loading branch information...
1 parent 2282b1a commit a3b16f97d298e377e5fc2ff9ab47ab86ccd360e6 @ignacio committed Mar 26, 2012
View
@@ -199,7 +199,7 @@ end
---
+---
-- Requests an Unauthorized Request Token (http://tools.ietf.org/html/rfc5849#section-2.1)
-- @param arguments is an optional table with whose keys and values will be encoded as "application/x-www-form-urlencoded"
-- (when doing a POST) or encoded and sent in the query string (when doing a GET).
@@ -209,6 +209,7 @@ end
-- of an error. The callback is mandatory when running under LuaNode.
-- @return nothing if running under LuaNode (the callback will be called instead). Else it will return a
-- table containing the returned values from the server if succesfull or throws an error otherwise.
+--
function RequestToken(self, arguments, headers, callback)
if type(arguments) == "function" then
@@ -265,10 +266,14 @@ function RequestToken(self, arguments, headers, callback)
local oauth_instance = self
PerformRequestHelper(self, endpoint.url, endpoint.method, headers, arguments, post_body,
- function(ok, response_code, response_headers, response_status_line, response_body)
- if not ok or response_code ~= 200 then
+ function(err, response_code, response_headers, response_status_line, response_body)
+ if err then
+ callback(err)
+ return
+ end
+ if response_code ~= 200 then
-- can't do much, the responses are not standard
- callback(nil, response_code, response_headers, response_status_line, response_body)
+ callback({ status = response_code, headers = response_headers, status_line = response_status_line, body = response_body})
return
end
local values = {}
@@ -280,7 +285,7 @@ function RequestToken(self, arguments, headers, callback)
oauth_instance.m_oauth_token_secret = values.oauth_token_secret
oauth_instance.m_oauth_token = values.oauth_token
- callback(values)
+ callback(nil, values)
end)
end
end
@@ -426,11 +431,16 @@ function GetAccessToken(self, arguments, headers, callback)
local oauth_instance = self
PerformRequestHelper(self, endpoint.url, endpoint.method, headers, arguments, post_body,
- function(ok, response_code, response_headers, response_status_line, response_body)
+ function(err, response_code, response_headers, response_status_line, response_body)
- if not ok or response_code ~= 200 then
+ if err then
+ callback(err)
+ return
+ end
+
+ if response_code ~= 200 then
-- can't do much, the responses are not standard
- callback(nil, response_code, response_headers, response_status_line, response_body)
+ callback({ status = response_code, headers = response_headers, status_line = response_status_line, body = response_body})
return
end
local values = {}
@@ -442,7 +452,7 @@ function GetAccessToken(self, arguments, headers, callback)
oauth_instance.m_oauth_token_secret = values.oauth_token_secret
oauth_instance.m_oauth_token = values.oauth_token
- callback(values)
+ callback(nil, values)
end)
end
end
@@ -456,9 +466,10 @@ end
-- @param arguments is an optional table whose keys and values will be encoded as "application/x-www-form-urlencoded"
-- (when doing a POST) or encoded and sent in the query string (when doing a GET).
-- @param headers is an optional table with http headers to be sent in the request
--- @param callback is only required if running under LuaNode. It is a function to be called with the result of the request.
+-- @param callback is only required if running under LuaNode. It is a function to be called with an (optional) error object and the result of the request.
-- @return nothing if running under Luanode (the callback will be called instead). Else, the http status code
-- (a number), a table with the response headers, the status line and the response itself.
+--
function PerformRequest(self, method, url, arguments, headers, callback)
assert(type(method) == "string", "'method' must be a string")
method = method:upper()
@@ -482,10 +493,7 @@ function PerformRequest(self, method, url, arguments, headers, callback)
return response_code, response_headers, response_status_line, response_body
else
- PerformRequestHelper(self, url, method, headers, arguments, post_body,
- function(ok, response_code, response_headers, response_status_line, response_body)
- callback(response_code, response_headers, response_status_line, response_body)
- end)
+ PerformRequestHelper(self, url, method, headers, arguments, post_body, callback)
end
end
View
@@ -9,7 +9,7 @@ local table = table
module((...))
---
+---
-- Performs the actual http request, using LuaSocket or LuaSec (when using an https scheme)
-- @param url is the url to request
-- @param method is the http method (GET, POST, etc)
@@ -19,10 +19,12 @@ module((...))
-- @param post_body is a string with all parameters (custom + oauth ones) encoded. This is used when the OAuth provider
-- does not support the 'Authorization' header.
-- @param callback is a function to be called with the results of the request when they're available. The callback
--- receives the following arguments: a status (true or false), http status code, http response headers,
+-- receives the following arguments: an (optional) error object, http status code, http response headers,
-- http status line and the response body
--- In case of a connection error (host unreacheable, etc), the callback will be called with (false, error message,
--- error code)
+-- In case of a connection error (host unreacheable, etc), the callback will be called with
+-- { message = <error message>,
+-- code = <error code>
+-- }
--
function PerformRequestHelper (self, url, method, headers, arguments, post_body, callback)
-- arguments have already been sanitized
@@ -101,7 +103,7 @@ function PerformRequestHelper (self, url, method, headers, arguments, post_body,
local client = Http.createClient(parsedUrl.port, parsedUrl.host, secure)
client:on("error", function(self, err_msg, err_code)
- callback(false, err_msg, err_code)
+ callback({ code = err_code, message = err_msg })
end)
-- this sucks!
@@ -118,7 +120,7 @@ function PerformRequestHelper (self, url, method, headers, arguments, post_body,
end)
response:on("end", function(self)
- callback(true, response.statusCode, response.headers, tostring(response.statusCode), table.concat(request.__buffer))
+ callback(nil, response.statusCode, response.headers, tostring(response.statusCode), table.concat(request.__buffer))
end)
end)
@@ -10,20 +10,16 @@ function test()
AccessToken = "http://echo.lab.madgex.com/access-token.ashx"
})
print("Requesting token")
- client:RequestToken(function(values, status, headers, response_line, response_body)
- if not values then
- print(status)
- for k,v in pairs(headers) do print(k,v) end
- print(response_line)
- print(response_body)
- end
+ client:RequestToken(function(err, values)
+ assert_nil(err)
+
assert_table(values)
assert_equal("requestkey", values.oauth_token)
assert_equal("requestsecret", values.oauth_token_secret)
print("Retrieving access token")
- client:GetAccessToken(function(values)
-
+ client:GetAccessToken(function(err, values)
+
assert_table(values)
assert_equal("accesskey", values.oauth_token)
assert_equal("accesssecret", values.oauth_token_secret)
@@ -37,8 +33,13 @@ function test()
client:SetTokenSecret("accesssecret")
client:PerformRequest("GET", "http://echo.lab.madgex.com/echo.ashx",
{foo = "bar"},
- function(response_code, response_headers, response_status_line, response_body)
-
+ function(err, response_code, response_headers, response_status_line, response_body)
+ if err then
+ print("Error making request")
+ print(err.code)
+ print(err.message)
+ return
+ end
if response_code ~= 200 then
print("Error requesting token:", response_code)
for k,v in pairs(response_headers) do print(k,v) end
@@ -9,9 +9,11 @@ function test()
AccessToken = "http://127.0.0.1:12345/baz"
})
- local ok, err_msg = client:RequestToken(function(values, status, headers, response_line, response_body)
- assert(not values)
- assert_equal(process.constants.ECONNREFUSED, headers)
+ local ok, err_msg = client:RequestToken(function(err, values, status, headers, response_line, response_body)
+ assert(err)
+ assert_string(err.message) -- error message
+ assert_number(err.code) -- error code
+ assert_equal(process.constants.ECONNREFUSED, err.code)
end)
process:loop()
@@ -15,37 +15,18 @@ function test()
UseAuthHeaders = false
})
print("Requesting token")
- client:RequestToken(function(values, status, headers, response_line, response_body)
- if not values then
- print("Error requesting token:", status)
- for k,v in pairs(headers) do print(k,v) end
- print(response_line)
- print(response_body)
- end
+ client:RequestToken(function(err, values)
+ assert_nil(err)
+
assert_table(values)
- if not values.oauth_token then
- print("Error requesting token:", status)
- for k,v in pairs(values) do print(k,v) end
- os.exit()
- error("No oauth_token")
- end
assert_equal("requestkey", values.oauth_token)
assert_equal("requestsecret", values.oauth_token_secret)
print("Retrieving access token")
- client:GetAccessToken(function(values, status, headers, response_line, response_body)
- if not values then
- print("Error requesting token:", status)
- for k,v in pairs(headers) do print(k,v) end
- print(response_line)
- print(response_body)
- end
+ client:GetAccessToken(function(err, values)
+ assert_nil(err)
+
assert_table(values)
- if not values.oauth_token then
- print("Error requesting token:", status)
- for k,v in pairs(values) do print(k,v) end
- error("No oauth_token")
- end
assert_equal("accesskey", values.oauth_token)
assert_equal("accesssecret", values.oauth_token_secret)
@@ -60,7 +41,9 @@ function test()
})
client:PerformRequest("GET", "http://term.ie/oauth/example/echo_api.php", { foo="bar"},
- function(response_code, response_headers, response_status_line, response_body)
+ function(err, response_code, response_headers, response_status_line, response_body)
+ assert_nil(err)
+
if response_code ~= 200 then
print("Error requesting token:", response_code)
for k,v in pairs(response_headers) do print(k,v) end
View
@@ -0,0 +1,19 @@
+require "luarocks.require"
+
+local OAuth = require "OAuth"
+
+module(..., lunit.testcase, package.seeall)
+
+function test()
+ print("net error")
+ local client = OAuth.new("anonymous", "anonymous", {
+ RequestToken = "http://127.0.0.1:12345/foo",
+ AuthorizeUser = {"http://127.0.0.1:12345/bar", method = "GET"},
+ AccessToken = "http://127.0.0.1:12345/baz"
+ })
+
+ local ok, err_msg = client:RequestToken()
+ assert(not ok)
+ assert_equal("connection refused", err_msg)
+end
+

0 comments on commit a3b16f9

Please sign in to comment.