Skip to content

Commit

Permalink
feat: add X-Watcher-Version header to http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pgebal committed Nov 12, 2020
1 parent d60fc03 commit fc904aa
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 26 deletions.
28 changes: 28 additions & 0 deletions apps/omg_utils/lib/omg_utils/app_version.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2019-2020 OmiseGO Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule OMG.Utils.AppVersion do
@moduledoc false

@sha String.replace(elem(System.cmd("git", ["rev-parse", "--short=7", "HEAD"]), 0), "\n", "")

@doc """
Derive the running service's version for adding to a response.
"""
@spec version(Application.app()) :: String.t()
def version(app) do
{:ok, vsn} = :application.get_key(app, :vsn)
List.to_string(vsn) <> "+" <> @sha
end
end
8 changes: 7 additions & 1 deletion apps/omg_utils/lib/omg_utils/http_rpc/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ defmodule OMG.Utils.HttpRPC.Adapter do
Provides functions to communicate with Child Chain API
"""

alias OMG.Utils.AppVersion

require Logger

@doc """
Makes HTTP POST request to the API
"""
def rpc_post(body, path, url) do
addr = "#{url}/#{path}"
headers = [{"content-type", "application/json"}]
headers = [{"content-type", "application/json"}, {"X-Watcher-Version", x_watcher_version()}]

with {:ok, body} <- Jason.encode(body),
{:ok, %HTTPoison.Response{} = response} <- HTTPoison.post(addr, body, headers) do
Expand Down Expand Up @@ -84,4 +86,8 @@ defmodule OMG.Utils.HttpRPC.Adapter do
|> Stream.map(fn {k, v} -> {String.to_existing_atom(k), v} end)
|> Map.new()
end

defp x_watcher_version() do
"#{inspect(AppVersion.version(:omg_watcher))}"
end
end
11 changes: 0 additions & 11 deletions apps/omg_utils/lib/omg_utils/http_rpc/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ defmodule OMG.Utils.HttpRPC.Response do
"""
alias OMG.Utils.HttpRPC.Encoding

@sha String.replace(elem(System.cmd("git", ["rev-parse", "--short=7", "HEAD"]), 0), "\n", "")

@type response_t :: %{version: binary(), success: boolean(), data: map()}

def serialize_page(data, data_paging) do
Expand Down Expand Up @@ -74,15 +72,6 @@ defmodule OMG.Utils.HttpRPC.Response do
def sanitize({key, value}), do: Map.put_new(%{}, key, value)
def sanitize(value), do: value

@doc """
Derive the running service's version for adding to a response.
"""
@spec version(Application.app()) :: String.t()
def version(app) do
{:ok, vsn} = :application.get_key(app, :vsn)
List.to_string(vsn) <> "+" <> @sha
end

defp do_filter(map_or_struct) do
if :code.is_loaded(Ecto) do
Enum.filter(map_or_struct, fn
Expand Down
27 changes: 27 additions & 0 deletions apps/omg_utils/test/omg_utils/app_version_tet.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2019-2020 OmiseGO Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule OMG.Utils.AppVersionTest do
use ExUnit.Case, async: true

alias OMG.Utils.AppVersion

describe "version/1" do
test "returns a compliant semver when given an application" do
# Using :elixir as the app because it is certain to be running during the test
version = AppVersion.version(:elixir)
assert {:ok, _} = Version.parse(version)
end
end
end
8 changes: 0 additions & 8 deletions apps/omg_utils/test/omg_utils/http_rpc/response_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ defmodule OMG.Utils.HttpRPC.ResponseTest do
assert response |> Map.get(:skip_hex_encode) |> is_nil()
end

describe "version/1" do
test "returns a compliant semver when given an application" do
# Using :elixir as the app because it is certain to be running during the test
version = Response.version(:elixir)
assert {:ok, _} = Version.parse(version)
end
end

defp unload_ecto() do
:code.purge(Ecto)
:code.delete(Ecto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ defmodule OMG.WatcherInfo.HttpRPC.Adapter do
Provides functions to communicate with Child Chain API
"""

alias OMG.Utils.AppVersion

require Logger

@doc """
Makes HTTP POST request to the API
"""
def rpc_post(body, path, url) do
addr = "#{url}/#{path}"
headers = [{"content-type", "application/json"}]
headers = [{"content-type", "application/json"}, {"X-Watcher-Version", x_watcher_version()}]

with {:ok, body} <- Jason.encode(body),
{:ok, %HTTPoison.Response{} = response} <- HTTPoison.post(addr, body, headers) do
Expand Down Expand Up @@ -82,4 +84,8 @@ defmodule OMG.WatcherInfo.HttpRPC.Adapter do
|> Stream.map(fn {k, v} -> {String.to_existing_atom(k), v} end)
|> Map.new()
end

defp x_watcher_version() do
"#{inspect(AppVersion.version(:omg_watcher_info))}"
end
end
8 changes: 3 additions & 5 deletions apps/omg_watcher_rpc/lib/web/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule OMG.WatcherRPC.Web.Response do
For the generic response, see `OMG.Utils.HttpRPC.Response`.
"""

alias OMG.Utils.AppVersion

@app :omg_watcher_rpc

@doc """
Expand All @@ -28,14 +30,10 @@ defmodule OMG.WatcherRPC.Web.Response do
@spec add_app_infos(map()) :: %{version: String.t(), service_name: String.t()}
def add_app_infos(response) do
response
|> Map.put(:version, version())
|> Map.put(:version, AppVersion.version(@app))
|> Map.put(:service_name, service_name())
end

defp version() do
OMG.Utils.HttpRPC.Response.version(@app)
end

defp service_name() do
case Application.get_env(@app, :api_mode) do
:watcher -> "watcher"
Expand Down

0 comments on commit fc904aa

Please sign in to comment.