Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/update_indexer_cookie_policy #308

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

All notable changes to this project will be documented in this file.

## [1.2.0] - 2024-02-12
## [1.3.1] - 2024-02-29
### Changed
- Updated cookie assistant logic to support the Indexer exchange server not using cookies and the chain server using them

## [1.3.0] - 2024-02-12
### Changed
- Removed `asyncio` from the dependencies

Expand Down
15 changes: 11 additions & 4 deletions pyinjective/core/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

async def exchange_metadata(self, metadata_query_provider: Callable) -> Tuple[Tuple[str, str]]:
cookie = await self.exchange_cookie(metadata_query_provider=metadata_query_provider)
return (("cookie", cookie),)
metadata = None
if cookie is not None and cookie != "":
metadata = (("cookie", cookie),)
return metadata


class KubernetesLoadBalancedCookieAssistant(CookieAssistant):
Expand Down Expand Up @@ -67,7 +70,7 @@
cookie_info = next((value for key, value in metadata if key == "set-cookie"), None)

if cookie_info is None:
raise RuntimeError(f"Error fetching exchange cookie ({metadata})")
cookie_info = ""

self._exchange_cookie = cookie_info

Expand All @@ -83,7 +86,11 @@
cookie = SimpleCookie()
cookie.load(cookie_data)

expiration_time = datetime.datetime.strptime(cookie["GCLB"]["expires"], "%a, %d-%b-%Y %H:%M:%S %Z").timestamp()
expiration_data: Optional[str] = cookie.get("GCLB", {}).get("expires", None)
if expiration_data is None:
expiration_time = 0
else:
expiration_time = datetime.datetime.strptime(expiration_data, "%a, %d-%b-%Y %H:%M:%S %Z").timestamp()

Check warning on line 93 in pyinjective/core/network.py

View check run for this annotation

Codecov / codecov/patch

pyinjective/core/network.py#L93

Added line #L93 was not covered by tests

timestamp_diff = expiration_time - time.time()
return timestamp_diff < self.SESSION_RENEWAL_OFFSET
Expand Down Expand Up @@ -130,7 +137,7 @@
cookie_info = next((value for key, value in metadata if key == "set-cookie"), None)

if cookie_info is None:
raise RuntimeError(f"Error fetching exchange cookie ({metadata})")
cookie_info = ""

self._exchange_cookie = cookie_info

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "injective-py"
version = "1.3.0"
version = "1.3.1"
description = "Injective Python SDK, with Exchange API Client"
authors = ["Injective Labs <contact@injectivelabs.org>"]
license = "Apache-2.0"
Expand Down
105 changes: 105 additions & 0 deletions tests/core/test_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import pytest

from pyinjective.core.network import BareMetalLoadBalancedCookieAssistant, KubernetesLoadBalancedCookieAssistant


class TestBareMetalLoadBalancedCookieAssistant:
@pytest.mark.asyncio
async def test_chain_metadata(self):
assistant = BareMetalLoadBalancedCookieAssistant()
dummy_metadata = [("set-cookie", "expected_cookie")]

async def dummy_metadata_provider():
return dummy_metadata

metadata = await assistant.chain_metadata(metadata_query_provider=dummy_metadata_provider)
expected_metadata = (("cookie", "expected_cookie"),)

assert expected_metadata == metadata

@pytest.mark.asyncio
async def test_chain_metadata_fails_when_cookie_info_not_included_in_server_response(self):
assistant = BareMetalLoadBalancedCookieAssistant()
dummy_metadata = [("invalid_key", "invalid_value")]

async def dummy_metadata_provider():
return dummy_metadata

with pytest.raises(RuntimeError, match=f"Error fetching chain cookie ({dummy_metadata})"):
await assistant.chain_metadata(metadata_query_provider=dummy_metadata_provider)

@pytest.mark.asyncio
async def test_exchange_metadata(self):
assistant = BareMetalLoadBalancedCookieAssistant()
dummy_metadata = [("set-cookie", "expected_cookie")]

async def dummy_metadata_provider():
return dummy_metadata

metadata = await assistant.exchange_metadata(metadata_query_provider=dummy_metadata_provider)
expected_metadata = (("cookie", "expected_cookie"),)

assert expected_metadata == metadata

@pytest.mark.asyncio
async def test_exchange_metadata_is_none_when_cookie_info_not_included_in_server_response(self):
assistant = BareMetalLoadBalancedCookieAssistant()
dummy_metadata = [("invalid_key", "invalid_value")]

async def dummy_metadata_provider():
return dummy_metadata

metadata = await assistant.exchange_metadata(metadata_query_provider=dummy_metadata_provider)

assert metadata is None


class TestKubernetesLoadBalancedCookieAssistant:
@pytest.mark.asyncio
async def test_chain_metadata(self):
assistant = KubernetesLoadBalancedCookieAssistant()
dummy_metadata = [("set-cookie", "expected_cookie")]

async def dummy_metadata_provider():
return dummy_metadata

metadata = await assistant.chain_metadata(metadata_query_provider=dummy_metadata_provider)
expected_metadata = (("cookie", "expected_cookie"),)

assert expected_metadata == metadata

@pytest.mark.asyncio
async def test_chain_metadata_fails_when_cookie_info_not_included_in_server_response(self):
assistant = KubernetesLoadBalancedCookieAssistant()
dummy_metadata = [("invalid_key", "invalid_value")]

async def dummy_metadata_provider():
return dummy_metadata

with pytest.raises(RuntimeError, match=f"Error fetching chain cookie ({dummy_metadata})"):
await assistant.chain_metadata(metadata_query_provider=dummy_metadata_provider)

@pytest.mark.asyncio
async def test_exchange_metadata(self):
assistant = KubernetesLoadBalancedCookieAssistant()
dummy_metadata = [("set-cookie", "expected_cookie")]

async def dummy_metadata_provider():
return dummy_metadata

metadata = await assistant.exchange_metadata(metadata_query_provider=dummy_metadata_provider)
expected_metadata = (("cookie", "expected_cookie"),)

assert expected_metadata == metadata

@pytest.mark.asyncio
async def test_exchange_metadata_is_none_when_cookie_info_not_included_in_server_response(self):
assistant = KubernetesLoadBalancedCookieAssistant()
dummy_metadata = [("invalid_key", "invalid_value")]

async def dummy_metadata_provider():
return dummy_metadata

metadata = await assistant.exchange_metadata(metadata_query_provider=dummy_metadata_provider)

assert metadata is None
Loading