92 lines
2.8 KiB
Julia
92 lines
2.8 KiB
Julia
# using URIParser
|
|
|
|
import Base.get
|
|
function get(s::MatrixServer, path::String)
|
|
HTTP.get(join([s.instance, API_PATH, path], "/"))
|
|
end
|
|
|
|
"""
|
|
Authenticated get request
|
|
"""
|
|
function get(u::MatrixUser, path::String;
|
|
extraquery = Dict())
|
|
query = Dict{String,Any}(
|
|
"access_token" => u.token
|
|
)
|
|
merge!(query, extraquery)
|
|
HTTP.get(join([u.server.instance, API_PATH, path], "/"),
|
|
query = query)
|
|
end
|
|
|
|
function post(s::MatrixServer, path::String, data::Dict{String,Any})
|
|
HTTP.request("POST", join([s.instance, API_PATH, path], "/"),
|
|
["Content-Type" => "application/json"],
|
|
JSON.json(data))
|
|
end
|
|
|
|
"""
|
|
Authenticated post request
|
|
"""
|
|
function post(u::MatrixUser, path::String,
|
|
data;
|
|
mime = "application/json")
|
|
HTTP.request("POST", join([u.server.instance, "_matrix/media/v1/upload", path], "/"),
|
|
["Content-Type" => mime],
|
|
data,
|
|
query = Dict(
|
|
"access_token" => u.token
|
|
))
|
|
end
|
|
|
|
function request(request::String, u::MatrixUser, path::String,
|
|
data::Dict{String,Any};
|
|
mime = "application/json")
|
|
HTTP.request(request,
|
|
join([u.server.instance, API_PATH, path], "/"),
|
|
["Content-Type" => string(mime)],
|
|
JSON.json(data),
|
|
query = Dict(
|
|
"access_token" => u.token
|
|
))
|
|
end
|
|
|
|
function put(u::MatrixUser, path::String,
|
|
data::Dict{String,Any};
|
|
mime = "application/json")
|
|
request("PUT", u, path, data, mime = mime)
|
|
end
|
|
|
|
function post(u::MatrixUser, path::String,
|
|
data::Dict{String,String};
|
|
mime = "application/json")
|
|
HTTP.request("POST",
|
|
join([u.server.instance, API_PATH, path], "/"),
|
|
["Content-Type" => string(mime)],
|
|
JSON.json(data),
|
|
query = Dict(
|
|
"access_token" => u.token
|
|
))
|
|
end
|
|
|
|
function upload(u::MatrixUser, mime, data::Any)
|
|
res = post(u, "media/upload", data, mime = mime)
|
|
JSON.parse(String(res.body))
|
|
end
|
|
|
|
# matrixurl
|
|
import Base.download
|
|
function download(u::MatrixUser, murl::String; download_path = "~/matrix/")
|
|
mkpath(expanduser(download_path))
|
|
uri = URI(murl)
|
|
murl = string(uri.host, uri.path)
|
|
|
|
query = Dict(
|
|
"access_token" => u.token
|
|
)
|
|
res = HTTP.get(join([u.server.instance, "_matrix/media/r0/download", murl], "/"), query = query)
|
|
filetype = filter( x -> x[1] == "Content-Type", res.headers |> values)[1][2]
|
|
filepath = realpath(joinpath(expanduser(download_path), string(".", uri.path)))
|
|
filesize = write(filepath, res.body)
|
|
(filetype, filesize, filepath)
|
|
end
|