Skip to content

Commit 3b57daf

Browse files
authored
Update Package Version & Modules Documentation (#10)
New Features - Introduced text rephrasing functionality for Pro users, including options for writing style and tone. - Added new modules for translation and rephrasing with improved APIs. Documentation - Expanded and clarified module and function documentation, including detailed usage examples and option descriptions. - Updated README and getting started guides for conciseness, improved formatting, and new badges. - Enhanced changelog with detailed entries for version 0.2.0. Bug Fixes - Resolved a compilation error related to API key configuration. Tests - Updated and added tests for translation, rephrasing, and usage features to match new API changes. Chores - Bumped project version to 0.2.0
1 parent ff491a4 commit 3b57daf

14 files changed

Lines changed: 189 additions & 76 deletions

File tree

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ To install `deepl`, follow the instructions in the
3030

3131
## Usage
3232

33-
Complete usage examples can be found in each module's documentation. See the
34-
[API Reference](https://hexdocs.pm/deepl/api-reference.html) page. Below are some basic examples
35-
to get you started.
33+
See the [API Reference](https://hexdocs.pm/deepl/api-reference.html) for full examples.
34+
Here are some basics:
3635

37-
Translate a text:
36+
**Translate text:**
3837

3938
```elixir
4039
iex> Deepl.Translator.translate("Hello World", "ID")
@@ -46,7 +45,7 @@ iex> Deepl.Translator.translate("Hello World", "ID")
4645
}}
4746
```
4847

49-
Rephrase a text:
48+
**Rephrase text:**
5049

5150
```elixir
5251
iex> Deepl.Writer.rephrase("this is a example sentence to imprve", "en-US")
@@ -62,7 +61,7 @@ iex> Deepl.Writer.rephrase("this is a example sentence to imprve", "en-US")
6261
}}
6362
```
6463

65-
Retrieve usage and quota information:
64+
**Get usage and quota:**
6665

6766
```elixir
6867
iex> Deepl.Usage.get()

lib/deepl.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
defmodule Deepl do
22
@moduledoc """
3-
An Elixir package providing a convenient interface to the [DeepL](https://www.deepl.com) API.
3+
Provide base functions for the DeepL API.
44
"""
55
@moduledoc since: "0.1.0"
66

77
@doc """
8-
Get the API key from the application environment.
9-
10-
Retrieves the API key set in the application configuration.
8+
Get the currently active API key.
119
1210
## Examples
1311
@@ -19,9 +17,7 @@ defmodule Deepl do
1917
def get_api_key, do: Application.get_env(:deepl, :api_key) || nil
2018

2119
@doc """
22-
Sets the API key for the DeepL service.
23-
24-
Stores the API key string in the application environment.
20+
Set the API key in the application environment.
2521
2622
## Examples
2723
@@ -33,9 +29,9 @@ defmodule Deepl do
3329
def set_api_key(key), do: Application.put_env(:deepl, :api_key, key)
3430

3531
@doc """
36-
Gets the current plan of the DeepL API based on the API key.
32+
Get current plan based on the API key.
3733
38-
Returns `:free` for free accounts and `:pro` for pro accounts.
34+
Returns atom `:free` for free accounts and `:pro` for pro accounts.
3935
4036
## Examples
4137
@@ -51,10 +47,10 @@ defmodule Deepl do
5147
end
5248

5349
@doc """
54-
Get the base URL for the DeepL API based on the current plan.
50+
Get base URL based on API key input.
5551
56-
Returns the base URL for the DeepL API, which varies depending on whether the user has a
57-
free or pro plan.
52+
Returns base URL for pro or free accounts, determined by the API key, free account keys end
53+
with `:fx`.
5854
5955
## Examples
6056

lib/deepl/http_helper.ex

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ defmodule Deepl.HTTPHelper do
33
@moduledoc since: "0.1.0"
44

55
@doc """
6-
Returns the required headers for Deepl API requests.
6+
Returns the required headers for a DeepL API request.
77
"""
8-
@spec required_request_header(Keyword.t()) :: list(tuple())
9-
def required_request_header(opts \\ []) do
8+
@spec required_request_headers(Keyword.t()) :: list(tuple())
9+
def required_request_headers(opts \\ []) do
1010
[
1111
{"Accept", Keyword.get(opts, :accept, "application/json")},
12-
{"Authorization", "DeepL-Auth-Key " <> Deepl.get_api_key()}
12+
{"Authorization", "DeepL-Auth-Key " <> Deepl.get_api_key()},
13+
{"Content-Type", Keyword.get(opts, :content_type, "application/json")}
1314
]
1415
end
1516

1617
@doc """
17-
Filters a keyword list to only include keys that are present in the given struct.
18+
Filters keywords by the keys of a given struct.
1819
1920
This function ensures that only valid options for the struct are included in the final map
2021
before sending a request to the DeepL API.
@@ -48,8 +49,8 @@ defmodule Deepl.HTTPHelper do
4849
@doc """
4950
Handles the response from the DeepL API.
5051
51-
It decodes the JSON body and returns the decoded body on success, or raises an exception on
52-
error.
52+
This function behaves like `response/2`, but raises an exception if the response is not
53+
successful.
5354
"""
5455
@spec response!(non_neg_integer(), binary()) :: map() | Exception.t()
5556
def response!(status, body) do

lib/deepl/request.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ defmodule Deepl.Request do
44

55
@callback run_request(term()) :: {term(), Req.Response.t() | Exception.t()}
66

7+
@doc """
8+
Runs the request using the configured request behaviour.
9+
10+
This function is used internally by the library to execute HTTP requests.
11+
"""
12+
@spec run_request(term()) :: {term(), Req.Response.t() | Exception.t()}
713
def run_request(request), do: implement().run_request(request)
814

915
defp implement, do: Application.get_env(:deepl, :request_behaviour, Req.Request)

lib/deepl/translator.ex

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
defmodule Deepl.Translator do
22
@moduledoc """
3-
Translates the given text to the target language.
3+
Provides functions to translate text.
4+
5+
API references for this module can be found in the Translate text
6+
[documentation](https://developers.deepl.com/api-reference/translate), please
7+
refer to the documentation for more information, especially options parameters detail usage in
8+
`translate/3` or `translate!/3`.
49
"""
510
@moduledoc since: "0.1.0"
611

@@ -10,9 +15,56 @@ defmodule Deepl.Translator do
1015
@type text :: binary() | list(binary())
1116

1217
@doc """
13-
Translates a single or multiple texts to the specified target language.
18+
Translates the given text to the specified target language.
19+
20+
The `text` parameter can be a single string or a list of strings, and the `target_lang`
21+
parameter specifies the language the text should be translated to, available languages can be
22+
found in the translation target languages
23+
[documentation](https://developers.deepl.com/docs/getting-started/supported-languages#translation-target-languages).
24+
25+
## Options
26+
27+
The accepted options are:
28+
29+
- `context` - Add additional context that can influence a translation but is not translated
30+
itself, the value must be a string.
31+
- `model_type` - Specifies which DeepL model should be used for translation, the value must be
32+
a string.
33+
- `split_sentences` - Sets whether the translation engine should first split the input into
34+
sentences, the value must be a string.
35+
- `preserve_formatting` - Sets whether the translation engine should respect the original
36+
formatting, even if it would usually correct some aspects, the value must be a boolean.
37+
- `formality` - Sets whether the translated text should lean towards formal or informal
38+
language, the value must be a string.
39+
- `glossary_id` - Specify the glossary to use for the translation, this option requires the
40+
`source_lang` option to be set, the value must be a string.
41+
- `show_billed_characters` - Show the number of billed characters in the response, the value
42+
must be a boolean.
43+
- `tag_handling` - Sets which kind of tags should be handled, the value must be a string.
44+
- `outline_detection` - Set the automatic detection of XML structure, the value must be a
45+
boolean.
46+
- `non_splitting_tags` - Set the XML tags that should not be split, the value must be a list of
47+
strings.
48+
- `splitting_tags` - Set the XML tags that always cause splits, the value must be a list of
49+
strings.
50+
- `ignore_tags` - Set the XML tags that you do not want to be translated, the value must be a
51+
list of strings.
52+
53+
The values for `model_type` can be:
54+
55+
- `latency_optimized`, `quality_optimized`, `prefer_quality_optimized`
56+
57+
The value for `split_sentences` can be:
58+
59+
- `"0"`, `"1"`, `"nonewlines"`
60+
61+
The values for `formality` can be:
62+
63+
- `default`, `more`, `less`, `prefer_more`, `prefer_less`
64+
65+
The values for `tag_handling` can be:
1466
15-
The `text` parameter can be a single string or a list of strings.
67+
- `xml` or `html`
1668
1769
## Examples
1870
@@ -24,9 +76,7 @@ defmodule Deepl.Translator do
2476
]
2577
}}
2678
27-
iex> Deepl.Translator.translate(
28-
...> ["Hello World", "Hello Developer"], "ID", show_billed_characters: true
29-
...> )
79+
iex> Deepl.Translator.translate(["Hello World", "Hello Developer"], "ID", show_billed_characters: true)
3080
{:ok,
3181
%{
3282
"translations" => [

lib/deepl/translator/translate_request.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Deepl.Translator.TranslateRequest do
22
@moduledoc false
33
@moduledoc since: "0.1.0"
44

5-
import Deepl.HTTPHelper, only: [required_request_header: 0]
5+
import Deepl.HTTPHelper, only: [required_request_headers: 0]
66

77
alias Deepl.HTTPHelper
88
alias Req.Request
@@ -44,7 +44,7 @@ defmodule Deepl.Translator.TranslateRequest do
4444
[
4545
method: :post,
4646
url: Deepl.base_url!() <> "/v2/translate",
47-
headers: [{"Content-Type", "application/json"} | required_request_header()],
47+
headers: required_request_headers(),
4848
body: body
4949
]
5050
|> Request.new()

lib/deepl/usage.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
defmodule Deepl.Usage do
22
@moduledoc """
3-
Module for retrieving usage and quota for the current billing period with the DeepL API.
3+
Provides function to retrieve usage and quota information.
44
5-
For more information, refer to the retrieve usage and quota
5+
API references for this module can be found in the Retrieve usage & quota
66
[documentation](https://developers.deepl.com/api-reference/usage-and-quota).
77
"""
88
@moduledoc since: "0.0.1"
99

10-
import Deepl.HTTPHelper, only: [required_request_header: 0, response: 2]
10+
import Deepl.HTTPHelper, only: [required_request_headers: 0, response: 2]
1111

1212
alias Req.Request
1313

1414
@doc """
15-
Retrieve current usage and quota information.
15+
Get the usage and quota information for current API key.
16+
17+
Returns usage and quota information, the output depends on the type of API key used
18+
(Free or Pro).
1619
1720
## Examples
1821
@@ -55,7 +58,7 @@ defmodule Deepl.Usage do
5558
[
5659
method: :get,
5760
url: Deepl.base_url!() <> "/v2/usage",
58-
headers: required_request_header()
61+
headers: required_request_headers()
5962
]
6063
|> Request.new()
6164
|> Deepl.Request.run_request()

lib/deepl/writer.ex

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
defmodule Deepl.Writer do
22
@moduledoc """
3+
Provides functions to rephrase text.
4+
5+
API references for this module can be found in the
6+
[Improve text documentation](https://developers.deepl.com/api-reference/improve-text).
7+
38
> #### DeepL API Pro Subscription {: .info}
49
>
5-
> **DeepL API for Write** requires a Pro subscription, ensure you use Pro API key for using
6-
> functions in this module.
7-
8-
Provides functions to rephrase text.
10+
> **DeepL API for Write** requires a Pro subscription. Ensure you use a Pro API key to access
11+
> the functions in this module.
912
"""
10-
@moduledoc since: "0.1.3"
13+
@moduledoc since: "0.2.0"
1114

1215
alias Deepl.HTTPHelper
1316
alias Deepl.Writer.RephraseRequest
@@ -17,7 +20,31 @@ defmodule Deepl.Writer do
1720
@doc """
1821
Rephrases the given text to the target language.
1922
20-
The `text` parameter can be a single string or a list of strings.
23+
The `text` parameter can be a single string or a list of strings, and the `target_lang`
24+
parameter available languages can be found in the Improve text
25+
[documentation](https://developers.deepl.com/api-reference/improve-text#param-target-lang).
26+
27+
## Options
28+
29+
The accepted options are:
30+
31+
- `writing_style` - Changes the writing style of your improvements, the value must be a string.
32+
- `tone` - Changes the tone of your improvements, the value must be a string.
33+
34+
> #### Using `writing_style` and `tone` {: .error}
35+
>
36+
> It’s not possible to include both `writing_style` and `tone` in a request; only one or the
37+
> other can be included, if you include both, the response will be an error.
38+
39+
The values for `writing_style` can be:
40+
41+
- `simple`, `business`, `academic`, `casual`, `default`, `prefer_simple`, `prefer_business`,
42+
`prefer_academic`, `prefer_casual`.
43+
44+
The values for `tone` can be:
45+
46+
- `friendly`, `enthusiastic`, `confident`, `diplomatic`, `default`, `prefer_enthusiastic`,
47+
`prefer_friendly`, `prefer_confident`, `prefer_diplomatic`.
2148
2249
## Examples
2350
@@ -57,9 +84,13 @@ defmodule Deepl.Writer do
5784
"""
5885
@spec rephrase(text(), String.t(), Keyword.t()) :: {:ok, map()} | {:error, String.t()}
5986
def rephrase(text, target_lang, opts \\ []) when is_binary(text) or is_list(text) do
60-
response = RephraseRequest.post_rephrase(text, target_lang, opts)
87+
if Keyword.has_key?(opts, :tone) && Keyword.has_key?(opts, :writing_style) do
88+
{:error, "Cannot include both tone and writing_style options"}
89+
else
90+
response = RephraseRequest.post_rephrase(text, target_lang, opts)
6191

62-
HTTPHelper.response(response.status, response.body)
92+
HTTPHelper.response(response.status, response.body)
93+
end
6394
end
6495

6596
@doc """
@@ -69,8 +100,12 @@ defmodule Deepl.Writer do
69100
"""
70101
@spec rephrase!(text(), String.t(), Keyword.t()) :: map()
71102
def rephrase!(text, target_lang, opts \\ []) when is_binary(text) or is_list(text) do
72-
response = RephraseRequest.post_rephrase(text, target_lang, opts)
103+
if Keyword.has_key?(opts, :tone) && Keyword.has_key?(opts, :writing_style) do
104+
raise ArgumentError, "Cannot include both tone and writing_style options"
105+
else
106+
response = RephraseRequest.post_rephrase(text, target_lang, opts)
73107

74-
HTTPHelper.response!(response.status, response.body)
108+
HTTPHelper.response!(response.status, response.body)
109+
end
75110
end
76111
end

lib/deepl/writer/rephrase_request.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Deepl.Writer.RephraseRequest do
22
@moduledoc false
33
@moduledoc since: "0.1.3"
44

5-
import Deepl.HTTPHelper, only: [required_request_header: 0]
5+
import Deepl.HTTPHelper, only: [required_request_headers: 0]
66

77
alias Deepl.HTTPHelper
88
alias Req.Request
@@ -11,6 +11,11 @@ defmodule Deepl.Writer.RephraseRequest do
1111

1212
defstruct text: nil, target_lang: nil, writing_style: nil, tone: nil
1313

14+
@doc """
15+
Sends a rephrasing request to the DeepL API.
16+
17+
Constructs a request to rephrase the provided text in the specified target language.
18+
"""
1419
@spec post_rephrase(text(), String.t(), Keyword.t()) :: Req.Response.t() | Exception.t()
1520
def post_rephrase(text, target_lang, opts \\ []) do
1621
body =
@@ -25,7 +30,7 @@ defmodule Deepl.Writer.RephraseRequest do
2530
[
2631
method: :post,
2732
url: Deepl.base_url!() <> "/v2/write/rephrase",
28-
headers: [{"Content-Type", "application/json"} | required_request_header()],
33+
headers: required_request_headers(),
2934
body: body
3035
]
3136
|> Request.new()

0 commit comments

Comments
 (0)