From a7b02c6b24d5040d23d7bb885e59a767b6ba8bec Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Wed, 29 May 2024 10:04:23 +0200 Subject: [PATCH 1/6] search_api: simplify using get search result --- lib/search_api.ex | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/search_api.ex b/lib/search_api.ex index 493cffe..20504fd 100644 --- a/lib/search_api.ex +++ b/lib/search_api.ex @@ -4,7 +4,6 @@ defmodule LogpointApi.SearchApi do """ alias LogpointApi.Credential - alias LogpointApi.SearchApi.{Query, SearchID} @allowed_types ["user_preference", "loginspects", "logpoint_repos", "devices", "livesearches"] @@ -77,10 +76,10 @@ defmodule LogpointApi.SearchApi do @doc """ Retrieve the search result of a specific search id. """ - @spec get_search_result(String.t(), Credential.t(), SearchID.t()) :: + @spec get_search_result(String.t(), Credential.t(), String.t()) :: {:ok, map()} | {:error, String.t()} - def get_search_result(ip, credential, %SearchID{} = search_id), - do: get_search_logs(ip, credential, search_id) + def get_search_result(ip, credential, search_id), + do: get_search_logs(ip, credential, %SearchID{search_id: search_id}) @doc false @spec make_request(String.t(), String.t(), String.t()) :: {:ok, map()} | {:error, String.t()} From 1758fc58601ac4a68b19310f92ca70490411ea74 Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Wed, 29 May 2024 10:04:55 +0200 Subject: [PATCH 2/6] logpoint_api: add initial run_search function --- lib/logpoint_api.ex | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/logpoint_api.ex b/lib/logpoint_api.ex index 57ff874..56e7eaf 100644 --- a/lib/logpoint_api.ex +++ b/lib/logpoint_api.ex @@ -1,6 +1,9 @@ defmodule LogpointApi do @moduledoc false + alias LogpointApi.SearchApi.Query + alias LogpointApi.SearchApi + defmodule Credential do @typedoc """ Struct representing credentials used for authorization. @@ -8,4 +11,36 @@ defmodule LogpointApi do @type t :: %__MODULE__{username: String.t(), secret_key: String.t()} defstruct [:username, :secret_key] end + + @spec run_search(String.t(), Credential.t(), Query.t()) :: {:ok, map()} | {:error, String.t()} + def run_search(ip, credential, %Query{} = query) do + {:ok, search_info} = SearchApi.get_search_id(ip, credential, query) + search_id = Map.get(search_info, "search_id") + + SearchApi.get_search_result(ip, credential, search_id) + |> handle_search_result(ip, credential, search_id, query) + end + + defp handle_search_result({:ok, %{"final" => true} = result}, _, _, _, _), do: result + + defp handle_search_result( + {:ok, %{"final" => false, "succes" => true}}, + ip, + credential, + search_id, + query + ) do + SearchApi.get_search_result(ip, credential, search_id) + |> handle_search_result(ip, credential, search_id, query) + end + + defp handle_search_result( + {:ok, %{"final" => false, "success" => false}}, + ip, + credential, + _, + query + ) do + run_search(ip, credential, query) + end end From dfd7e0174a9be01923715776e7a23fdfc6fb3e8c Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Wed, 29 May 2024 10:10:25 +0200 Subject: [PATCH 3/6] logpoint_api: add missing s in success --- lib/logpoint_api.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logpoint_api.ex b/lib/logpoint_api.ex index 56e7eaf..40b691f 100644 --- a/lib/logpoint_api.ex +++ b/lib/logpoint_api.ex @@ -24,7 +24,7 @@ defmodule LogpointApi do defp handle_search_result({:ok, %{"final" => true} = result}, _, _, _, _), do: result defp handle_search_result( - {:ok, %{"final" => false, "succes" => true}}, + {:ok, %{"final" => false, "success" => true}}, ip, credential, search_id, From b4db837618bc3a7cf4fe37456a96dde6d57fa5e5 Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Wed, 29 May 2024 10:14:26 +0200 Subject: [PATCH 4/6] logpoint_api: make run_search fail fast --- lib/logpoint_api.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logpoint_api.ex b/lib/logpoint_api.ex index 40b691f..3c23d62 100644 --- a/lib/logpoint_api.ex +++ b/lib/logpoint_api.ex @@ -14,7 +14,7 @@ defmodule LogpointApi do @spec run_search(String.t(), Credential.t(), Query.t()) :: {:ok, map()} | {:error, String.t()} def run_search(ip, credential, %Query{} = query) do - {:ok, search_info} = SearchApi.get_search_id(ip, credential, query) + {:ok, %{"success" => true} = search_info} = SearchApi.get_search_id(ip, credential, query) search_id = Map.get(search_info, "search_id") SearchApi.get_search_result(ip, credential, search_id) From f3e3aace05c09315fd54c3248258301d17a4b4ca Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Wed, 29 May 2024 10:22:29 +0200 Subject: [PATCH 5/6] logpoint_api: add doc and fix spec --- lib/logpoint_api.ex | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/logpoint_api.ex b/lib/logpoint_api.ex index 3c23d62..6fa838a 100644 --- a/lib/logpoint_api.ex +++ b/lib/logpoint_api.ex @@ -12,7 +12,10 @@ defmodule LogpointApi do defstruct [:username, :secret_key] end - @spec run_search(String.t(), Credential.t(), Query.t()) :: {:ok, map()} | {:error, String.t()} + @doc """ + Run a search query. + """ + @spec run_search(String.t(), Credential.t(), Query.t()) :: map() def run_search(ip, credential, %Query{} = query) do {:ok, %{"success" => true} = search_info} = SearchApi.get_search_id(ip, credential, query) search_id = Map.get(search_info, "search_id") @@ -30,8 +33,12 @@ defmodule LogpointApi do search_id, query ) do - SearchApi.get_search_result(ip, credential, search_id) - |> handle_search_result(ip, credential, search_id, query) + result = SearchApi.get_search_result(ip, credential, search_id) + + # Wait before retrying. + :timer.sleep(1000) + + handle_search_result(result, ip, credential, search_id, query) end defp handle_search_result( @@ -41,6 +48,9 @@ defmodule LogpointApi do _, query ) do + # Wait before recreating the search. + :timer.sleep(1000) + run_search(ip, credential, query) end end From d58d7f7b7679a74377cfbe4fc9d46bd53706a701 Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Wed, 29 May 2024 10:24:27 +0200 Subject: [PATCH 6/6] version: bump minor --- README.md | 2 +- mix.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de6c441..d3e0668 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Elixir library implementing the [Logpoint API reference](https://docs.logpoint.c ```elixir def deps do [ - {:logpoint_api, github: "MikaelFangel/logpoint_api", tag: "v0.1.1"} + {:logpoint_api, github: "MikaelFangel/logpoint_api", tag: "v0.2.1"} ] end ``` diff --git a/mix.exs b/mix.exs index 923ee3e..1fbd90a 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule LogpointApi.MixProject do def project do [ app: :logpoint_api, - version: "0.1.1", + version: "0.2.1", elixir: "~> 1.15", start_permanent: Mix.env() == :prod, deps: deps(),