From c4ca6ba2b5d45f333f33712081dc03af07b4eb55 Mon Sep 17 00:00:00 2001 From: saneery Date: Mon, 19 Aug 2019 11:34:04 +0300 Subject: [PATCH 01/14] Fix error of url in API page --- apps/block_scout_web/assets/js/lib/try_api.js | 2 +- apps/block_scout_web/assets/js/lib/try_eth_api.js | 5 +++-- .../lib/block_scout_web/views/api_docs_view.ex | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/block_scout_web/assets/js/lib/try_api.js b/apps/block_scout_web/assets/js/lib/try_api.js index c0eb8cd5e3..e6460f6c6b 100644 --- a/apps/block_scout_web/assets/js/lib/try_api.js +++ b/apps/block_scout_web/assets/js/lib/try_api.js @@ -124,7 +124,7 @@ $('button[data-try-api-ui-button-type="execute"]').click(event => { } $.ajax({ - url: `/api${query}`, + url: composeRequestUrl(query), success: (_data, _status, xhr) => { handleSuccess(query, xhr, clickedButton) }, diff --git a/apps/block_scout_web/assets/js/lib/try_eth_api.js b/apps/block_scout_web/assets/js/lib/try_eth_api.js index a3dd7b6f4e..f6e1adffae 100644 --- a/apps/block_scout_web/assets/js/lib/try_eth_api.js +++ b/apps/block_scout_web/assets/js/lib/try_eth_api.js @@ -50,7 +50,6 @@ $('button[data-try-eth-api-ui-button-type="execute"]').click(event => { const inputs = $(`input[data-selector="${module}-${action}-try-api-ui"]`) const params = $.map(inputs, parseInput) const formData = wrapJsonRpc(action, params) - console.log(formData) const loadingText = ' Loading...' clickedButton.prop('disabled', true) @@ -60,8 +59,10 @@ $('button[data-try-eth-api-ui-button-type="execute"]').click(event => { clickedButton.html(loadingText) } + const url = $('[data-endpoint-url]').attr('data-endpoint-url') + $.ajax({ - url: '/api/eth_rpc', + url: url, type: 'POST', data: JSON.stringify(formData), dataType: 'json', diff --git a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex index ac7d4d55ee..895fad79c2 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex @@ -1,7 +1,7 @@ defmodule BlockScoutWeb.APIDocsView do use BlockScoutWeb, :view - alias BlockScoutWeb.{Endpoint, LayoutView} + alias BlockScoutWeb.LayoutView def action_tile_id(module, action) do "#{module}-#{action}" @@ -41,9 +41,10 @@ defmodule BlockScoutWeb.APIDocsView do scheme = url_params[:scheme] if host != "localhost" do - scheme <> "://" <> host <> path + "#{scheme}://#{host}#{path}" else - Endpoint.url() + port = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:http][:port] + "#{scheme}://#{host}:#{to_string(port)}#{path}" end end From 7270b32ffb959ade1480a945b6a578bf3eeb251e Mon Sep 17 00:00:00 2001 From: saneery Date: Mon, 19 Aug 2019 11:43:56 +0300 Subject: [PATCH 02/14] Add double quotes to example of API call --- .../block_scout_web/templates/api_docs/_eth_rpc_item.html.eex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex index 7f342b4a16..57f96e42e0 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex @@ -6,7 +6,7 @@ - curl -X POST --data '{"id":0,"jsonrpc":"2.0","method": "<%= @action %>", params: []}' + curl -X POST --data '{"id":0,"jsonrpc":"2.0","method": "<%= @action %>", "params": []}'

From 13058bea30d7afc79f9303bcd4f72b57b8a20166 Mon Sep 17 00:00:00 2001 From: saneery Date: Mon, 19 Aug 2019 11:47:51 +0300 Subject: [PATCH 03/14] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f006769dbf..649b476ec3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [#2497](https://github.com/poanetwork/blockscout/pull/2497) - Add generic Ordered Cache behaviour and implementation ### Fixes +- [#2591](https://github.com/poanetwork/blockscout/pull/2591) - Fix url error in API page - [#2572](https://github.com/poanetwork/blockscout/pull/2572) - Ease non-critical css - [#2570](https://github.com/poanetwork/blockscout/pull/2570) - Network icons preload - [#2569](https://github.com/poanetwork/blockscout/pull/2569) - do not fetch emission rewards for transactions csv exporter From 14f099b5b35b219873751e184d631bcf234c3a73 Mon Sep 17 00:00:00 2001 From: saneery Date: Mon, 19 Aug 2019 17:11:29 +0300 Subject: [PATCH 04/14] remove additional path in local url --- apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex index 895fad79c2..382a7b26b9 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex @@ -44,7 +44,7 @@ defmodule BlockScoutWeb.APIDocsView do "#{scheme}://#{host}#{path}" else port = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:http][:port] - "#{scheme}://#{host}:#{to_string(port)}#{path}" + "#{scheme}://#{host}:#{to_string(port)}" end end From f1c1e066a7803cd56305ad602fbeb5705c27e699 Mon Sep 17 00:00:00 2001 From: saneery Date: Tue, 20 Aug 2019 10:39:40 +0300 Subject: [PATCH 05/14] Add double quotes to copying string --- .../block_scout_web/templates/api_docs/_eth_rpc_item.html.eex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex index 57f96e42e0..f464533c7e 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex @@ -4,7 +4,7 @@

<%= @action %>

<%= raw @info.notes %>

curl -X POST --data '{"id":0,"jsonrpc":"2.0","method": "<%= @action %>", "params": []}' From 0a647d0acbe3cfcb8b575cf92d420f6ae3268582 Mon Sep 17 00:00:00 2001 From: saneery Date: Wed, 21 Aug 2019 12:06:20 +0300 Subject: [PATCH 06/14] Remove schema from api url --- apps/block_scout_web/assets/js/lib/try_api.js | 6 +++++- apps/block_scout_web/assets/js/lib/try_eth_api.js | 6 +++++- .../lib/block_scout_web/views/api_docs_view.ex | 5 ++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/apps/block_scout_web/assets/js/lib/try_api.js b/apps/block_scout_web/assets/js/lib/try_api.js index e6460f6c6b..02d82a65ef 100644 --- a/apps/block_scout_web/assets/js/lib/try_api.js +++ b/apps/block_scout_web/assets/js/lib/try_api.js @@ -55,6 +55,10 @@ function handleSuccess (query, xhr, clickedButton) { clickedButton.prop('disabled', false) } +function dropDomain (url) { + return url.split('/').slice(1).join('/') +} + // Show 'Try it out' UI for a module/action. $('button[data-selector*="btn-try-api"]').click(event => { const clickedButton = $(event.target) @@ -124,7 +128,7 @@ $('button[data-try-api-ui-button-type="execute"]').click(event => { } $.ajax({ - url: composeRequestUrl(query), + url: '/' + dropDomain(composeRequestUrl(query)), success: (_data, _status, xhr) => { handleSuccess(query, xhr, clickedButton) }, diff --git a/apps/block_scout_web/assets/js/lib/try_eth_api.js b/apps/block_scout_web/assets/js/lib/try_eth_api.js index f6e1adffae..464229885d 100644 --- a/apps/block_scout_web/assets/js/lib/try_eth_api.js +++ b/apps/block_scout_web/assets/js/lib/try_eth_api.js @@ -43,6 +43,10 @@ function parseInput (input) { } } +function dropDomain (url) { + return url.split('/').slice(1).join('/') +} + $('button[data-try-eth-api-ui-button-type="execute"]').click(event => { const clickedButton = $(event.target) const module = clickedButton.attr('data-module') @@ -62,7 +66,7 @@ $('button[data-try-eth-api-ui-button-type="execute"]').click(event => { const url = $('[data-endpoint-url]').attr('data-endpoint-url') $.ajax({ - url: url, + url: '/' + dropDomain(url), type: 'POST', data: JSON.stringify(formData), dataType: 'json', diff --git a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex index 382a7b26b9..6f731d3627 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex @@ -38,13 +38,12 @@ defmodule BlockScoutWeb.APIDocsView do url_params = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:url] host = url_params[:host] path = url_params[:path] - scheme = url_params[:scheme] if host != "localhost" do - "#{scheme}://#{host}#{path}" + "#{host}#{path}" else port = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:http][:port] - "#{scheme}://#{host}:#{to_string(port)}" + "#{host}:#{to_string(port)}" end end From 1ddbbb4b4063090a4df352bf0a7459dc1cc758db Mon Sep 17 00:00:00 2001 From: saneery Date: Wed, 21 Aug 2019 12:41:24 +0300 Subject: [PATCH 07/14] fix tests --- .../block_scout_web/views/api_docs_view_test.exs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs index 7583aeaa4f..69be27ef09 100644 --- a/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs @@ -17,7 +17,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/"] ) - assert APIDocsView.blockscout_url() == "https://blockscout.com/" + assert APIDocsView.blockscout_url() == "blockscout.com/" end test "returns url with scheme and host with path" do @@ -25,7 +25,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/chain/dog"] ) - assert APIDocsView.blockscout_url() == "https://blockscout.com/chain/dog" + assert APIDocsView.blockscout_url() == "blockscout.com/chain/dog" end end @@ -43,7 +43,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/chain/dog"] ) - assert APIDocsView.api_url() == "https://blockscout.com/chain/dog/api" + assert APIDocsView.api_url() == "blockscout.com/chain/dog/api" end test "does not add slash to empty path" do @@ -51,7 +51,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: ""] ) - assert APIDocsView.api_url() == "https://blockscout.com/api" + assert APIDocsView.api_url() == "blockscout.com/api" end end @@ -69,7 +69,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/chain/dog"] ) - assert APIDocsView.eth_rpc_api_url() == "https://blockscout.com/chain/dog/api/eth_rpc" + assert APIDocsView.eth_rpc_api_url() == "blockscout.com/chain/dog/api/eth_rpc" end test "does not add slash to empty path" do @@ -77,7 +77,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: ""] ) - assert APIDocsView.eth_rpc_api_url() == "https://blockscout.com/api/eth_rpc" + assert APIDocsView.eth_rpc_api_url() == "blockscout.com/api/eth_rpc" end end end From 2db7702cef68d187a1662cdf8457aacaed066a6a Mon Sep 17 00:00:00 2001 From: saneery Date: Wed, 28 Aug 2019 14:29:58 +0300 Subject: [PATCH 08/14] Add scheme in api base url --- .../templates/api_docs/eth_rpc.html.eex | 2 +- .../templates/api_docs/index.html.eex | 2 +- .../block_scout_web/views/api_docs_view.ex | 26 ++++---- .../views/api_docs_view_test.exs | 64 +++++++++++++------ 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/eth_rpc.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/eth_rpc.html.eex index 7b8a9a98de..ad27e68185 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/eth_rpc.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/eth_rpc.html.eex @@ -2,7 +2,7 @@

<%= gettext("ETH RPC API Documentation") %>

-

[ <%= gettext "Base URL:" %> <%= eth_rpc_api_url()%> ]

+

[ <%= gettext "Base URL:" %> <%= eth_rpc_api_url(with_scheme: true)%> ]

<%= gettext "This API is provided to support some rpc methods in the exact format specified for ethereum nodes, which can be found " %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/index.html.eex index 59dae38cdb..10ff5a6c27 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/index.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/index.html.eex @@ -2,7 +2,7 @@

<%= gettext("API Documentation") %>

-

[ <%= gettext "Base URL:" %> <%= api_url()%> ]

+

[ <%= gettext "Base URL:" %> <%= api_url(with_scheme: true)%> ]

<%= gettext "This API is provided for developers transitioning their applications from Etherscan to BlockScout. It supports GET and POST requests." %>

diff --git a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex index 6f731d3627..c30364e96b 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex @@ -34,30 +34,30 @@ defmodule BlockScoutWeb.APIDocsView do end) end - def blockscout_url do + defp blockscout_url(opts) do url_params = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:url] host = url_params[:host] path = url_params[:path] + scheme = + if Keyword.get(opts, :with_scheme, false) do + url_params[:scheme] <> "://" + end if host != "localhost" do - "#{host}#{path}" + "#{scheme}#{host}#{path}" else port = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:http][:port] - "#{host}:#{to_string(port)}" + "#{scheme}#{host}:#{to_string(port)}" end end - def api_url do - handle_slash("api") + def api_url(opts \\ []) do + base_url = blockscout_url(opts) + Path.join(base_url, "api") end - def eth_rpc_api_url do - handle_slash("api/eth_rpc") - end - - defp handle_slash(path) do - base_url = blockscout_url() - - Path.join(base_url, path) + def eth_rpc_api_url(opts \\ []) do + base_url = blockscout_url(opts) + Path.join(base_url, "api/eth_rpc") end end diff --git a/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs index 69be27ef09..bd2bb6cd05 100644 --- a/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs @@ -3,7 +3,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do alias BlockScoutWeb.APIDocsView - describe "blockscout_url/0" do + describe "api_url/1" do setup do original = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint) @@ -12,46 +12,44 @@ defmodule BlockScoutWeb.ApiDocsViewTest do :ok end - test "returns url with scheme and host without port" do + test "adds slash before path" do Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, - url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/"] + url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/chain/dog"] ) - assert APIDocsView.blockscout_url() == "blockscout.com/" + assert APIDocsView.api_url() == "blockscout.com/chain/dog/api" end - test "returns url with scheme and host with path" do + test "does not add slash to empty path" do Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, - url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/chain/dog"] + url: [scheme: "https", host: "blockscout.com", port: 9999, path: ""] ) - assert APIDocsView.blockscout_url() == "blockscout.com/chain/dog" + assert APIDocsView.api_url() == "blockscout.com/api" end - end - describe "api_url/1" do - setup do - original = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint) - - on_exit(fn -> Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, original) end) + test "path with schems" do + Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, + url: [scheme: "https", host: "blockscout.com", port: 9999] + ) - :ok - end + assert APIDocsView.api_url(with_scheme: true) == "https://blockscout.com/api" - test "adds slash before path" do Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, - url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/chain/dog"] + url: [scheme: "http", host: "blockscout.com", port: 9999] ) - assert APIDocsView.api_url() == "blockscout.com/chain/dog/api" + assert APIDocsView.api_url(with_scheme: true) == "http://blockscout.com/api" end - test "does not add slash to empty path" do + test "localhost return with port" do Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, - url: [scheme: "https", host: "blockscout.com", port: 9999, path: ""] + url: [scheme: "http", host: "localhost"], + http: [port: 9999] ) - assert APIDocsView.api_url() == "blockscout.com/api" + assert APIDocsView.api_url() == "localhost:9999/api" + assert APIDocsView.api_url(with_scheme: true) == "http://localhost:9999/api" end end @@ -79,5 +77,29 @@ defmodule BlockScoutWeb.ApiDocsViewTest do assert APIDocsView.eth_rpc_api_url() == "blockscout.com/api/eth_rpc" end + + test "path with schems" do + Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, + url: [scheme: "https", host: "blockscout.com", port: 9999] + ) + + assert APIDocsView.eth_rpc_api_url(with_scheme: true) == "https://blockscout.com/api/eth_rpc" + + Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, + url: [scheme: "http", host: "blockscout.com", port: 9999] + ) + + assert APIDocsView.eth_rpc_api_url(with_scheme: true) == "http://blockscout.com/api/eth_rpc" + end + + test "localhost return with port" do + Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, + url: [scheme: "http", host: "localhost"], + http: [port: 9999] + ) + + assert APIDocsView.eth_rpc_api_url() == "localhost:9999/api/eth_rpc" + assert APIDocsView.eth_rpc_api_url(with_scheme: true) == "http://localhost:9999/api/eth_rpc" + end end end From aabb0069405954d98f2ccb73d29428d5fec9233d Mon Sep 17 00:00:00 2001 From: saneery Date: Wed, 28 Aug 2019 14:36:36 +0300 Subject: [PATCH 09/14] Add host in an example of curl request --- apps/block_scout_web/assets/js/lib/try_eth_api.js | 3 ++- .../block_scout_web/lib/block_scout_web/views/api_docs_view.ex | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/block_scout_web/assets/js/lib/try_eth_api.js b/apps/block_scout_web/assets/js/lib/try_eth_api.js index 464229885d..8f81f852ec 100644 --- a/apps/block_scout_web/assets/js/lib/try_eth_api.js +++ b/apps/block_scout_web/assets/js/lib/try_eth_api.js @@ -1,7 +1,8 @@ import $ from 'jquery' function composeCurlCommand (data) { - return `curl -H "content-type: application/json" -X POST --data '${JSON.stringify(data)}'` + const url = $('[data-endpoint-url]').attr('data-endpoint-url') + return `curl ${url} -H "content-type: application/json" -X POST --data '${JSON.stringify(data)}'` } function handleResponse (data, xhr, clickedButton) { diff --git a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex index c30364e96b..a9b816abb9 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex @@ -38,6 +38,7 @@ defmodule BlockScoutWeb.APIDocsView do url_params = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:url] host = url_params[:host] path = url_params[:path] + scheme = if Keyword.get(opts, :with_scheme, false) do url_params[:scheme] <> "://" From d167795623e29cd840d08b576cf0afbb2b721a2c Mon Sep 17 00:00:00 2001 From: saneery Date: Wed, 28 Aug 2019 15:19:48 +0300 Subject: [PATCH 10/14] fix test --- .../lib/block_scout_web/views/api_docs_view.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex index a9b816abb9..eabd883df9 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex @@ -40,8 +40,8 @@ defmodule BlockScoutWeb.APIDocsView do path = url_params[:path] scheme = - if Keyword.get(opts, :with_scheme, false) do - url_params[:scheme] <> "://" + if Keyword.get(opts, :with_scheme, false) && url_params[:scheme] do + "#{url_params[:scheme]}://" end if host != "localhost" do From d40aaef0c6c889986cf81c77c76ed66601b8de01 Mon Sep 17 00:00:00 2001 From: saneery Date: Thu, 29 Aug 2019 10:28:39 +0300 Subject: [PATCH 11/14] Add scheme in api urls --- .../templates/api_docs/eth_rpc.html.eex | 2 +- .../templates/api_docs/index.html.eex | 2 +- .../block_scout_web/views/api_docs_view.ex | 20 ++++----- .../views/api_docs_view_test.exs | 42 +++---------------- 4 files changed, 16 insertions(+), 50 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/eth_rpc.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/eth_rpc.html.eex index ad27e68185..7b8a9a98de 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/eth_rpc.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/eth_rpc.html.eex @@ -2,7 +2,7 @@

<%= gettext("ETH RPC API Documentation") %>

-

[ <%= gettext "Base URL:" %> <%= eth_rpc_api_url(with_scheme: true)%> ]

+

[ <%= gettext "Base URL:" %> <%= eth_rpc_api_url()%> ]

<%= gettext "This API is provided to support some rpc methods in the exact format specified for ethereum nodes, which can be found " %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/index.html.eex index 10ff5a6c27..59dae38cdb 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/api_docs/index.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/api_docs/index.html.eex @@ -2,7 +2,7 @@

<%= gettext("API Documentation") %>

-

[ <%= gettext "Base URL:" %> <%= api_url(with_scheme: true)%> ]

+

[ <%= gettext "Base URL:" %> <%= api_url()%> ]

<%= gettext "This API is provided for developers transitioning their applications from Etherscan to BlockScout. It supports GET and POST requests." %>

diff --git a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex index eabd883df9..e067c997d2 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex @@ -34,31 +34,27 @@ defmodule BlockScoutWeb.APIDocsView do end) end - defp blockscout_url(opts) do + defp blockscout_url do url_params = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:url] host = url_params[:host] path = url_params[:path] - - scheme = - if Keyword.get(opts, :with_scheme, false) && url_params[:scheme] do - "#{url_params[:scheme]}://" - end + scheme = Keyword.get(url_params, :scheme, "http") if host != "localhost" do - "#{scheme}#{host}#{path}" + "#{scheme}://#{host}#{path}" else port = Application.get_env(:block_scout_web, BlockScoutWeb.Endpoint)[:http][:port] - "#{scheme}#{host}:#{to_string(port)}" + "#{scheme}://#{host}:#{to_string(port)}" end end - def api_url(opts \\ []) do - base_url = blockscout_url(opts) + def api_url do + base_url = blockscout_url() Path.join(base_url, "api") end - def eth_rpc_api_url(opts \\ []) do - base_url = blockscout_url(opts) + def eth_rpc_api_url do + base_url = blockscout_url() Path.join(base_url, "api/eth_rpc") end end diff --git a/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs index bd2bb6cd05..31922b4486 100644 --- a/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/api_docs_view_test.exs @@ -17,7 +17,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/chain/dog"] ) - assert APIDocsView.api_url() == "blockscout.com/chain/dog/api" + assert APIDocsView.api_url() == "https://blockscout.com/chain/dog/api" end test "does not add slash to empty path" do @@ -25,21 +25,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: ""] ) - assert APIDocsView.api_url() == "blockscout.com/api" - end - - test "path with schems" do - Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, - url: [scheme: "https", host: "blockscout.com", port: 9999] - ) - - assert APIDocsView.api_url(with_scheme: true) == "https://blockscout.com/api" - - Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, - url: [scheme: "http", host: "blockscout.com", port: 9999] - ) - - assert APIDocsView.api_url(with_scheme: true) == "http://blockscout.com/api" + assert APIDocsView.api_url() == "https://blockscout.com/api" end test "localhost return with port" do @@ -48,8 +34,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do http: [port: 9999] ) - assert APIDocsView.api_url() == "localhost:9999/api" - assert APIDocsView.api_url(with_scheme: true) == "http://localhost:9999/api" + assert APIDocsView.api_url() == "http://localhost:9999/api" end end @@ -67,7 +52,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: "/chain/dog"] ) - assert APIDocsView.eth_rpc_api_url() == "blockscout.com/chain/dog/api/eth_rpc" + assert APIDocsView.eth_rpc_api_url() == "https://blockscout.com/chain/dog/api/eth_rpc" end test "does not add slash to empty path" do @@ -75,21 +60,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do url: [scheme: "https", host: "blockscout.com", port: 9999, path: ""] ) - assert APIDocsView.eth_rpc_api_url() == "blockscout.com/api/eth_rpc" - end - - test "path with schems" do - Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, - url: [scheme: "https", host: "blockscout.com", port: 9999] - ) - - assert APIDocsView.eth_rpc_api_url(with_scheme: true) == "https://blockscout.com/api/eth_rpc" - - Application.put_env(:block_scout_web, BlockScoutWeb.Endpoint, - url: [scheme: "http", host: "blockscout.com", port: 9999] - ) - - assert APIDocsView.eth_rpc_api_url(with_scheme: true) == "http://blockscout.com/api/eth_rpc" + assert APIDocsView.eth_rpc_api_url() == "https://blockscout.com/api/eth_rpc" end test "localhost return with port" do @@ -98,8 +69,7 @@ defmodule BlockScoutWeb.ApiDocsViewTest do http: [port: 9999] ) - assert APIDocsView.eth_rpc_api_url() == "localhost:9999/api/eth_rpc" - assert APIDocsView.eth_rpc_api_url(with_scheme: true) == "http://localhost:9999/api/eth_rpc" + assert APIDocsView.eth_rpc_api_url() == "http://localhost:9999/api/eth_rpc" end end end From 37da2eaa6e5500b493e892e2c4da8358ba52d6a6 Mon Sep 17 00:00:00 2001 From: saneery Date: Thu, 29 Aug 2019 10:30:05 +0300 Subject: [PATCH 12/14] Move api's url to the end of example --- apps/block_scout_web/assets/js/lib/try_eth_api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/block_scout_web/assets/js/lib/try_eth_api.js b/apps/block_scout_web/assets/js/lib/try_eth_api.js index 8f81f852ec..c59d71dbcc 100644 --- a/apps/block_scout_web/assets/js/lib/try_eth_api.js +++ b/apps/block_scout_web/assets/js/lib/try_eth_api.js @@ -2,7 +2,7 @@ import $ from 'jquery' function composeCurlCommand (data) { const url = $('[data-endpoint-url]').attr('data-endpoint-url') - return `curl ${url} -H "content-type: application/json" -X POST --data '${JSON.stringify(data)}'` + return `curl -H "content-type: application/json" -X POST --data '${JSON.stringify(data)}' ${url}` } function handleResponse (data, xhr, clickedButton) { From a86b95cbe6c72eb33b496343eca196c66abd2c0a Mon Sep 17 00:00:00 2001 From: saneery Date: Thu, 29 Aug 2019 10:35:37 +0300 Subject: [PATCH 13/14] Use pipeline --- .../lib/block_scout_web/views/api_docs_view.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex index e067c997d2..363a4a0179 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api_docs_view.ex @@ -49,12 +49,12 @@ defmodule BlockScoutWeb.APIDocsView do end def api_url do - base_url = blockscout_url() - Path.join(base_url, "api") + blockscout_url() + |> Path.join("api") end def eth_rpc_api_url do - base_url = blockscout_url() - Path.join(base_url, "api/eth_rpc") + blockscout_url() + |> Path.join("api/eth_rpc") end end From b479aae84a152b02a642ac35a1ab19aa7f817cef Mon Sep 17 00:00:00 2001 From: saneery Date: Thu, 29 Aug 2019 17:17:54 +0300 Subject: [PATCH 14/14] Set not full path to api on ajax queries --- apps/block_scout_web/assets/js/lib/try_api.js | 4 ++-- apps/block_scout_web/assets/js/lib/try_eth_api.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/block_scout_web/assets/js/lib/try_api.js b/apps/block_scout_web/assets/js/lib/try_api.js index 02d82a65ef..6c33779f30 100644 --- a/apps/block_scout_web/assets/js/lib/try_api.js +++ b/apps/block_scout_web/assets/js/lib/try_api.js @@ -56,7 +56,7 @@ function handleSuccess (query, xhr, clickedButton) { } function dropDomain (url) { - return url.split('/').slice(1).join('/') + return new URL(url).pathname } // Show 'Try it out' UI for a module/action. @@ -128,7 +128,7 @@ $('button[data-try-api-ui-button-type="execute"]').click(event => { } $.ajax({ - url: '/' + dropDomain(composeRequestUrl(query)), + url: dropDomain(composeRequestUrl(query)), success: (_data, _status, xhr) => { handleSuccess(query, xhr, clickedButton) }, diff --git a/apps/block_scout_web/assets/js/lib/try_eth_api.js b/apps/block_scout_web/assets/js/lib/try_eth_api.js index c59d71dbcc..30299431be 100644 --- a/apps/block_scout_web/assets/js/lib/try_eth_api.js +++ b/apps/block_scout_web/assets/js/lib/try_eth_api.js @@ -45,7 +45,7 @@ function parseInput (input) { } function dropDomain (url) { - return url.split('/').slice(1).join('/') + return new URL(url).pathname } $('button[data-try-eth-api-ui-button-type="execute"]').click(event => { @@ -67,7 +67,7 @@ $('button[data-try-eth-api-ui-button-type="execute"]').click(event => { const url = $('[data-endpoint-url]').attr('data-endpoint-url') $.ajax({ - url: '/' + dropDomain(url), + url: dropDomain(url), type: 'POST', data: JSON.stringify(formData), dataType: 'json',