-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: update photo's caption #24
Changes from 4 commits
9627c72
5d0a1fc
8f589d3
7bc2693
6a97220
d268df3
ac0f402
0545ed5
c35c174
bd0b637
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# 2024-10-25 | ||
|
||
## Req call typesense API alway :timeout, but typesense was updated. | ||
|
||
```elixir | ||
** (MatchError) no match of right hand side value: {:error, %Req.TransportError{reason: :timeout}} | ||
(save_it 0.2.0-rc.1) lib/migration/typesense.ex:11: Migration.Typesense.create_collection!/1 | ||
priv/typesense/reset.exs:3: (file) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Typesense | ||
|
||
## Typesense API Errors | ||
|
||
``` | ||
# 400 Bad Request - The request could not be understood due to malformed syntax. | ||
# 401 Unauthorized - Your API key is wrong. | ||
# 404 Not Found - The requested resource is not found. | ||
# 409 Conflict - When a resource already exists. | ||
# 422 Unprocessable Entity - Request is well-formed, but cannot be processed. | ||
# 503 Service Unavailable - We’re temporarily offline. Please try again later. | ||
``` | ||
|
||
docs: https://typesense.org/docs/27.1/api/api-errors.html#api-errors |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Migration.Typesense do | ||
def create_collection!(schema) do | ||
req = build_request("/collections") | ||
{:ok, res} = Req.post(req, json: schema) | ||
|
||
res.body | ||
end | ||
|
||
def delete_collection!(collection_name) do | ||
req = build_request("/collections/#{collection_name}") | ||
{:ok, res} = Req.delete(req) | ||
|
||
res.body | ||
end | ||
|
||
def list_collections() do | ||
req = build_request("/collections") | ||
{:ok, res} = Req.get(req) | ||
|
||
res.body | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve error handling and add documentation for public API functions. The public functions need several improvements for production readiness:
Here's a suggested implementation for better error handling and documentation: + @doc """
+ Creates a new collection in Typesense with the given schema.
+
+ ## Parameters
+ - schema: Map containing the collection schema
+
+ ## Returns
+ - {:ok, response_body} on success
+ - {:error, reason} on failure
+
+ ## Raises
+ - RuntimeError if the API request fails
+ """
+ @spec create_collection!(map()) :: map() | no_return()
def create_collection!(schema) do
req = build_request("/collections")
- {:ok, res} = Req.post(req, json: schema)
-
- res.body
+ case Req.post(req, json: schema) do
+ {:ok, %{status: status} = res} when status in 200..299 ->
+ res.body
+ {:ok, %{status: status, body: body}} ->
+ raise "Failed to create collection. Status: #{status}, Body: #{inspect(body)}"
+ {:error, error} ->
+ raise "Failed to create collection: #{inspect(error)}"
+ end
end Similar improvements should be applied to
|
||
|
||
defp get_env() do | ||
url = Application.fetch_env!(:save_it, :typesense_url) | ||
api_key = Application.fetch_env!(:save_it, :typesense_api_key) | ||
|
||
{url, api_key} | ||
end | ||
|
||
defp build_request(path) do | ||
{url, api_key} = get_env() | ||
|
||
Req.new( | ||
base_url: url, | ||
url: path, | ||
headers: [ | ||
{"Content-Type", "application/json"}, | ||
{"X-TYPESENSE-API-KEY", api_key} | ||
] | ||
) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance configuration handling and request building. Consider the following improvements for the helper functions:
Here's a suggested implementation: + @content_type "application/json"
+ @typesense_config_keys [:typesense_url, :typesense_api_key]
+
+ @doc false
defp get_env() do
- url = Application.fetch_env!(:save_it, :typesense_url)
- api_key = Application.fetch_env!(:save_it, :typesense_api_key)
+ url =
+ :save_it
+ |> Application.fetch_env!(:typesense_url)
+ |> validate_url!()
+
+ api_key = Application.fetch_env!(:save_it, :typesense_api_key)
{url, api_key}
end
+ @doc false
defp build_request(path) do
{url, api_key} = get_env()
Req.new(
base_url: url,
url: path,
headers: [
- {"Content-Type", "application/json"},
+ {"Content-Type", @content_type},
{"X-TYPESENSE-API-KEY", api_key}
]
)
end
+
+ defp validate_url!(url) do
+ uri = URI.parse(url)
+ unless uri.scheme in ["http", "https"] and uri.host do
+ raise ArgumentError, "Invalid Typesense URL: #{url}"
+ end
+ url
+ end
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Migration.Typesense.Photo do | ||
alias Migration.Typesense | ||
|
||
@photos_schema %{ | ||
"name" => "photos", | ||
"fields" => [ | ||
# image: base64 encoded string | ||
%{"name" => "image", "type" => "image", "store" => false}, | ||
%{ | ||
"name" => "image_embedding", | ||
"type" => "float[]", | ||
"embed" => %{ | ||
"from" => ["image"], | ||
"model_config" => %{ | ||
"model_name" => "ts/clip-vit-b-p32" | ||
} | ||
} | ||
}, | ||
%{"name" => "caption", "type" => "string", "optional" => true}, | ||
%{"name" => "file_id", "type" => "string"}, | ||
%{"name" => "belongs_to_id", "type" => "string"}, | ||
%{"name" => "inserted_at", "type" => "int64"} | ||
], | ||
"default_sorting_field" => "inserted_at" | ||
} | ||
|
||
def create_collection!() do | ||
Typesense.create_collection!(@photos_schema) | ||
end | ||
|
||
def reset!() do | ||
Typesense.delete_collection!(@photos_schema["name"]) | ||
Typesense.create_collection!(@photos_schema) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enhance security guidance for configuration management.
The current setup instructions expose sensitive configuration details. Consider these security improvements:
start.sh
private and not committing it to version control.env
file instead of direct exportsHere's a suggested improvement: