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

feat: add handler to nc creation list API #358

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions gateways/clients/hathor_core_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
NC_BLUEPRINT_SOURCE_CODE_ENDPOINT = "/v1a/nano_contract/blueprint/source"
NC_BUILTIN_BLUEPRINTS_ENDPOINT = "/v1a/nano_contract/blueprint/builtin"
NC_ON_CHAIN_BLUEPRINTS_ENDPOINT = "/v1a/nano_contract/blueprint/on_chain"
NC_CREATION_LIST_ENDPOINT = "/v1a/nano_contract/creation/"
STATUS_ENDPOINT = "/v1a/status"
TOKEN_ENDPOINT = "/v1a/thin_wallet/token"
TOKEN_HISTORY_ENDPOINT = "/v1a/thin_wallet/token_history"
Expand Down
34 changes: 26 additions & 8 deletions gateways/node_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
NC_BLUEPRINT_INFORMATION_ENDPOINT,
NC_BLUEPRINT_SOURCE_CODE_ENDPOINT,
NC_BUILTIN_BLUEPRINTS_ENDPOINT,
NC_CREATION_LIST_ENDPOINT,
NC_HISTORY_ENDPOINT,
NC_ON_CHAIN_BLUEPRINTS_ENDPOINT,
NC_STATE_ENDPOINT,
Expand Down Expand Up @@ -271,8 +272,7 @@ def get_nc_builtin_blueprints(
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
find_blueprint_id: Optional[str] = None,
find_blueprint_name: Optional[str] = None,
search: Optional[str] = None,
) -> Optional[dict]:
"""Get the list of built in blueprints."""
return self.hathor_core_client.get(
Expand All @@ -281,8 +281,7 @@ def get_nc_builtin_blueprints(
"after": after,
"count": count,
"before": before,
"find_blueprint_id": find_blueprint_id,
"find_blueprint_name": find_blueprint_name,
"search": search,
},
timeout=NODE_API_TIMEOUT_IN_SECONDS,
)
Expand All @@ -292,8 +291,7 @@ def get_nc_on_chain_blueprints(
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
find_blueprint_id: Optional[str] = None,
find_blueprint_name: Optional[str] = None,
search: Optional[str] = None,
order: Optional[str] = None,
) -> Optional[dict]:
"""Get the list of on chain blueprints."""
Expand All @@ -303,8 +301,28 @@ def get_nc_on_chain_blueprints(
"after": after,
"count": count,
"before": before,
"find_blueprint_id": find_blueprint_id,
"find_blueprint_name": find_blueprint_name,
"search": search,
"order": order,
},
timeout=NODE_API_TIMEOUT_IN_SECONDS,
)

