Skip to content

Commit

Permalink
feat: add new hathor core api to get blueprint source code
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroferreira1 committed Aug 20, 2024
1 parent 4af2317 commit 71346f6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions gateways/clients/hathor_core_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
NC_STATE_ENDPOINT = "/v1a/nano_contract/state"
NC_HISTORY_ENDPOINT = "/v1a/nano_contract/history"
NC_BLUEPRINT_INFORMATION_ENDPOINT = "/v1a/nano_contract/blueprint"
NC_BLUEPRINT_SOURCE_CODE_ENDPOINT = "/v1a/nano_contract/blueprint_source_code"
STATUS_ENDPOINT = "/v1a/status"
TOKEN_ENDPOINT = "/v1a/thin_wallet/token"
TOKEN_HISTORY_ENDPOINT = "/v1a/thin_wallet/token_history"
Expand Down
9 changes: 9 additions & 0 deletions gateways/node_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
FEATURE_ENDPOINT,
GRAPHVIZ_DOT_NEIGHBORS_ENDPOINT,
NC_BLUEPRINT_INFORMATION_ENDPOINT,
NC_BLUEPRINT_SOURCE_CODE_ENDPOINT,
NC_HISTORY_ENDPOINT,
NC_STATE_ENDPOINT,
PUSH_TX_ENDPOINT,
Expand Down Expand Up @@ -254,3 +255,11 @@ def get_nc_blueprint_information(self, blueprint_id: str) -> Optional[dict]:
params={"blueprint_id": blueprint_id},
timeout=NODE_API_TIMEOUT_IN_SECONDS,
)

def get_nc_blueprint_source_code(self, blueprint_id: str) -> Optional[dict]:
"""Get blueprint source code."""
return self.hathor_core_client.get(
NC_BLUEPRINT_SOURCE_CODE_ENDPOINT,
params={"blueprint_id": blueprint_id},
timeout=NODE_API_TIMEOUT_IN_SECONDS,
)
25 changes: 25 additions & 0 deletions handlers/node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,28 @@ def nc_blueprint_information(
"body": json.dumps(response or {}),
"headers": {"Content-Type": "application/json"},
}


@ApiGateway()
def nc_blueprint_source_code(
event: ApiGatewayEvent, _context: LambdaContext, node_api: Optional[NodeApi] = None
) -> dict:
"""Get blueprint source code."""
node_api = node_api or NodeApi()
blueprint_id = event.query.get("blueprint_id")

if blueprint_id is None:
raise ApiError("invalid_parameters")

# This might throw HathorCoreTimeout error
response = node_api.get_nc_blueprint_source_code(blueprint_id)

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

return {
"statusCode": 200,
"body": json.dumps(response or {}),
"headers": {"Content-Type": "application/json"},
}
20 changes: 20 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,26 @@ functions:
- name: request.querystring.blueprint_id
- name: request.header.origin

node_api_nc_blueprint_code:
handler: handlers/node_api.nc_blueprint_source_code
maximumRetryAttempts: 0
package:
patterns:
- 'handlers/node_api.py'
layers:
- { Ref: PythonRequirementsLambdaLayer }
events:
- http:
path: node_api/nc_blueprint_source_code
method: get
cors: true
caching:
enabled: true
ttlInSeconds: 300
cacheKeyParameters:
- name: request.querystring.blueprint_id
- name: request.header.origin

walletServiceAddressBalance:
handler: handlers/wallet_service.handle_address_balance
timeout: 15
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/gateways/test_node_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,20 @@ def test_nc_blueprint_information(self, hathor_client):
)
assert result
assert sorted(result) == sorted(obj)

@patch(
"gateways.node_api_gateway.NC_BLUEPRINT_SOURCE_CODE_ENDPOINT", "mock-endpoint"
)
def test_nc_blueprint_source_code(self, hathor_client):
obj = {"foo": "bar"}
data = {
"blueprint_id": "1234",
}
hathor_client.get = MagicMock(return_value=obj)
gateway = NodeApiGateway(hathor_core_client=hathor_client)
result = gateway.get_nc_blueprint_source_code(**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)
9 changes: 9 additions & 0 deletions tests/unit/usecases/test_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,12 @@ def test_get_blueprint_information(self, node_api_gateway):
node_api_gateway.get_nc_blueprint_information.assert_called_once_with("1234")
assert result
assert sorted(result) == sorted(obj)

def test_get_blueprint_source_code(self, node_api_gateway):
obj = {"foo": "bar"}
node_api_gateway.get_nc_blueprint_source_code = MagicMock(return_value=obj)
node_api = NodeApi(node_api_gateway)
result = node_api.get_nc_blueprint_source_code("1234")
node_api_gateway.get_nc_blueprint_source_code.assert_called_once_with("1234")
assert result
assert sorted(result) == sorted(obj)
3 changes: 3 additions & 0 deletions usecases/node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,6 @@ def get_nc_history(

def get_nc_blueprint_information(self, blueprint_id: str) -> Optional[dict]:
return self.node_api_gateway.get_nc_blueprint_information(blueprint_id)

def get_nc_blueprint_source_code(self, blueprint_id: str) -> Optional[dict]:
return self.node_api_gateway.get_nc_blueprint_source_code(blueprint_id)

0 comments on commit 71346f6

Please sign in to comment.