Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yandex,curl): fixed yandex v2, changes to curl #18

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lua/pantran/curl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local curl = {

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

for key, value in pairs(vim.tbl_extend("error", self._data, data)) do
table.insert(args, 1, ("%s=%s"):format(key, value))
table.insert(args, 1, "--data-urlencode")
if self._headers["Content-Type"] == "application/json" then
table.insert(args, 1, vim.json.encode(vim.tbl_extend("error", vim.empty_dict(), self._data, data)))
table.insert(args, 1, "--data")
else
for key, value in pairs(vim.tbl_extend("error", self._data, data)) do
table.insert(args, 1, ("%s=%s"):format(key, value))
table.insert(args, 1, "--data-urlencode")
end
end

for key, value in pairs(self._headers) do
Expand Down
9 changes: 5 additions & 4 deletions lua/pantran/engines/yandex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local yandex = {
}

function yandex.detect(text)
local detected = yandex._api:post("detectLanguage", {
local detected = yandex._api:post("detect", {
text = text,
})

Expand All @@ -32,7 +32,7 @@ function yandex.languages()
target = {}
}

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

yandex._api = curl.new{
url = yandex.url,
static_paths = {"listLanguages"},
static_paths = {"languages"},
fmt_error = function(response) return response.message end,
headers = {
authorization = c.api_key and ("Api-Key %s"):format(c.api_key) or ("Bearer %s"):format(c.iam_token)
["Content-Type"] = "application/json",
["Authorization"] = c.api_key and ("Api-Key %s"):format(c.api_key) or ("Bearer %s"):format(c.iam_token)
},
data = {
folderId = c.folder_id,
Expand Down