Skip to content

Commit a77be46

Browse files
authored
Merge pull request #2 from ekaputra07/refactoring
Refactoring for v0.3.0
2 parents 1f46f48 + c04b2da commit a77be46

11 files changed

+302
-108
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Api2pdf
22

3-
Unofficial API client for PDF generator/converter service https://www.api2pdf.com.
3+
API client for PDF generator/converter service https://www.api2pdf.com.
44

5-
At this point only features that are related to PDF generation and conversion are supported as those are the ones that I'm currently using personally.
5+
At this point only features that are related to PDF generation and conversion that are supported as those are the ones that I'm currently using.
66

7-
Contribution are welcomed to add missing features or simply open a ticket for feature requests. If you're looking for API clients in other languages please check their [official repository](https://github.com/Api2Pdf).
7+
Contribution are welcomed to add/fix features or simply open a ticket for feature requests or bug report. If you're looking for API clients in other languages please check their [official repository](https://github.com/Api2Pdf).
88

99
Below are lists of features that are supported and those that are not, grouped by their engine:
1010

@@ -36,10 +36,10 @@ Below are lists of features that are supported and those that are not, grouped b
3636
- [ ] generate_barcode
3737

3838
### Utility Commands
39-
- [x] check_balance (`Api2pdf.check_balance/1`)
40-
- [ ] delete_file
41-
- [ ] zip_files
42-
- [ ] api_status
39+
- [x] check_balance (`Api2pdf.balance/1`)
40+
- [x] delete_file (`Api2pdf.delete_file/2`)
41+
- [x] zip_files (`Api2pdf.zip_files/2`)
42+
- [x] api_status (`Api2pdf.status/1`)
4343

4444
## Installation
4545

@@ -48,7 +48,7 @@ Add `api2pdf` to your list of dependencies in `mix.exs`:
4848
```elixir
4949
def deps do
5050
[
51-
{:api2pdf, "~> 0.2"}
51+
{:api2pdf, "~> 0.3"}
5252
]
5353
end
5454
```

lib/api2pdf.ex

Lines changed: 99 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
defmodule Api2pdf do
22
@moduledoc File.read!("README.md")
33

4-
alias Api2pdf.Model.{
5-
ApiSuccessResponse,
6-
BalanceCheckSuccessResponse
7-
}
8-
9-
@spec make_post_request(String.t(), struct, keyword) ::
10-
{:error, any} | {:ok, ApiSuccessResponse.t()} | {:ok, BalanceCheckSuccessResponse.t()}
11-
def make_post_request(endpoint, payload, options \\ []) do
12-
client = Application.get_env(:api2pdf, :client, Api2pdf.Client)
13-
client.post_request(endpoint, payload, options) |> handle_response()
14-
end
4+
alias Api2pdf.Model.{ApiSuccessResponse, ZipFilesRequest}
155

16-
@spec make_get_request(String.t(), keyword) ::
17-
{:error, any} | {:ok, ApiSuccessResponse.t()} | {:ok, BalanceCheckSuccessResponse.t()}
18-
def make_get_request(endpoint, options \\ []) do
19-
client = Application.get_env(:api2pdf, :client, Api2pdf.Client)
20-
client.get_request(endpoint, options) |> handle_response()
6+
@doc """
7+
Returns HTTP client.
8+
"""
9+
@spec http_client() :: any()
10+
def http_client() do
11+
Application.get_env(:api2pdf, :client, Api2pdf.Client)
2112
end
2213

