From 782319591151ebf3cdd44e4323177d01ef9632bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Tue, 26 Apr 2022 11:21:04 +0200 Subject: [PATCH] added api to get the latest block /api/v1/blocks/latest (#88) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added api to get the latest block /api/v1/blocks/latest Signed-off-by: Jürgen Eckel * git fixed flake8 whitespace warnings Signed-off-by: Jürgen Eckel * fixed flake8 warnings: removed too many newline Signed-off-by: Jürgen Eckel * added missing deps in the testing docker files Signed-off-by: Jürgen Eckel * install meson prior to the rest to have it availabe and fix the issue of missing depenceny declarations within zenroom Signed-off-by: Jürgen Eckel * added missing ninja dependency Signed-off-by: Jürgen Eckel * fixed zenroom dependency Signed-off-by: Jürgen Eckel --- acceptance/python/Dockerfile | 15 +++++++++------ integration/python/Dockerfile | 13 +++++++++---- planetmint/web/routes.py | 1 + planetmint/web/views/blocks.py | 19 +++++++++++++++++++ tests/web/test_blocks.py | 6 ++++++ 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/acceptance/python/Dockerfile b/acceptance/python/Dockerfile index 14defda..269446b 100644 --- a/acceptance/python/Dockerfile +++ b/acceptance/python/Dockerfile @@ -4,17 +4,20 @@ RUN apt-get update \ && pip install -U pip \ && apt-get autoremove \ && apt-get clean -RUN apt-get install -y vim zsh build-essential cmake +RUN apt-get install -y vim zsh build-essential cmake RUN mkdir -p /src +RUN /usr/local/bin/python -m pip install --upgrade pip +RUN pip install --upgrade meson ninja +RUN pip install zenroom==2.0.0.dev1644927841 RUN pip install --upgrade \ pycco \ websocket-client~=0.47.0 \ pytest~=3.0 \ - git+https://github.com/planetmint/cryptoconditions.git@gitzenroom \ - git+https://github.com/planetmint/planetmint-driver.git@gitzenroom \ - #planetmint-cryptoconditions>=0.9.0\ - #planetmint-driver>=0.9.0 \ + #git+https://github.com/planetmint/cryptoconditions.git@gitzenroom \ + #git+https://github.com/planetmint/planetmint-driver.git@gitzenroom \ + planetmint-cryptoconditions>=0.9.4\ + planetmint-driver>=0.9.0 \ blns @@ -58,4 +61,4 @@ RUN pip install --upgrade \ # pyyaml==5.4.1 \ # requests==2.25.1 \ # setproctitle==1.2.2 -# \ No newline at end of file +# diff --git a/integration/python/Dockerfile b/integration/python/Dockerfile index 65f5e15..2498a58 100644 --- a/integration/python/Dockerfile +++ b/integration/python/Dockerfile @@ -4,14 +4,19 @@ RUN apt-get update \ && pip install -U pip \ && apt-get autoremove \ && apt-get clean -RUN apt-get install -y vim zsh build-essential cmake openssh-client openssh-server +RUN apt-get install -y vim +RUN apt-get update +RUN apt-get install -y build-essential cmake openssh-client openssh-server +RUN apt-get install -y zsh RUN mkdir -p /src +RUN pip install --upgrade meson ninja RUN pip install --upgrade \ pytest~=6.2.5 \ planetmint-driver~=0.9.0 \ pycco \ websocket-client~=0.47.0 \ - git+https://github.com/planetmint/cryptoconditions.git@gitzenroom \ - git+https://github.com/planetmint/planetmint-driver.git@gitzenroom \ - blns \ No newline at end of file + #git+https://github.com/planetmint/cryptoconditions.git@gitzenroom \ + #git+https://github.com/planetmint/planetmint-driver.git@gitzenroom \ + blns + diff --git a/planetmint/web/routes.py b/planetmint/web/routes.py index 7bd51f0..2c650c0 100644 --- a/planetmint/web/routes.py +++ b/planetmint/web/routes.py @@ -34,6 +34,7 @@ ROUTES_API_V1 = [ r('assets/', assets.AssetListApi), r('metadata/', metadata.MetadataApi), r('blocks/', blocks.BlockApi), + r('blocks/latest', blocks.LatestBlock), r('blocks/', blocks.BlockListApi), r('transactions/', tx.TransactionApi), r('transactions', tx.TransactionListApi), diff --git a/planetmint/web/views/blocks.py b/planetmint/web/views/blocks.py index 5a8ae41..5154ba3 100644 --- a/planetmint/web/views/blocks.py +++ b/planetmint/web/views/blocks.py @@ -13,6 +13,25 @@ from flask_restful import Resource, reqparse from planetmint.web.views.base import make_error +class LatestBlock(Resource): + def get(self): + """API endpoint to get details about a block. + + Return: + A JSON string containing the data about the block. + """ + + pool = current_app.config['bigchain_pool'] + + with pool() as planet: + block = planet.get_latest_block() + + if not block: + return make_error(404) + + return block + + class BlockApi(Resource): def get(self, block_id): """API endpoint to get details about a block. diff --git a/tests/web/test_blocks.py b/tests/web/test_blocks.py index 54aa134..7dfc00e 100644 --- a/tests/web/test_blocks.py +++ b/tests/web/test_blocks.py @@ -17,6 +17,12 @@ def test_get_block_returns_404_if_not_found(client): res = client.get(BLOCKS_ENDPOINT + '123/') assert res.status_code == 404 + res = client.get(BLOCKS_ENDPOINT + 'latest') + assert res.status_code == 200 + + res = client.get(BLOCKS_ENDPOINT + 'latest/') + assert res.status_code == 200 + @pytest.mark.bdb def test_get_blocks_by_txid_endpoint_returns_empty_list_not_found(client):