def get_nc_creation_list(
self,
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
search: Optional[str] = None,
order: Optional[str] = None,
) -> Optional[dict]:
"""Get the list nano contract creations."""
return self.hathor_core_client.get(
NC_CREATION_LIST_ENDPOINT,
params={
"after": after,
"count": count,
"before": before,
"search": search,
"order": order,
},
timeout=NODE_API_TIMEOUT_IN_SECONDS,
Expand Down
48 changes: 39 additions & 9 deletions handlers/node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,10 @@ def nc_builtin_blueprints(
after = event.query.get("after")
before = event.query.get("before")
count = event.query.get("count")
find_blueprint_id = event.query.get("find_blueprint_id")
find_blueprint_name = event.query.get("find_blueprint_name")
search = event.query.get("search")

# This might throw HathorCoreTimeout error
response = node_api.get_nc_builtin_blueprints(
after, before, count, find_blueprint_id, find_blueprint_name
)
response = node_api.get_nc_builtin_blueprints(after, before, count, search)

if response is None or "error" in response:
message = (
Expand All @@ -472,13 +469,46 @@ def nc_on_chain_blueprints(
after = event.query.get("after")
before = event.query.get("before")
count = event.query.get("count")
find_blueprint_id = event.query.get("find_blueprint_id")
find_blueprint_name = event.query.get("find_blueprint_name")
search = event.query.get("search")
order = event.query.get("order")

# This might throw HathorCoreTimeout error
response = node_api.get_nc_on_chain_blueprints(after, before, count, search, order)

if response is None or "error" in response:
message = (
response.get("error")
if (response and "error" in response)
else "Unknown error"
)
raise ApiError(message)

return {
"statusCode": 200,
"body": json.dumps(response or {}),
"headers": {"Content-Type": "application/json"},
}


@ApiGateway()
def nc_creation_list(
event: ApiGatewayEvent, _context: LambdaContext, node_api: Optional[NodeApi] = None
) -> dict:
"""Get the list of nc creations."""
node_api = node_api or NodeApi()
after = event.query.get("after")
before = event.query.get("before")
count = event.query.get("count")
search = event.query.get("search")
order = event.query.get("order")

# This might throw HathorCoreTimeout error
response = node_api.get_nc_on_chain_blueprints(
after, before, count, find_blueprint_id, find_blueprint_name, order
response = node_api.get_nc_creation_list(
after,
before,
count,
search,
order,
)

if response is None or "error" in response:
Expand Down
30 changes: 26 additions & 4 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,7 @@ functions:
- name: request.querystring.after
- name: request.querystring.before
- name: request.querystring.count
- name: request.querystring.find_blueprint_id
- name: request.querystring.find_blueprint_name
- name: request.querystring.search
- name: request.header.origin

node_api_nc_on_chain_blueprints:
Expand All @@ -778,8 +777,31 @@ functions:
- name: request.querystring.after
- name: request.querystring.before
- name: request.querystring.count
- name: request.querystring.find_blueprint_id
- name: request.querystring.find_blueprint_name
- name: request.querystring.search
- name: request.querystring.order
- name: request.header.origin

node_api_nc_creation_list:
handler: handlers/node_api.nc_creation_list
maximumRetryAttempts: 0
package:
patterns:
- 'handlers/node_api.py'
layers:
- { Ref: PythonRequirementsLambdaLayer }
events:
- http:
path: node_api/nc_creation_list
method: get
cors: true
caching:
enabled: true
ttlInSeconds: 300
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: This time period is approximately 10 blocks at our current config. Does this mean a new nano-contract will have to wait up to this time to be displayed in the search results/api responses?

If so, this time seems a bit too high for this specific feature.

cacheKeyParameters:
- name: request.querystring.after
- name: request.querystring.before
- name: request.querystring.count
- name: request.querystring.search
- name: request.querystring.order
- name: request.header.origin

Expand Down
25 changes: 21 additions & 4 deletions tests/unit/gateways/test_node_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ def test_nc_builtin_blueprints(self, hathor_client):
"after": "1234",
"before": None,
"count": 10,
"find_blueprint_id": "5678",
"find_blueprint_name": None,
"search": "5678",
}
hathor_client.get = MagicMock(return_value=obj)
gateway = NodeApiGateway(hathor_core_client=hathor_client)
Expand All @@ -415,8 +414,7 @@ def test_nc_on_chain_blueprints(self, hathor_client):
"after": "1234",
"before": None,
"count": 10,
"find_blueprint_id": "5678",
"find_blueprint_name": None,
"search": "5678",
"order": "asc",
}
hathor_client.get = MagicMock(return_value=obj)
Expand All @@ -427,3 +425,22 @@ def test_nc_on_chain_blueprints(self, hathor_client):
)
assert result
assert sorted(result) == sorted(obj)

@patch("gateways.node_api_gateway.NC_CREATION_LIST_ENDPOINT", "mock-endpoint")
def test_nc_creation_list(self, hathor_client):
obj = {"foo": "bar"}
data = {
"after": "1234",
"before": None,
"count": 10,
"search": "5678",
"order": "asc",
}
hathor_client.get = MagicMock(return_value=obj)
gateway = NodeApiGateway(hathor_core_client=hathor_client)
result = gateway.get_nc_creation_list(**data)
hathor_client.get.assert_called_once_with(
"mock-endpoint", params=data, timeout=NODE_API_TIMEOUT_IN_SECONDS
)
assert result
assert sorted(result) == sorted(obj)
22 changes: 18 additions & 4 deletions tests/unit/usecases/test_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ def test_get_builtin_blueprints(self, node_api_gateway):
"after": "1234",
"before": None,
"count": 10,
"find_blueprint_id": "5678",
"find_blueprint_name": None,
"search": "5678",
}
result = node_api.get_nc_builtin_blueprints(**data)
node_api_gateway.get_nc_builtin_blueprints.assert_called_once_with(
Expand All @@ -409,8 +408,7 @@ def test_get_on_chain_blueprints(self, node_api_gateway):
"after": "1234",
"before": None,
"count": 10,
"find_blueprint_id": "5678",
"find_blueprint_name": None,
"search": "5678",
"order": "asc",
}
result = node_api.get_nc_on_chain_blueprints(**data)
Expand All @@ -419,3 +417,19 @@ def test_get_on_chain_blueprints(self, node_api_gateway):
)
assert result
assert sorted(result) == sorted(obj)

def test_get_nc_creation_list(self, node_api_gateway):
obj = {"foo": "bar"}
node_api_gateway.get_nc_creation_list = MagicMock(return_value=obj)
node_api = NodeApi(node_api_gateway)
data = {
"after": "1234",
"before": None,
"count": 10,
"search": "5678",
"order": "asc",
}
result = node_api.get_nc_creation_list(**data)
node_api_gateway.get_nc_creation_list.assert_called_once_with(*data.values())
assert result
assert sorted(result) == sorted(obj)
26 changes: 20 additions & 6 deletions usecases/node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,36 @@ def get_nc_builtin_blueprints(
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
find_blueprint_id: Optional[str] = None,
find_blueprint_name: Optional[str] = None,
search: Optional[str] = None,
) -> Optional[dict]:
return self.node_api_gateway.get_nc_builtin_blueprints(
after, before, count, find_blueprint_id, find_blueprint_name
after, before, count, search
)

def get_nc_on_chain_blueprints(
self,
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
find_blueprint_id: Optional[str] = None,
find_blueprint_name: Optional[str] = None,
search: Optional[str] = None,
order: Optional[str] = None,
) -> Optional[dict]:
return self.node_api_gateway.get_nc_on_chain_blueprints(
after, before, count, find_blueprint_id, find_blueprint_name, order
after, before, count, search, order
)

def get_nc_creation_list(
self,
after: Optional[str] = None,
before: Optional[str] = None,
count: Optional[int] = None,
search: Optional[str] = None,
order: Optional[str] = None,
) -> Optional[dict]:
return self.node_api_gateway.get_nc_creation_list(
after,
before,
count,
search,
order,
)
Loading