2314
@doc """
@@ -28,31 +19,109 @@ defmodule Api2pdf do
2819
## Examples
2920
```elixir
3021
# api_key in config.exs
31-
Api2pdf.check_balance()
22+
{:ok, 5.0} = Api2pdf.balance()
23+
24+
# OR, api_key as option
25+
{:ok, 5.0} = Api2pdf.balance(api_key: "YOUR-API-KEY")
26+
```
27+
"""
28+
@spec balance(keyword) :: {:error, any} | {:ok, number()}
29+
def balance(options \\ []) do
30+
http_client().get_request("/balance", options) |> handle_response()
31+
end
32+
33+
@doc """
34+
Api2pdf API health check.
35+
36+
https://app.swaggerhub.com/apis-docs/api2pdf/api2pdf/2.0.0#/Utility%20Commands/statusCheck
37+
38+
## Examples
39+
```elixir
40+
# api_key in config.exs
41+
:ok = Api2pdf.status()
42+
43+
# OR, api_key as option
44+
:ok = Api2pdf.status(api_key: "YOUR-API-KEY")
45+
```
46+
"""
47+
@spec status(keyword) :: :ok | :error
48+
def status(options \\ []) do
49+
http_client().get_request("/status", options)
50+
|> handle_response()
51+
|> case do
52+
{:ok, _} -> :ok
53+
_ -> :error
54+
end
55+
end
56+
57+
@doc """
58+
Delete a file on command instead of waiting 24 hours for self-delete.
59+
60+
https://app.swaggerhub.com/apis-docs/api2pdf/api2pdf/2.0.0#/Utility%20Commands/fileDELETE
61+
62+
## Examples
63+
```elixir
64+
response_id = "857af41a-b382-4c61-ace4-95be78dcd605"
65+
66+
# api_key in config.exs
67+
{:ok, _} = Api2pdf.delete_file(response_id)
68+
69+
# OR, api_key as option
70+
{:ok, _} = Api2pdf.delete_file(response_id, api_key: "YOUR-API-KEY")
71+
```
72+
"""
73+
@spec delete_file(String.t(), keyword) :: {:ok, ApiSuccessResponse.t()} | {:error, any()}
74+
def delete_file(response_id, options \\ []) do
75+
http_client().delete_request("/file/#{response_id}", options) |> handle_response()
76+
end
77+
78+
@doc """
79+
Create a ZIP file from a list of files.
80+
81+
https://app.swaggerhub.com/apis-docs/api2pdf/api2pdf/2.0.0#/Utility%20Commands/filesZip
82+
83+
## Examples
84+
```elixir
85+
alias Api2pdf.Model.ZipFilesRequest
86+
87+
files = ZipFilesRequest.new()
88+
|> ZipFilesRequest.add("https://example.com/halo.png")
89+
|> ZipFilesRequest.add("https://example.com/hola.png", "new-name.png")
90+
91+
# api_key in config.exs
92+
{:ok, _} = Api2pdf.zip_files(files)
3293
3394
# OR, api_key as option
34-
Api2pdf.check_balance(api_key: "YOUR-API-KEY")
95+
{:ok, _} = Api2pdf.zip_files(files, api_key: "YOUR-API-KEY")
3596
```
3697
"""
37-
@spec check_balance(keyword) ::
38-
{:error, any} | {:ok, BalanceCheckSuccessResponse.t()}
39-
def check_balance(options \\ []), do: make_get_request("/balance", options)
98+
@spec zip_files(ZipFilesRequest.t(), keyword) :: {:error, any} | {:ok, ApiSuccessResponse.t()}
99+
def zip_files(files, options \\ []) do
100+
http_client().post_request("/zip?outputBinary=false", files, options) |> handle_response()
101+
end
102+
103+
@doc """
104+
HTTP response handling.
105+
"""
106+
def handle_response(%{body: "OK"}), do: {:ok, "OK"}
107+
108+
def handle_response(%{body: %{"UserBalance" => balance}}) when is_number(balance),
109+
do: {:ok, balance}
40110

41-
# Response handling
42-
defp handle_response(%{body: %{"UserBalance" => _} = body}),
43-
do: {:ok, BalanceCheckSuccessResponse.from_body(body)}
111+
def handle_response(%{body: %{"FileUrl" => file_url} = body}) when is_binary(file_url),
112+
do: {:ok, ApiSuccessResponse.from_body(body)}
44113

45-
defp handle_response(%{body: %{"FileUrl" => file_url} = body}) when is_binary(file_url),
114+
def handle_response(%{body: %{"Success" => true} = body}),
46115
do: {:ok, ApiSuccessResponse.from_body(body)}
47116

48-
defp handle_response(%{body: %{"Error" => nil} = body}),
117+
def handle_response(%{body: %{"Error" => nil} = body}),
49118
do: {:ok, ApiSuccessResponse.from_body(body)}
50119

51-
defp handle_response(%{body: %{"Error" => error}}), do: {:error, error}
52-
defp handle_response(%{body: error}) when is_binary(error), do: {:error, error}
53-
defp handle_response({:error, _} = error), do: error
120+
def handle_response(%{body: %{"Error" => error}}), do: {:error, error}
121+
def handle_response(%{body: error}) when is_binary(error), do: {:error, error}
122+
def handle_response({:error, _} = error), do: error
54123

