Skip to content

Commit

Permalink
Revert "Support a second Client Gateway URL (#900)" (#942)
Browse files Browse the repository at this point in the history
This reverts commit 77d0ec2.
  • Loading branch information
fmrsabino authored Sep 7, 2023
1 parent fa0487c commit 60b852c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 386 deletions.
2 changes: 0 additions & 2 deletions .dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ GUNICORN_WEB_RELOAD=false

# The Client Gateway URL. This is for triggering webhooks to invalidate its cache for example
#CGW_URL=http://127.0.0.1
#ALTERNATIVE_CGW_URL=

# The Client Gateway /flush token.
#CGW_FLUSH_TOKEN=example-flush-token
#ALTERNATIVE_CGW_FLUSH_TOKEN=example-flush-token

# What CPU and memory constraints will be added to your services? When left at
# 0, they will happily use as much as needed.
Expand Down
2 changes: 0 additions & 2 deletions src/chains/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def _flush_cgw_chains() -> None:
clients.safe_client_gateway.flush(
cgw_url=settings.CGW_URL,
cgw_flush_token=settings.CGW_FLUSH_TOKEN,
alternative_cgw_url=settings.ALTERNATIVE_CGW_URL,
alternative_cgw_flush_token=settings.ALTERNATIVE_CGW_FLUSH_TOKEN,
json={"invalidate": "Chains"},
)

Expand Down
96 changes: 5 additions & 91 deletions src/chains/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

@override_settings(
CGW_URL="http://127.0.0.1",
ALTERNATIVE_CGW_URL="http://alternative.cgw.url",
CGW_FLUSH_TOKEN="example-token",
ALTERNATIVE_CGW_FLUSH_TOKEN="alternative-token",
)
class ChainNetworkHookTestCase(TestCase):
@responses.activate
Expand All @@ -25,35 +23,17 @@ def test_on_chain_update_hook_200(self) -> None:
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)
responses.add(
responses.POST,
"http://alternative.cgw.url/v2/flush",
status=200,
match=[
responses.matchers.header_matcher(
{"Authorization": "Basic alternative-token"}
),
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)

ChainFactory.create()

assert len(responses.calls) == 2
assert len(responses.calls) == 1
assert isinstance(responses.calls[0], responses.Call)
assert responses.calls[0].request.body == b'{"invalidate": "Chains"}'
assert responses.calls[0].request.url == "http://127.0.0.1/v2/flush"
assert (
responses.calls[0].request.headers.get("Authorization")
== "Basic example-token"
)
assert isinstance(responses.calls[1], responses.Call)
assert responses.calls[1].request.body == b'{"invalidate": "Chains"}'
assert responses.calls[1].request.url == "http://alternative.cgw.url/v2/flush"
assert (
responses.calls[1].request.headers.get("Authorization")
== "Basic alternative-token"
)

@responses.activate
def test_on_chain_update_hook_400(self) -> None:
Expand All @@ -68,21 +48,10 @@ def test_on_chain_update_hook_400(self) -> None:
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)
responses.add(
responses.POST,
"http://alternative.cgw.url/v2/flush",
status=400,
match=[
responses.matchers.header_matcher(
{"Authorization": "Basic alternative-token"}
),
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)

ChainFactory.create()

assert len(responses.calls) == 2
assert len(responses.calls) == 1

@responses.activate
def test_on_chain_update_hook_500(self) -> None:
Expand All @@ -97,21 +66,10 @@ def test_on_chain_update_hook_500(self) -> None:
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)
responses.add(
responses.POST,
"http://alternative.cgw.url/v2/flush",
status=500,
match=[
responses.matchers.header_matcher(
{"Authorization": "Basic alternative-token"}
),
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)

ChainFactory.create()

assert len(responses.calls) == 2
assert len(responses.calls) == 1

@responses.activate
def test_on_chain_delete_hook_call(self) -> None:
Expand All @@ -135,9 +93,7 @@ def test_on_chain_update_hook_call(self) -> None:

@override_settings(
CGW_URL=None,
ALTERNATIVE_CGW_URL="http://alternative.cgw.url",
CGW_FLUSH_TOKEN=None,
ALTERNATIVE_CGW_FLUSH_TOKEN="alternative-token",
)
@responses.activate
def test_on_chain_update_with_no_cgw_set(self) -> None:
Expand All @@ -147,9 +103,7 @@ def test_on_chain_update_with_no_cgw_set(self) -> None:

@override_settings(
CGW_URL="http://127.0.0.1",
ALTERNATIVE_CGW_URL="http://alternative.cgw.url",
CGW_FLUSH_TOKEN=None,
ALTERNATIVE_CGW_FLUSH_TOKEN="alternative-token",
)
@responses.activate
def test_on_chain_update_with_no_flush_token_set(self) -> None:
Expand All @@ -160,9 +114,7 @@ def test_on_chain_update_with_no_flush_token_set(self) -> None:

@override_settings(
CGW_URL="http://127.0.0.1",
ALTERNATIVE_CGW_URL="http://alternative.cgw.url",
CGW_FLUSH_TOKEN="example-token",
ALTERNATIVE_CGW_FLUSH_TOKEN="alternative-token",
)
class FeatureHookTestCase(TestCase):
@responses.activate
Expand All @@ -178,35 +130,17 @@ def test_on_feature_create_hook_call(self) -> None:
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)
responses.add(
responses.POST,
"http://alternative.cgw.url/v2/flush",
status=200,
match=[
responses.matchers.header_matcher(
{"Authorization": "Basic alternative-token"}
),
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)

Feature(key="Test Feature").save()

assert len(responses.calls) == 2
assert len(responses.calls) == 1
assert isinstance(responses.calls[0], responses.Call)
assert responses.calls[0].request.body == b'{"invalidate": "Chains"}'
assert responses.calls[0].request.url == "http://127.0.0.1/v2/flush"
assert (
responses.calls[0].request.headers.get("Authorization")
== "Basic example-token"
)
assert isinstance(responses.calls[1], responses.Call)
assert responses.calls[1].request.body == b'{"invalidate": "Chains"}'
assert responses.calls[1].request.url == "http://alternative.cgw.url/v2/flush"
assert (
responses.calls[1].request.headers.get("Authorization")
== "Basic alternative-token"
)

@responses.activate
def test_on_feature_delete_hook_call(self) -> None:
Expand All @@ -232,9 +166,7 @@ def test_on_feature_update_hook_call(self) -> None:

@override_settings(
CGW_URL="http://127.0.0.1",
ALTERNATIVE_CGW_URL="http://alternative.cgw.url",
CGW_FLUSH_TOKEN="example-token",
ALTERNATIVE_CGW_FLUSH_TOKEN="alternative-token",
)
class WalletHookTestCase(TestCase):
@responses.activate
Expand All @@ -250,35 +182,17 @@ def test_on_wallet_create_hook_call(self) -> None:
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)
responses.add(
responses.POST,
"http://alternative.cgw.url/v2/flush",
status=200,
match=[
responses.matchers.header_matcher(
{"Authorization": "Basic alternative-token"}
),
responses.matchers.json_params_matcher({"invalidate": "Chains"}),
],
)

