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 .. "}"
|
return self.__name .. "{" .. self.base_url .. "}"
|
||||||
end
|
end
|
||||||
|
|
||||||
function API:_quote(string)
|
|
||||||
return self._http:quote(string)
|
|
||||||
end
|
|
||||||
|
|
||||||
function API:initial_sync(limit)
|
function API:initial_sync(limit)
|
||||||
return self:_send("GET", "/initialSync", { limit = limit or 1 })
|
return self:_send("GET", "/initialSync", { limit = limit or 1 })
|
||||||
@@ -103,7 +100,7 @@ function API:create_room(options)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function API:join_room(room_id_or_alias)
|
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
|
end
|
||||||
|
|
||||||
function API:event_stream(from_token, timeout)
|
function API:event_stream(from_token, timeout)
|
||||||
@@ -111,9 +108,10 @@ function API:event_stream(from_token, timeout)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function API:send_state_event(room_id, event_type, content, state_key)
|
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
|
if state_key then
|
||||||
path = path .. "/" .. self:_quote(state_key)
|
path = path .. "/" .. self._http.quote(state_key)
|
||||||
end
|
end
|
||||||
return self:_send("PUT", path, nil, content)
|
return self:_send("PUT", path, nil, content)
|
||||||
end
|
end
|
||||||
@@ -123,8 +121,9 @@ function API:send_message_event(room_id, event_type, content, txn_id)
|
|||||||
txn_id = self.txn_id
|
txn_id = self.txn_id
|
||||||
self.txn_id = self.txn_id + 1
|
self.txn_id = self.txn_id + 1
|
||||||
end
|
end
|
||||||
local path = "/rooms/" .. self:_quote(room_id) .. "/send/" ..
|
local path = "/rooms/" .. self._http.quote(room_id) .. "/send/" ..
|
||||||
self:_quote(event_type) .. "/" .. self:_quote(tostring(txn_id))
|
self._http.quote(event_type) .. "/" ..
|
||||||
|
self._http.quote(tostring(txn_id))
|
||||||
return self:_send("PUT", path, nil, content)
|
return self:_send("PUT", path, nil, content)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -149,19 +148,19 @@ function API:send_notice(room_id, text_content)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function API:get_room_name(room_id)
|
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
|
end
|
||||||
|
|
||||||
function API:get_room_topic(room_id)
|
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
|
end
|
||||||
|
|
||||||
function API:leave_room(room_id)
|
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
|
end
|
||||||
|
|
||||||
function API:invite_user(room_id, user_id)
|
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 })
|
{ user_id = user_id })
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -170,17 +169,18 @@ function API:kick_user(room_id, user_id, reason)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function API:set_membership(room_id, user_id, membership, reason)
|
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 "" })
|
return self:_send("PUT", path, nil, { membership = membership, reason = reason or "" })
|
||||||
end
|
end
|
||||||
|
|
||||||
function API:ban_user(room_id, user_id, reason)
|
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 "" })
|
{ user_id = user_id, reason = reason or "" })
|
||||||
end
|
end
|
||||||
|
|
||||||
function API:get_room_state(room_id)
|
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
|
end
|
||||||
|
|
||||||
function API:get_text_body(text, msg_type)
|
function API:get_text_body(text, msg_type)
|
||||||
@@ -199,22 +199,22 @@ function API:media_upload(content, content_type)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function API:get_display_name(user_id)
|
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
|
return data.displayname
|
||||||
end
|
end
|
||||||
|
|
||||||
function API:set_display_name(user_id, display_name)
|
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 })
|
nil, { displayname = display_name })
|
||||||
end
|
end
|
||||||
|
|
||||||
function API:get_avatar_url(user_id)
|
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
|
return data.avatar_url
|
||||||
end
|
end
|
||||||
|
|
||||||
function API:set_avatar_url(user_id, avatar_url)
|
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 })
|
nil, { avatar_url = avatar_url })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -8,24 +8,19 @@
|
|||||||
|
|
||||||
local request = require "http.request"
|
local request = require "http.request"
|
||||||
local headers = require "http.headers"
|
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 httpclient = {
|
||||||
local dict_to_query = util.dict_to_query
|
quote = require "http.util" .encodeURI,
|
||||||
|
unquote = require "http.util" .decodeURI,
|
||||||
|
|
||||||
local CqHttpClient = {
|
|
||||||
quote = function (self, text) return encodeURI(text) end,
|
|
||||||
unquote = function (self, text) return decodeURI(text) end,
|
|
||||||
}
|
}
|
||||||
CqHttpClient.__name = "matrix.factory.chttp"
|
httpclient.__name = "matrix.factory.chttp"
|
||||||
CqHttpClient.__index = CqHttpClient
|
httpclient.__index = httpclient
|
||||||
|
|
||||||
function CqHttpClient:__tostring()
|
function httpclient:__tostring()
|
||||||
return self.__name
|
return self.__name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function headers_to_dict(h)
|
local function headers_to_dict(h)
|
||||||
local headers = {}
|
local headers = {}
|
||||||
for name, value in pairs(h) do
|
for name, value in pairs(h) do
|
||||||
@@ -36,8 +31,7 @@ local function headers_to_dict(h)
|
|||||||
return headers
|
return headers
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function httpclient:request(log, method, url, query_args, body, headers)
|
||||||
function CqHttpClient:request(log, method, url, query_args, body, headers)
|
|
||||||
do
|
do
|
||||||
local qs = dict_to_query(query_args)
|
local qs = dict_to_query(query_args)
|
||||||
if #qs > 0 then
|
if #qs > 0 then
|
||||||
@@ -71,5 +65,5 @@ function CqHttpClient:request(log, method, url, query_args, body, headers)
|
|||||||
end
|
end
|
||||||
|
|
||||||
return function ()
|
return function ()
|
||||||
return setmetatable({}, CqHttpClient)
|
return setmetatable({}, httpclient)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user