55-
defp handle_response(_) do
124+
def handle_response(_) do
56125
{:error, "unknown response format"}
57126
end
58127
end

lib/api2pdf/chrome.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ defmodule Api2pdf.Chrome do
22
@moduledoc """
33
Convert HTML document, web page to PDF or Image using Headless Chrome backend.
44
"""
5+
import Api2pdf, only: [http_client: 0, handle_response: 1]
56

67
alias Api2pdf.Model.{
78
ChromeHtmlToImageRequest,
89
ChromeHtmlToPdfRequest,
910
ChromeUrlToImageRequest,
10-
ChromeUrlToPdfRequest
11+
ChromeUrlToPdfRequest,
12+
ApiSuccessResponse
1113
}
1214

1315
@doc """
@@ -103,18 +105,18 @@ defmodule Api2pdf.Chrome do
103105
def request(payload, options \\ [])
104106

105107
def request(%ChromeHtmlToImageRequest{} = payload, options) do
106-
Api2pdf.make_post_request("/chrome/image/html", payload, options)
108+
http_client().post_request("/chrome/image/html", payload, options) |> handle_response()
107109
end
108110

109111
def request(%ChromeHtmlToPdfRequest{} = payload, options) do
110-
Api2pdf.make_post_request("/chrome/pdf/html", payload, options)
112+
http_client().post_request("/chrome/pdf/html", payload, options) |> handle_response()
111113
end
112114

113115
def request(%ChromeUrlToImageRequest{} = payload, options) do
114-
Api2pdf.make_post_request("/chrome/image/url", payload, options)
116+
http_client().post_request("/chrome/image/url", payload, options) |> handle_response()
115117
end
116118

117119
def request(%ChromeUrlToPdfRequest{} = payload, options) do
118-
Api2pdf.make_post_request("/chrome/pdf/url", payload, options)
120+
http_client().post_request("/chrome/pdf/url", payload, options) |> handle_response()
119121
end
120122
end

lib/api2pdf/client.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule Api2pdf.ClientBehaviour do
22
@callback post_request(url :: String.t(), payload :: struct, options :: keyword) ::
33
{:error, any} | map
44
@callback get_request(url :: String.t(), options :: keyword) :: {:error, any} | map
5+
@callback delete_request(url :: String.t(), options :: keyword) :: {:error, any} | map
56
end
67

78
defmodule Api2pdf.Client do
@@ -56,11 +57,18 @@ defmodule Api2pdf.Client do
5657
@spec get_request(String.t(), keyword) :: {:error, any} | map
5758
def get_request(endpoint, options \\ []) do
5859
client = make_client(options)
59-
api_key = read_config(options, :api_key, "")
60-
# put apikey into the query params
61-
query = Keyword.get(options, :query, []) |> Keyword.put(:apikey, api_key)
6260

63-
case Tesla.get(client, endpoint, query) do
61+
case Tesla.get(client, endpoint) do
62+
{:ok, resp} -> resp
63+
err -> err
64+
end
65+
end
66+
67+
@spec delete_request(String.t(), keyword) :: {:error, any} | map
68+
def delete_request(endpoint, options \\ []) do
69+
client = make_client(options)
70+
71+
case Tesla.delete(client, endpoint) do
6472
{:ok, resp} -> resp
6573
err -> err
6674
end

lib/api2pdf/libre_office.ex

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule Api2pdf.LibreOffice do
22
@moduledoc """
33
Convert documents from and to various formats using Libre Office backend.
44
"""
5+
import Api2pdf, only: [http_client: 0, handle_response: 1]
56

67
alias Api2pdf.Model.{
78
LibreOfficeRequest,
@@ -19,8 +20,9 @@ defmodule Api2pdf.LibreOffice do
1920
"""
2021
@spec any_to_pdf(LibreOfficeRequest.t(), keyword) ::
2122
{:error, any} | {:ok, ApiSuccessResponse.t()}
22-
def any_to_pdf(%LibreOfficeRequest{} = payload, options \\ []),
23-
do: Api2pdf.make_post_request("/libreoffice/any-to-pdf", payload, options)
23+
def any_to_pdf(%LibreOfficeRequest{} = payload, options \\ []) do
24+
http_client().post_request("/libreoffice/any-to-pdf", payload, options) |> handle_response()
25+
end
2426

