Update. Object wrapper int examples.
This commit is contained in:
parent
ef25a2f444
commit
fc7a570060
@ -3,9 +3,9 @@
|
|||||||
-- Usage:
|
-- Usage:
|
||||||
--
|
--
|
||||||
-- 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)
|
||||||
@ -71,7 +62,7 @@ function easy:new(opt)
|
|||||||
|
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
function easy:handle()
|
function easy:handle()
|
||||||
return self._handle
|
return self._handle
|
||||||
end
|
end
|
||||||
@ -93,33 +84,21 @@ function easy:set(k, v, ...)
|
|||||||
if not ok then return nil, err end
|
if not ok then return nil, err end
|
||||||
return self
|
return self
|
||||||
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
|
||||||
|
|
||||||
function easy:post(data)
|
function easy:post(data)
|
||||||
local post = curl.form()
|
local post = curl.form()
|
||||||
local ok, err
|
local ok, err
|
||||||
@ -150,7 +129,7 @@ function easy:post(data)
|
|||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function easy:get(k, ...)
|
function easy:get(k, ...)
|
||||||
assert(type(k) == "string")
|
assert(type(k) == "string")
|
||||||
local handle = self:handle()
|
local handle = self:handle()
|
||||||
@ -158,7 +137,7 @@ function easy:get(k, ...)
|
|||||||
if not fn then return nil, "Unknown info: " .. k end
|
if not fn then return nil, "Unknown info: " .. k end
|
||||||
return fn(handle, ...)
|
return fn(handle, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local cURL = {}
|
local cURL = {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user