From 7e95bdd60b46d231e98a08193971d40583688515 Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Fri, 26 Apr 2024 12:26:02 +0200 Subject: [PATCH] logpoint_api: refactored functions --- lib/incident_api.ex | 23 +++++++-------- lib/search_api.ex | 68 ++++++++++++++++++++++----------------------- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/lib/incident_api.ex b/lib/incident_api.ex index 4a29401..2694a6d 100644 --- a/lib/incident_api.ex +++ b/lib/incident_api.ex @@ -1,7 +1,7 @@ defmodule LogpointApi.IncidentApi do alias LogpointApi.Credential, as: Credential - defmodule TimePeriod do + defmodule TimeRange do @derive {Jason.Encoder, only: [:version, :ts_from, :ts_to]} defstruct [:ts_from, :ts_to, version: "0.1"] end @@ -11,14 +11,14 @@ defmodule LogpointApi.IncidentApi do defstruct [:incident_obj_id, :incident_id] end - def get_incidents(ip, %Credential{} = credential, %TimePeriod{} = request_data), - do: get_incident_information(ip, "/incidents", credential, request_data) + def get_incidents(ip, credential, %TimeRange{} = time_range), + do: get_incident_information(ip, "/incidents", credential, time_range) - def get_data_from_incident(ip, %Credential{} = credential, %Incident{} = incident), + def get_data_from_incident(ip, credential, %Incident{} = incident), do: get_incident_information(ip, "/get_data_from_incident", credential, incident) - def get_incident_states(ip, %Credential{} = credential, %TimePeriod{} = request_data), - do: get_incident_information(ip, "/incident_states", credential, request_data) + def get_incident_states(ip, credential, %TimeRange{} = time_range), + do: get_incident_information(ip, "/incident_states", credential, time_range) def get_users(ip, %Credential{} = credential) do params = %{ @@ -40,7 +40,7 @@ defmodule LogpointApi.IncidentApi do end defp make_request(ip, path, params) do - url = "https://" <> ip <> path + url = build_url(ip, path) headers = [{"Content-Type", "application/json"}] body = Jason.encode!(params) # On-prem uses self signed certificates and we thus need to disable the verification. @@ -48,14 +48,15 @@ defmodule LogpointApi.IncidentApi do case HTTPoison.request(:get, url, body, headers, options) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> - body - |> Jason.decode!() + {:ok, Jason.decode!(body)} {:ok, %HTTPoison.Response{status_code: status_code}} -> - IO.puts("Received response with status code #{status_code}") + {:error, "Received response with status code #{status_code}"} {:error, %HTTPoison.Error{reason: reason}} -> - IO.puts("HTTP request failed with reason: #{reason}") + {:error, "HTTP request failed with reason: #{reason}"} end end + + defp build_url(ip, path), do: "https://" <> ip <> path end diff --git a/lib/search_api.ex b/lib/search_api.ex index ddccd74..9e5cabf 100644 --- a/lib/search_api.ex +++ b/lib/search_api.ex @@ -13,65 +13,65 @@ defmodule LogpointApi.SearchApi do defstruct [:search_id] end - def get_user_timezone(ip, %Credential{} = credential), - do: get_allowed_data(ip, %Credential{} = credential, "user_preference") + def get_user_timezone(ip, credential), + do: get_allowed_data(ip, credential, "user_preference") - def get_logpoints(ip, %Credential{} = credential), - do: get_allowed_data(ip, %Credential{} = credential, "loginspects") + def get_logpoints(ip, credential), + do: get_allowed_data(ip, credential, "loginspects") - def get_repos(ip, %Credential{} = credential), - do: get_allowed_data(ip, %Credential{} = credential, "Logpoint_repos") + def get_repos(ip, credential), + do: get_allowed_data(ip, credential, "Logpoint_repos") - def get_devices(ip, %Credential{} = credential), - do: get_allowed_data(ip, %Credential{} = credential, "devices") + def get_devices(ip, credential), + do: get_allowed_data(ip, credential, "devices") - def get_livesearches(ip, %Credential{} = credential), - do: get_allowed_data(ip, %Credential{} = credential, "livesearches") + def get_livesearches(ip, credential), + do: get_allowed_data(ip, credential, "livesearches") - def get_search_id(ip, %Credential{} = credential, %Query{} = request_data), - do: get_search_logs(ip, %Credential{} = credential, request_data) + def get_search_id(ip, credential, %Query{} = query), + do: get_search_logs(ip, credential, query) - def get_search_result(ip, %Credential{} = credential, %SearchID{} = request_data), - do: get_search_logs(ip, %Credential{} = credential, request_data) + def get_search_result(ip, credential, %SearchID{} = search_id), + do: get_search_logs(ip, credential, search_id) defp make_request(ip, path, payload) do - url = "https://" <> ip <> path + url = build_url(ip, path) headers = [{"Content-Type", "application/x-www-form-urlencoded"}] # On-prem uses self signed certificates and we thus need to disable the verification. options = [ssl: [{:verify, :verify_none}]] case HTTPoison.post(url, payload, headers, options) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> - body - |> Jason.decode!() + {:ok, Jason.decode!(body)} {:ok, %HTTPoison.Response{status_code: status_code}} -> - IO.puts("Received response with status code #{status_code}") + {:error, "Received response with status code #{status_code}"} {:error, %HTTPoison.Error{reason: reason}} -> - IO.puts("HTTP request failed with reason: #{reason}") + {:error, "HTTP request failed with reason: #{reason}"} end end - defp get_allowed_data(ip, %Credential{} = credential, type) when type in @allowed_types do - payload = - URI.encode_query(%{ - "username" => credential.username, - "secret_key" => credential.secret_key, - "type" => type - }) + defp build_url(ip, path), do: "https://" <> ip <> path + defp get_allowed_data(ip, credential, type) when type in @allowed_types do + payload = build_payload(credential, %{"type" => type}) make_request(ip, "/getalloweddata", payload) end - defp get_search_logs(ip, %Credential{} = credential, request_data) do - payload = - URI.encode_query(%{ - "username" => credential.username, - "secret_key" => credential.secret_key, - "requestData" => request_data |> Jason.encode!() - }) - + defp get_search_logs(ip, credential, request_data) do + payload = build_payload(credential, %{"requestData" => Jason.encode!(request_data)}) make_request(ip, "/getsearchlogs", payload) end + + defp build_payload(%Credential{} = credential, data) do + Map.merge( + %{ + "username" => credential.username, + "secret_key" => credential.secret_key + }, + data + ) + |> URI.encode_query() + end end