Skip to content

Commit e88c337

Browse files
authored
Reduce code redunancy (#29)
1 parent 2ee575e commit e88c337

38 files changed

Lines changed: 850 additions & 3248 deletions

CHANGELOG.md

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

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

10+
## 1.0.0 (2025.05.31)
11+
12+
### Changed
13+
14+
* Passing `conn` as part of `opts` when calling a function, as this reduces code duplication. [Issue #26](https://github.com/jaeyson/open_api_typesense/issues/26) [PR #29](https://github.com/jaeyson/open_api_typesense/pull/29)
15+
16+
### Chore
17+
18+
* Bumped dev dependencies
19+
1020
## 0.7.1 (2025.04.06)
1121

1222
### Fixed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ Restful client for Typesense with adherence to Open API spec 3 (formerly Swagger
1717
[![Hex.pm](https://img.shields.io/hexpm/l/open_api_typesense)](https://hexdocs.pm/open_api_typesense/license.html)
1818
[![Latest Typesense compatible](https://img.shields.io/badge/Latest%20Typesense%20compatible-v28.0-%230F35BC)](https://typesense.org/docs/28.0/api)
1919

20+
> #### Upgrading to v1 {: .warning}
21+
>
22+
> The breaking change here is `conn` is now part of `opts`
23+
> when calling functions, see example below:
24+
25+
```elixir
26+
# pre-v1
27+
Collections.get_collections(conn, opts)
28+
29+
# v1
30+
Collections.get_collections(conn: conn)
31+
32+
# another way (v1)
33+
opts = [limit: 1, conn: conn]
34+
Collections.get_collections(opts)
35+
```
2036
## Installation
2137

2238
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
@@ -25,7 +41,7 @@ by adding `open_api_typesense` to your list of dependencies in `mix.exs`:
2541
```elixir
2642
def deps do
2743
[
28-
{:open_api_typesense, "~> 0.7"}
44+
{:open_api_typesense, "~> 1.0"}
2945

3046
# Or from GitHub repository, if you want the latest greatest from main branch
3147
{:open_api_typesense, git: "https://github.com/jaeyson/open_api_typesense.git"}
@@ -93,7 +109,16 @@ config :open_api_typesense,
93109
scheme: "https"
94110
```
95111

96-
## Using a another HTTP client
112+
## Using another connection via maps
113+
114+
You might be using a connection that changes dynamically. You can pass it as a map:
115+
116+
```elixir
117+
custom_conn = %{api_key: "xyz", host: "localhost", port: 8108, scheme: "http"}
118+
OpenApiTypesense.Health(conn: conn)
119+
```
120+
121+
## Using another HTTP client
97122

98123
In order to use another HTTP client, OpenApiTypesense has a
99124
callback function ([Behaviours](https://hexdocs.pm/elixir/typespecs.html#behaviours))

lib/open_api_typesense/client.ex

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule OpenApiTypesense.Client do
1111
A callback function for custom HTTP client
1212
"""
1313
@doc since: "0.2.0"
14-
@callback request(conn :: map(), params :: keyword()) :: response()
14+
@callback request(params :: map()) :: response()
1515

1616
@typedoc since: "0.2.0"
1717
@type response() ::
@@ -64,15 +64,22 @@ defmodule OpenApiTypesense.Client do
6464
{:ok, %OpenApiTypesense.HealthStatus{ok: true}}
6565
"""
6666
@doc since: "0.2.0"
67-
@spec request(map() | Connection.t(), map()) :: response()
68-
def request(conn, opts) do
67+
@spec request(map()) :: response()
68+
def request(params) do
69+
conn =
70+
if params.opts[:conn] do
71+
Connection.new(params.opts[:conn])
72+
else
73+
Connection.new()
74+
end
75+
6976
client = Map.get(conn, :client)
7077

7178
if client do
72-
client.request(conn, opts)
79+
client.request(conn, params)
7380
else
74-
req_client = build_req_client(conn, opts)
75-
req_request(req_client, opts)
81+
req_client = build_req_client(conn, params)
82+
req_request(req_client, params)
7683
end
7784
end
7885

@@ -113,9 +120,9 @@ defmodule OpenApiTypesense.Client do
113120
defp encode_body(opts) do
114121
if opts[:request] do
115122
[content_type] = opts[:request]
116-
parse_content_type(content_type, opts[:body])
123+
parse_content_type(content_type, opts[:args][:body])
117124
else
118-
Jason.encode_to_iodata!(opts[:body])
125+
Jason.encode_to_iodata!(opts[:args][:body])
119126
end
120127
end
121128

0 commit comments

Comments
 (0)