Skip to content

Commit

Permalink
feat: add run_search (#13)
Browse files Browse the repository at this point in the history
* search_api: simplify using get search result

* logpoint_api: add initial run_search function

* logpoint_api: add missing s in success

* logpoint_api: make run_search fail fast

* logpoint_api: add doc and fix spec

* version: bump minor
  • Loading branch information
MikaelFangel authored May 29, 2024
1 parent c18e4bb commit a03ad82
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
45 changes: 45 additions & 0 deletions lib/logpoint_api.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
defmodule LogpointApi do
@moduledoc false

alias LogpointApi.SearchApi.Query
alias LogpointApi.SearchApi

defmodule Credential do
@typedoc """
Struct representing credentials used for authorization.
"""
@type t :: %__MODULE__{username: String.t(), secret_key: String.t()}
defstruct [:username, :secret_key]
end

@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")

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, "success" => true}},
ip,
credential,
search_id,
query
) do
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(
{:ok, %{"final" => false, "success" => false}},
ip,
credential,
_,
query
) do
# Wait before recreating the search.
:timer.sleep(1000)

run_search(ip, credential, query)
end
end
7 changes: 3 additions & 4 deletions lib/search_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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()}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit a03ad82

Please sign in to comment.