Skip to content

Commit 3497cfc

Browse files
authored
3 add configurable options (#4)
* Add configurable options * remove unused function * update typos
1 parent 01f016f commit 3497cfc

8 files changed

Lines changed: 101 additions & 17 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Continuous Integration (CI)
1+
name: CI
22

33
on:
44
push:

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## major.minor.patch (yyyy.mm.dd)
99

10+
## 0.3.0 (2024.12.15)
11+
12+
### Changed
13+
14+
* HTTP request construction in `OpenApiTypesense.Client` to include `options`.
15+
16+
### Added
17+
18+
* `options` in config `config/config.exs`.
19+
* `get_options/0` function in `Client` to fetch the `options` configuration.
20+
* tests for `get_options/0` in `ClientTest`.
21+
1022
## 0.2.1 (2024.12.15)
1123

1224
### Changed

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Restful client for Typesense with adherence to Open API spec 3 (formerly Swagger
1313
## TODO
1414
- Custom Http client adapter (currently [`Req`](https://hexdocs.pm/req))
1515

16-
1716
## Installation
1817

1918
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
@@ -22,7 +21,10 @@ by adding `open_api_typesense` to your list of dependencies in `mix.exs`:
2221
```elixir
2322
def deps do
2423
[
25-
{:open_api_typesense, "~> 0.2"}
24+
{:open_api_typesense, "~> 0.3"}
25+
26+
# Or from GitHub repository, if you want the latest greatest from main branch
27+
{:open_api_typesense, git: "https://github.com/jaeyson/open_api_typesense.git"}
2628
]
2729
end
2830
```
@@ -31,3 +33,42 @@ Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_do
3133
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
3234
be found at <https://hexdocs.pm/open_api_typesense>.
3335

36+
## Getting started
37+
38+
### Adding credentials
39+
40+
Spin up local Typesense instance
41+
42+
```bash
43+
docker compose up -d
44+
45+
# check if "peer refreshed" in logs
46+
docker container logs --follow --tail 50 typesense
47+
```
48+
49+
```elixir
50+
# e.g. config/runtime.exs
51+
if config_env() == :prod do # if you'll use this in prod environment
52+
config :open_api_typesense,
53+
api_key: "xyz",
54+
host: "localhost",
55+
port: 8108,
56+
scheme: "http",
57+
options: %{}
58+
...
59+
```
60+
61+
> **Note**: The `options` key can be used to pass additional configuration options such as custom Finch instance or receive timeout settings. You can add any options supported by Req here. For more details check [Req documentation](https://hexdocs.pm/req/Req.Steps.html#run_finch/1-request-options).
62+
63+
> **Note**: If you use this for adding tests in your app, you might want to add this in `config/test.exs`:
64+
65+
For Cloud hosted, you can generate and obtain the credentials from cluster instance admin interface:
66+
67+
```elixir
68+
config :open_api_typesense,
69+
api_key: "credential", # Admin API key
70+
host: "111222333aaabbbcc-9.x9.typesense.net", # Nodes
71+
port: 443,
72+
scheme: "https",
73+
options: %{}
74+
```

lib/open_api_typesense.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
defmodule OpenApiTypesense do
22
@moduledoc false
3-
4-
@doc false
5-
def count_uniq_atom_keys do
6-
nil
7-
end
83
end

lib/open_api_typesense/client.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ defmodule OpenApiTypesense.Client do
2828
Application.get_env(:open_api_typesense, :port)
2929
end
3030

31+
@doc since: "0.3.0"
32+
@spec get_options :: map()
33+
def get_options, do: Application.get_env(:open_api_typesense, :options, %{})
34+
3135
@doc """
3236
Returns the Typesense's API key
3337
@@ -56,15 +60,13 @@ defmodule OpenApiTypesense.Client do
5660
- `:query`: Request query params (defaults to `%{}`).
5761
5862
## Examples
59-
iex> alias OpenApiTypesense.Client
60-
61-
iex> connection = %OpenApiTypesense.Connection{
63+
iex> alias OpenApiTypesense.{Client,Connection}
64+
iex> connection = %Connection{
6265
...> host: "localhost",
6366
...> api_key: "some_api_key",
6467
...> port: "8108",
6568
...> scheme: "http"
6669
...> }
67-
6870
iex> opts = %{
6971
...> args: [],
7072
...> call: {OpenApiTypesense.Health, :health},
@@ -73,13 +75,12 @@ defmodule OpenApiTypesense.Client do
7375
...> response: [{200, {OpenApiTypesense.HealthStatus, :t}}],
7476
...> opts: opts
7577
...> }
76-
7778
iex> Client.request(connection, opts)
7879
{:ok, %OpenApiTypesense.HealthStatus{ok: true}}
7980
"""
8081
@doc since: "0.2.0"
81-
@spec request(Connection.t(), list()) :: response()
82-
def request(conn, opts \\ []) do
82+
@spec request(Connection.t(), map()) :: response()
83+
def request(conn, opts) do
8384
# Req.Request.append_error_steps and its retry option are used here.
8485
# options like retry, max_retries, etc. can be found in:
8586
# https://hexdocs.pm/req/Req.Steps.html#retry/1
@@ -122,6 +123,7 @@ defmodule OpenApiTypesense.Client do
122123
decode_json: [keys: :atoms]
123124
]
124125
|> Req.new()
126+
|> Req.Request.merge_options(Map.to_list(get_options()))
125127
|> Req.Request.put_header("x-typesense-api-key", api_key())
126128
|> Req.Request.run_request()
127129

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule OpenApiTypesense.MixProject do
33

44
@source_url "https://github.com/jaeyson/open_api_typesense"
55
@hex_url "https://hexdocs.pm/open_api_typesense"
6-
@version "0.2.1"
6+
@version "0.3.0"
77

88
def project do
99
[

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"hpax": {:hex, :hpax, "1.0.1", "c857057f89e8bd71d97d9042e009df2a42705d6d690d54eca84c8b29af0787b0", [:mix], [], "hexpm", "4e2d5a4f76ae1e3048f35ae7adb1641c36265510a2d4638157fbcb53dda38445"},
1010
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
1111
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
12-
"makeup_elixir": {:hex, :makeup_elixir, "1.0.0", "74bb8348c9b3a51d5c589bf5aebb0466a84b33274150e3b6ece1da45584afc82", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49159b7d7d999e836bedaf09dcf35ca18b312230cf901b725a64f3f42e407983"},
12+
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},
1313
"makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"},
1414
"mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
1515
"mint": {:hex, :mint, "1.6.2", "af6d97a4051eee4f05b5500671d47c3a67dac7386045d87a904126fd4bbcea2e", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "5ee441dffc1892f1ae59127f74afe8fd82fda6587794278d924e4d90ea3d63f9"},

test/client_test.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule ClientTest do
2+
use ExUnit.Case, async: true
3+
4+
alias OpenApiTypesense.Client
5+
6+
setup do
7+
# Backup the original configuration
8+
original_config = Application.get_env(:open_api_typesense, :options, %{})
9+
10+
on_exit(fn ->
11+
# Restore the original configuration after each test
12+
Application.put_env(:open_api_typesense, :options, original_config)
13+
end)
14+
end
15+
16+
describe "get_options/0" do
17+
@tag ["27.1": true, "26.0": true, "0.25.2": true]
18+
test "returns the configured options" do
19+
Application.put_env(:open_api_typesense, :options, %{
20+
finch: MyApp.CustomFinch,
21+
receive_timeout: 5_000
22+
})
23+
24+
assert Client.get_options() == %{finch: MyApp.CustomFinch, receive_timeout: 5_000}
25+
end
26+
27+
@tag ["27.1": true, "26.0": true, "0.25.2": true]
28+
test "returns an empty map if options is not configured" do
29+
Application.delete_env(:open_api_typesense, :options)
30+
31+
assert Client.get_options() == %{}
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)