Skip to content

Commit c0d2844

Browse files
infastininfastin
and
infastin
authored
fix(yandex,curl): fixed yandex v2, changes to curl (#18)
* fix(yandex,curl): fixed yandex v2, changes to curl Now curl will automatically generate json data if Content-Type is application/json. * feat(curl): move vim.empty_dict() --------- Co-authored-by: infastin <thecookied123@gmail.com>
1 parent ab49d88 commit c0d2844

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lua/pantran/curl.lua

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local curl = {
1212

1313
function curl:_spawn(request, path, data, callback)
1414
local cmd, stdout, response, handle = self.config.cmd, vim.loop.new_pipe(), ""
15-
local args =vim.tbl_extend("keep", self.config.user_args, {
15+
local args = vim.tbl_extend("keep", self.config.user_args, {
1616
"--fail-with-body",
1717
"--retry", self.config.retry,
1818
"--max-time", self.config.timeout,
@@ -22,9 +22,14 @@ function curl:_spawn(request, path, data, callback)
2222
tostring(self._url / path)
2323
})
2424

25-
for key, value in pairs(vim.tbl_extend("error", self._data, data)) do
26-
table.insert(args, 1, ("%s=%s"):format(key, value))
27-
table.insert(args, 1, "--data-urlencode")
25+
if self._headers["Content-Type"] == "application/json" then
26+
table.insert(args, 1, vim.json.encode(vim.tbl_extend("error", vim.empty_dict(), self._data, data)))
27+
table.insert(args, 1, "--data")
28+
else
29+
for key, value in pairs(vim.tbl_extend("error", self._data, data)) do
30+
table.insert(args, 1, ("%s=%s"):format(key, value))
31+
table.insert(args, 1, "--data-urlencode")
32+
end
2833
end
2934

3035
for key, value in pairs(self._headers) do

lua/pantran/engines/yandex.lua

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local yandex = {
1717
}
1818

1919
function yandex.detect(text)
20-
local detected = yandex._api:post("detectLanguage", {
20+
local detected = yandex._api:post("detect", {
2121
text = text,
2222
})
2323

@@ -32,7 +32,7 @@ function yandex.languages()
3232
target = {}
3333
}
3434

35-
local langs = yandex._api:post("listLanguages").languages
35+
local langs = yandex._api:post("languages").languages
3636
for _, lang in pairs(langs) do
3737
languages.source[lang.code] = lang.name
3838
languages.target[lang.code] = lang.name
@@ -71,10 +71,11 @@ function yandex.setup()
7171

7272
yandex._api = curl.new{
7373
url = yandex.url,
74-
static_paths = {"listLanguages"},
74+
static_paths = {"languages"},
7575
fmt_error = function(response) return response.message end,
7676
headers = {
77-
authorization = c.api_key and ("Api-Key %s"):format(c.api_key) or ("Bearer %s"):format(c.iam_token)
77+
["Content-Type"] = "application/json",
78+
["Authorization"] = c.api_key and ("Api-Key %s"):format(c.api_key) or ("Bearer %s"):format(c.iam_token)
7879
},
7980
data = {
8081
folderId = c.folder_id,

0 commit comments

Comments
 (0)