Simplify HTTP client API
* The :quote and :unquote methods are now plain functions and no longer receive the "self" parameter. This allows to directly use the utility functions provided by the HTTP client library. * Renamed CqHttpClient to plain "httclient". The variable itself is local to the script so it does not need to be unique.
This commit is contained in:
@@ -45,9 +45,6 @@ function API:__tostring()
|
||||
return self.__name .. "{" .. self.base_url .. "}"
|
||||
end
|
||||
|
||||
function API:_quote(string)
|
||||
return self._http:quote(string)
|
||||
end
|
||||
|
||||
function API:initial_sync(limit)
|
||||
return self:_send("GET", "/initialSync", { limit = limit or 1 })
|
||||
@@ -103,7 +100,7 @@ function API:create_room(options)
|
||||
end
|
||||
|
||||
function API:join_room(room_id_or_alias)
|
||||
return self:_send("POST", "/join/" .. self:_quote(room_id_or_alias))
|
||||
return self:_send("POST", "/join/" .. self._http.quote(room_id_or_alias))
|
||||
end
|
||||
|
||||
function API:event_stream(from_token, timeout)
|
||||
@@ -111,9 +108,10 @@ function API:event_stream(from_token, timeout)
|
||||
end
|
||||
|
||||
function API:send_state_event(room_id, event_type, content, state_key)
|
||||
local path = "/rooms/" .. self:_quote(room_id) .. "/state/" .. self:_quote(event_type)
|
||||
local path = "/rooms/" .. self._http.quote(room_id) ..
|
||||
"/state/" .. self._http.quote(event_type)
|
||||
if state_key then
|
||||
path = path .. "/" .. self:_quote(state_key)
|
||||
path = path .. "/" .. self._http.quote(state_key)
|
||||
end
|
||||
return self:_send("PUT", path, nil, content)
|
||||
end
|
||||
@@ -123,8 +121,9 @@ function API:send_message_event(room_id, event_type, content, txn_id)
|
||||
txn_id = self.txn_id
|
||||
self.txn_id = self.txn_id + 1
|
||||
end
|
||||
local path = "/rooms/" .. self:_quote(room_id) .. "/send/" ..
|
||||
self:_quote(event_type) .. "/" .. self:_quote(tostring(txn_id))
|
||||
local path = "/rooms/" .. self._http.quote(room_id) .. "/send/" ..
|
||||
self._http.quote(event_type) .. "/" ..
|
||||
self._http.quote(tostring(txn_id))
|
||||
return self:_send("PUT", path, nil, content)
|
||||
end
|
||||
|
||||
@@ -149,19 +148,19 @@ function API:send_notice(room_id, text_content)
|
||||
end
|
||||
|
||||
function API:get_room_name(room_id)
|
||||
return self:_send("GET", "/rooms/" .. self:_quote(room_id) .. "/state/m.room.name")
|
||||
return self:_send("GET", "/rooms/" .. self._http.quote(room_id) .. "/state/m.room.name")
|
||||
end
|
||||
|
||||
function API:get_room_topic(room_id)
|
||||
return self:_send("GET", "/rooms/" .. self:_quote(room_id) .. "/state/m.room.topic")
|
||||
return self:_send("GET", "/rooms/" .. self._http.quote(room_id) .. "/state/m.room.topic")
|
||||
end
|
||||
|
||||
function API:leave_room(room_id)
|
||||
return self:_send("POST", "/rooms/" .. self:_quote(room_id) .. "/leave")
|
||||
return self:_send("POST", "/rooms/" .. self._http.quote(room_id) .. "/leave")
|
||||
end
|
||||
|
||||
function API:invite_user(room_id, user_id)
|
||||
return self:_send("POST", "/rooms/" .. self:_quote(room_id) .. "/invite", nil,
|
||||
return self:_send("POST", "/rooms/" .. self._http.quote(room_id) .. "/invite", nil,
|
||||
{ user_id = user_id })
|
||||
end
|
||||
|
||||
@@ -170,17 +169,18 @@ function API:kick_user(room_id, user_id, reason)
|
||||
end
|
||||
|
||||
function API:set_membership(room_id, user_id, membership, reason)
|
||||
local path = "/rooms/" .. self:_quote(room_id) .. "/state/m.room.member/" .. self:_quote(user_id)
|
||||
local path = "/rooms/" .. self._http.quote(room_id) ..
|
||||
"/state/m.room.member/" .. self._http.quote(user_id)
|
||||
return self:_send("PUT", path, nil, { membership = membership, reason = reason or "" })
|
||||
end
|
||||
|
||||
function API:ban_user(room_id, user_id, reason)
|
||||
return self:_send("POST", "/rooms/" .. self:_quote(room_id) .. "/ban", nil,
|
||||
return self:_send("POST", "/rooms/" .. self._http.quote(room_id) .. "/ban", nil,
|
||||
{ user_id = user_id, reason = reason or "" })
|
||||
end
|
||||
|
||||
function API:get_room_state(room_id)
|
||||
return self:_send("GET", "/rooms/" .. self:_quote(room_id) .. "/state")
|
||||
return self:_send("GET", "/rooms/" .. self._http.quote(room_id) .. "/state")
|
||||
end
|
||||
|
||||
function API:get_text_body(text, msg_type)
|
||||
@@ -199,22 +199,22 @@ function API:media_upload(content, content_type)
|
||||
end
|
||||
|
||||
function API:get_display_name(user_id)
|
||||
local data = self:_send("GET", "/profile/" .. self:_quote(user_id) .. "/displayname")
|
||||
local data = self:_send("GET", "/profile/" .. self._http.quote(user_id) .. "/displayname")
|
||||
return data.displayname
|
||||
end
|
||||
|
||||
function API:set_display_name(user_id, display_name)
|
||||
return self:_send("PUT", "/profile/" .. self:_quote(user_id) .. "/displayname",
|
||||
return self:_send("PUT", "/profile/" .. self._http.quote(user_id) .. "/displayname",
|
||||
nil, { displayname = display_name })
|
||||
end
|
||||
|
||||
function API:get_avatar_url(user_id)
|
||||
local data = self:_send("GET", "/profile/" .. self:_quote(user_id) .. "/avatar_url")
|
||||
local data = self:_send("GET", "/profile/" .. self._http.quote(user_id) .. "/avatar_url")
|
||||
return data.avatar_url
|
||||
end
|
||||
|
||||
function API:set_avatar_url(user_id, avatar_url)
|
||||
return self:_send("PUT", "/profile/" .. self:_quote(user_id) .. "/avatar_url",
|
||||
return self:_send("PUT", "/profile/" .. self._http.quote(user_id) .. "/avatar_url",
|
||||
nil, { avatar_url = avatar_url })
|
||||
end
|
||||
|
||||
|
||||
@@ -8,24 +8,19 @@
|
||||
|
||||
local request = require "http.request"
|
||||
local headers = require "http.headers"
|
||||
local util = require "http.util"
|
||||
local dict_to_query = require "http.util" .dict_to_query
|
||||
|
||||
local encodeURI, decodeURI = util.encodeURI, util.decodeURI
|
||||
local dict_to_query = util.dict_to_query
|
||||
|
||||
|
||||
local CqHttpClient = {
|
||||
quote = function (self, text) return encodeURI(text) end,
|
||||
unquote = function (self, text) return decodeURI(text) end,
|
||||
local httpclient = {
|
||||
quote = require "http.util" .encodeURI,
|
||||
unquote = require "http.util" .decodeURI,
|
||||
}
|
||||
CqHttpClient.__name = "matrix.factory.chttp"
|
||||
CqHttpClient.__index = CqHttpClient
|
||||
httpclient.__name = "matrix.factory.chttp"
|
||||
httpclient.__index = httpclient
|
||||
|
||||
function CqHttpClient:__tostring()
|
||||
function httpclient:__tostring()
|
||||
return self.__name
|
||||
end
|
||||
|
||||
|
||||
local function headers_to_dict(h)
|
||||
local headers = {}
|
||||
for name, value in pairs(h) do
|
||||
@@ -36,8 +31,7 @@ local function headers_to_dict(h)
|
||||
return headers
|
||||
end
|
||||
|
||||
|
||||
function CqHttpClient:request(log, method, url, query_args, body, headers)
|
||||
function httpclient:request(log, method, url, query_args, body, headers)
|
||||
do
|
||||
local qs = dict_to_query(query_args)
|
||||
if #qs > 0 then
|
||||
@@ -71,5 +65,5 @@ function CqHttpClient:request(log, method, url, query_args, body, headers)
|
||||
end
|
||||
|
||||
return function ()
|
||||
return setmetatable({}, CqHttpClient)
|
||||
return setmetatable({}, httpclient)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user