Add checking and updating DeepL usage information
This commit is contained in:
@ -16,12 +16,10 @@ defmodule Outlook.Translators do
|
||||
|
||||
def get_deepl_account!(id), do: Repo.get!(DeeplAccount, id)
|
||||
|
||||
def get_deepl_auth_key(user_id) do
|
||||
query =
|
||||
from DeeplAccount,
|
||||
where: [user_id: ^user_id],
|
||||
select: [:auth_key]
|
||||
Repo.one!(query)
|
||||
def get_deepl_auth_key(user) do
|
||||
deepl_account_for_user(user)
|
||||
|> select([:auth_key])
|
||||
|> Repo.one()
|
||||
|> Map.get(:auth_key)
|
||||
end
|
||||
|
||||
@ -45,6 +43,10 @@ defmodule Outlook.Translators do
|
||||
DeeplAccount.changeset(deepl_account, attrs)
|
||||
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
|
||||
%{lang: target_lang,
|
||||
@ -64,6 +66,11 @@ defmodule Outlook.Translators do
|
||||
Task.start_link(Deepl, :translate, args)
|
||||
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
|
||||
# Logger.info "so far."
|
||||
HtmlDocComponent.render_doc(%{tree: tree})
|
||||
|
||||
@ -10,6 +10,12 @@ defmodule Outlook.Translators.Deepl do
|
||||
send(pid, {:progress, %{progress: nil}})
|
||||
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]
|
||||
|
||||
@doc """
|
||||
|
||||
@ -18,6 +18,6 @@ defmodule Outlook.Translators.DeeplAccount do
|
||||
def changeset(deepl_account, attrs) do
|
||||
deepl_account
|
||||
|> 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
|
||||
|
||||
@ -68,7 +68,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
|
||||
|
||||
def update(%{deepl_translation: translation}, socket) do
|
||||
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, socket
|
||||
|> assign(translation_content: new_translation_content)
|
||||
|
||||
Reference in New Issue
Block a user