2527
@doc """
2628
Generate an image of the first page of a PDF or Office Document.
@@ -35,8 +37,9 @@ defmodule Api2pdf.LibreOffice do
3537
"""
3638
@spec any_to_image(LibreOfficeRequest.t(), keyword) ::
3739
{:error, any} | {:ok, ApiSuccessResponse.t()}
38-
def any_to_image(%LibreOfficeRequest{} = payload, options \\ []),
39-
do: Api2pdf.make_post_request("/libreoffice/thumbnail", payload, options)
40+
def any_to_image(%LibreOfficeRequest{} = payload, options \\ []) do
41+
http_client().post_request("/libreoffice/thumbnail", payload, options) |> handle_response()
42+
end
4043

4144
@doc """
4245
Convert a PDF file to HTML using LibreOffice. **Limitation is that images will be lost**.
@@ -49,8 +52,9 @@ defmodule Api2pdf.LibreOffice do
4952
"""
5053
@spec pdf_to_html(LibreOfficeRequest.t(), keyword) ::
5154
{:error, any} | {:ok, ApiSuccessResponse.t()}
52-
def pdf_to_html(%LibreOfficeRequest{} = payload, options \\ []),
53-
do: Api2pdf.make_post_request("/libreoffice/pdf-to-html", payload, options)
55+
def pdf_to_html(%LibreOfficeRequest{} = payload, options \\ []) do
56+
http_client().post_request("/libreoffice/pdf-to-html", payload, options) |> handle_response()
57+
end
5458

5559
@doc """
5660
Convert HTML to `.docx` format using LibreOffice.
@@ -63,8 +67,9 @@ defmodule Api2pdf.LibreOffice do
6367
"""
6468
@spec html_to_docx(LibreOfficeRequest.t(), keyword) ::
6569
{:error, any} | {:ok, ApiSuccessResponse.t()}
66-
def html_to_docx(%LibreOfficeRequest{} = payload, options \\ []),
67-
do: Api2pdf.make_post_request("/libreoffice/html-to-docx", payload, options)
70+
def html_to_docx(%LibreOfficeRequest{} = payload, options \\ []) do
71+
http_client().post_request("/libreoffice/html-to-docx", payload, options) |> handle_response()
72+
end
6873

6974
@doc """
7075
Convert HTML to `.xlsx` using LibreOffice.
@@ -77,6 +82,7 @@ defmodule Api2pdf.LibreOffice do
7782
"""
7883
@spec html_to_xlsx(LibreOfficeRequest.t(), keyword) ::
7984
{:error, any} | {:ok, ApiSuccessResponse.t()}
80-
def html_to_xlsx(%LibreOfficeRequest{} = payload, options \\ []),
81-
do: Api2pdf.make_post_request("/libreoffice/html-to-xlsx", payload, options)
85+
def html_to_xlsx(%LibreOfficeRequest{} = payload, options \\ []) do
86+
http_client().post_request("/libreoffice/html-to-xlsx", payload, options) |> handle_response()
87+
end
8288
end

lib/api2pdf/model/balance_check_success_response.ex

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
defmodule Api2pdf.Model.ZipFilesRequest do
2+
@moduledoc """
3+
Compose payload for zipping multiple files into a single archive.
4+
5+
https://app.swaggerhub.com/apis-docs/api2pdf/api2pdf/2.0.0#/UtilityZipAllFiles
6+
7+
## Examples
8+
```elixir
9+
files = ZipFilesRequest.new()
10+
|> ZipFilesRequest.add("https://example.com/halo.png")
11+
|> ZipFilesRequest.add("https://example.com/hola.png", "new-name.png")
12+
```
13+
"""
14+
alias Api2pdf.Model.ZipFilesRequest
15+
@enforce_keys [:files]
16+
17+
defstruct [:files]
18+
19+
@type file :: %{
20+
url: String.t(),
21+
fileName: String.t()
22+
}
23+
@type t :: %__MODULE__{
24+
files: [file]
25+
}
26+
27+
def new(), do: %__MODULE__{files: []}
28+
29+
def add(%ZipFilesRequest{files: files} = zipfiles, url, name \\ nil) when is_binary(url) do
30+
name = name || Path.basename(url)
31+
%{zipfiles | files: [%{url: url, fileName: name} | files]}
32+
end
33+
end

0 commit comments

Comments
 (0)