-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crash on startup if configs can't load; don't crash on reload
- Loading branch information
Showing
2 changed files
with
46 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
defmodule StatsigEx.APIClient do | ||
def download_config_specs(api_key, since \\ 0) do | ||
# let it crash if it can't pull the specs. This will prevent startup, but that's probably a good thing | ||
{:ok, resp} = | ||
HTTPoison.get( | ||
"https://statsigapi.net/v1/download_config_specs?sinceTime=#{since}", | ||
[{"STATSIG-API-KEY", api_key}, {"Content-Type", "application/json"}] | ||
) | ||
|
||
Jason.decode(resp.body) | ||
# don't crash here, let the calling process decide | ||
with {:ok, resp} <- | ||
HTTPoison.get( | ||
"https://statsigapi.net/v1/download_config_specs?sinceTime=#{since}", | ||
headers(api_key) | ||
) do | ||
Jason.decode(resp.body) | ||
else | ||
result -> result | ||
end | ||
end | ||
|
||
def push_logs(api_key, logs) do | ||
HTTPoison.post( | ||
"https://statsigapi.net/v1/rgstr", | ||
Jason.encode!(%{"events" => logs}), | ||
[{"STATSIG-API-KEY", api_key}, {"Content-Type", "application/json"}] | ||
headers(api_key) | ||
) | ||
|> case do | ||
{:ok, %{status_code: code}} when code < 300 -> {:ok, []} | ||
_ -> {:error, logs} | ||
end | ||
end | ||
|
||
defp headers(api_key) do | ||
[ | ||
{"STATSIG-API-KEY", api_key}, | ||
{"Content-Type", "application/json"}, | ||
{"STATSIG-SDK-VERSION", "0.0.1"}, | ||
{"STATSIG-SDK-TYPE", "elixir-server"}, | ||
{"STATSIG-CLIENT-TIME", DateTime.utc_now() |> DateTime.to_unix(:millisecond)} | ||
] | ||
end | ||
end |