Update. Object wrapper int examples.
This commit is contained in:
parent
ef25a2f444
commit
fc7a570060
@ -4,8 +4,8 @@
|
|||||||
--
|
--
|
||||||
-- local e = cURL.easy{
|
-- local e = cURL.easy{
|
||||||
-- url = 'http://example.com',
|
-- url = 'http://example.com',
|
||||||
-- write = io.stdout;
|
-- writefunction = io.stdout;
|
||||||
-- header = function(msg) io.stderr:write(msg) end;
|
-- headerfunction = function(msg) io.stderr:write(msg) end;
|
||||||
-- }
|
-- }
|
||||||
-- e:perform()
|
-- e:perform()
|
||||||
--
|
--
|
||||||
@ -14,16 +14,8 @@ local curl = require "lcurl.safe"
|
|||||||
|
|
||||||
local easy = {} do
|
local easy = {} do
|
||||||
|
|
||||||
local LCURL_ERROR_EASY = 1
|
local LCURL_ERROR_EASY = curl.ERROR_EASY
|
||||||
local CURLE_UNKNOWN_OPTION = 48
|
local CURLE_UNKNOWN_OPTION = curl.E_UNKNOWN_OPTION
|
||||||
|
|
||||||
local RENAME_OPT = {
|
|
||||||
read = "readfunction";
|
|
||||||
write = "writefunction";
|
|
||||||
header = "headerfunction";
|
|
||||||
}
|
|
||||||
|
|
||||||
local postfields_storage = setmetatable({}, {__mode = "k"})
|
|
||||||
|
|
||||||
easy.__index = function(self, k)
|
easy.__index = function(self, k)
|
||||||
if k:sub(1, 4) == 'set_' then
|
if k:sub(1, 4) == 'set_' then
|
||||||
@ -43,7 +35,6 @@ easy.__index = function(self, k)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function setopt(handle, k, v, ...)
|
local function setopt(handle, k, v, ...)
|
||||||
k = RENAME_OPT[k] or k
|
|
||||||
local fn = handle["setopt_" .. k]
|
local fn = handle["setopt_" .. k]
|
||||||
if not fn then
|
if not fn then
|
||||||
return nil, curl.error(LCURL_ERROR_EASY, CURLE_UNKNOWN_OPTION)
|
return nil, curl.error(LCURL_ERROR_EASY, CURLE_UNKNOWN_OPTION)
|
||||||
@ -95,27 +86,15 @@ function easy:set(k, v, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function easy:perform(opt)
|
function easy:perform(opt)
|
||||||
|
if opt then
|
||||||
|
local ok, err = self:set(opt)
|
||||||
|
if not ok then return nil, err end
|
||||||
|
end
|
||||||
|
|
||||||
local e = self:handle()
|
local e = self:handle()
|
||||||
|
|
||||||
local oerror = opt.error or function(err) return nil, err end
|
|
||||||
|
|
||||||
if opt.read then
|
|
||||||
local ok, err = e:setopt_readfunction(opt.read)
|
|
||||||
if not ok then return oerror(err) end
|
|
||||||
end
|
|
||||||
|
|
||||||
if opt.write then
|
|
||||||
local ok, err = e:setopt_writefunction(opt.write)
|
|
||||||
if not ok then return oerror(err) end
|
|
||||||
end
|
|
||||||
|
|
||||||
if opt.header then
|
|
||||||
local ok, err = e:setopt_headerfunction(opt.header)
|
|
||||||
if not ok then return oerror(err) end
|
|
||||||
end
|
|
||||||
|
|
||||||
local ok, err = e:perform()
|
local ok, err = e:perform()
|
||||||
if ok then return oerror(err) end
|
if ok then return nil, err end
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user