Add checking and updating DeepL usage information

This commit is contained in:
Thelonius Kort
2023-01-10 20:06:54 +01:00
parent 33ed533a1a
commit e16710664d
4 changed files with 21 additions and 8 deletions

View File

@ -16,12 +16,10 @@ defmodule Outlook.Translators do
def get_deepl_account!(id), do: Repo.get!(DeeplAccount, id) def get_deepl_account!(id), do: Repo.get!(DeeplAccount, id)
def get_deepl_auth_key(user_id) do def get_deepl_auth_key(user) do
query = deepl_account_for_user(user)
from DeeplAccount, |> select([:auth_key])
where: [user_id: ^user_id], |> Repo.one()
select: [:auth_key]
Repo.one!(query)
|> Map.get(:auth_key) |> Map.get(:auth_key)
end end
@ -45,6 +43,10 @@ defmodule Outlook.Translators do
DeeplAccount.changeset(deepl_account, attrs) DeeplAccount.changeset(deepl_account, attrs)
end end
def increase_our_character_count(user, billed_characters) do
deepl_account_for_user(user)
|> Repo.update_all([inc: [our_character_count: billed_characters]])
end
def translate(translation, current_user) do def translate(translation, current_user) do
%{lang: target_lang, %{lang: target_lang,
@ -64,6 +66,11 @@ defmodule Outlook.Translators do
Task.start_link(Deepl, :translate, args) Task.start_link(Deepl, :translate, args)
end end
defp deepl_account_for_user(user) when is_struct(user), do: deepl_account_for_user(user.id)
defp deepl_account_for_user(user_id) do
DeeplAccount |> where(user_id: ^user_id)
end
defp prepare_article(tree) do defp prepare_article(tree) do
# Logger.info "so far." # Logger.info "so far."
HtmlDocComponent.render_doc(%{tree: tree}) HtmlDocComponent.render_doc(%{tree: tree})

View File

@ -10,6 +10,12 @@ defmodule Outlook.Translators.Deepl do
send(pid, {:progress, %{progress: nil}}) send(pid, {:progress, %{progress: nil}})
end end
def get_usage_counts(auth_key) do
response = HTTPoison.get!("https://api-free.deepl.com/v2/usage",
["Authorization": "DeepL-Auth-Key #{auth_key}"])
Jason.decode!(response.body, keys: :atoms)
end
# @options [ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 500] # @options [ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 500]
@doc """ @doc """

View File

@ -18,6 +18,6 @@ defmodule Outlook.Translators.DeeplAccount do
def changeset(deepl_account, attrs) do def changeset(deepl_account, attrs) do
deepl_account deepl_account
|> cast(attrs, [:name, :description, :auth_key, :character_limit, :character_count, :our_character_count, :user_id]) |> cast(attrs, [:name, :description, :auth_key, :character_limit, :character_count, :our_character_count, :user_id])
|> validate_required([:name, :description, :auth_key, :character_limit, :user_id]) |> validate_required([:name, :description, :auth_key, :user_id])
end end
end end

View File

@ -68,7 +68,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
def update(%{deepl_translation: translation}, socket) do def update(%{deepl_translation: translation}, socket) do
tunit_keys = Map.keys(socket.assigns.translation_content) tunit_keys = Map.keys(socket.assigns.translation_content)
case Outlook.Translators.process_translation_result(translation, tunit_keys) do case Outlook.Translators.process_translation_result(translation, tunit_keys, socket.assigns.current_user) do
{:ok, new_translation_content} -> {:ok, new_translation_content} ->
{:ok, socket {:ok, socket
|> assign(translation_content: new_translation_content) |> assign(translation_content: new_translation_content)