Wallet(key="Test Wallet").save()

assert len(responses.calls) == 2
assert len(responses.calls) == 1
assert isinstance(responses.calls[0], responses.Call)
assert responses.calls[0].request.body == b'{"invalidate": "Chains"}'
assert responses.calls[0].request.url == "http://127.0.0.1/v2/flush"
assert (
responses.calls[0].request.headers.get("Authorization")
== "Basic example-token"
)
assert isinstance(responses.calls[1], responses.Call)
assert responses.calls[1].request.body == b'{"invalidate": "Chains"}'
assert responses.calls[1].request.url == "http://alternative.cgw.url/v2/flush"
assert (
responses.calls[1].request.headers.get("Authorization")
== "Basic alternative-token"
)

@responses.activate
def test_on_wallet_delete_hook_call(self) -> None:
Expand Down
27 changes: 7 additions & 20 deletions src/clients/safe_client_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ def setup_session() -> requests.Session:


def flush(
cgw_url: Optional[str],
cgw_flush_token: Optional[str],
alternative_cgw_url: Optional[str],
alternative_cgw_flush_token: Optional[str],
json: Dict[str, Any],
cgw_url: Optional[str], cgw_flush_token: Optional[str], json: Dict[str, Any]
) -> None:
if cgw_url is None:
logger.error("CGW_URL is not set. Skipping hook call")
Expand All @@ -31,22 +27,13 @@ def flush(
logger.error("CGW_FLUSH_TOKEN is not set. Skipping hook call")
return

targets = [cgw_url]
tokens = [cgw_flush_token]

if alternative_cgw_url is not None and alternative_cgw_flush_token is not None:
targets.append(alternative_cgw_url)
tokens.append(alternative_cgw_flush_token)

urls = list(map(lambda url: urljoin(url, "/v2/flush"), targets))

url = urljoin(cgw_url, "/v2/flush")
try:
for url, token in zip(urls, tokens):
post = setup_session().post(
url,
json=json,
headers={"Authorization": f"Basic {token}"},
)
post = setup_session().post(
url,
json=json,
headers={"Authorization": f"Basic {cgw_flush_token}"},
)
post.raise_for_status()
except Exception as error:
logger.error(error)
2 changes: 0 additions & 2 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@
CORS_URLS_REGEX = r"^/api/.*$"

CGW_URL = os.environ.get("CGW_URL")
ALTERNATIVE_CGW_URL = os.environ.get("ALTERNATIVE_CGW_URL")
CGW_FLUSH_TOKEN = os.environ.get("CGW_FLUSH_TOKEN")
ALTERNATIVE_CGW_FLUSH_TOKEN = os.environ.get("ALTERNATIVE_CGW_FLUSH_TOKEN")

# By default, Django stores files locally, using the MEDIA_ROOT and MEDIA_URL settings.
# (using the default the default FileSystemStorage)
Expand Down
2 changes: 0 additions & 2 deletions src/safe_apps/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ def _flush_cgw_safe_apps() -> None:
clients.safe_client_gateway.flush(
cgw_url=settings.CGW_URL,
cgw_flush_token=settings.CGW_FLUSH_TOKEN,
alternative_cgw_url=settings.ALTERNATIVE_CGW_URL,
alternative_cgw_flush_token=settings.ALTERNATIVE_CGW_FLUSH_TOKEN,
# Even though the payload is Chains, it actually invalidates all the safe-config related cache
json={"invalidate": "Chains"},
)
Expand Down
Loading

0 comments on commit 60b852c

Please sign in to comment.