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

Improve API versioning #36

Merged
merged 6 commits into from
Mar 15, 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
2 changes: 1 addition & 1 deletion .github/workflows/format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black flake8 flake8-bugbear flake8-implicit-str-concat isort
pip install -r requirements/formatting.txt
- name: Run code checks
run: |
make lint-check
4 changes: 3 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ jobs:
- uses: actions/checkout@v3
- name: Install dependencies
run: |
sudo apt-get install libmemcached-dev
python -m pip install --upgrade pip
pip install cbor flask==2.3.2 flask_smorest==0.42.0 marshmallow Pillow psycopg[pool] pylibmc pytest toml
pip install -r requirements/api.txt
pip install -r requirements/tests.txt
- name: Run pytest
run: |
python -m pytest
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
files: api/|blockchain/|mockups/|schemas/|scripts/|tests/
repos:
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
args: ["-c"]
- repo: https://github.com/psf/black
rev: 23.7.0
rev: 24.2.0
hooks:
- id: black
args: ["--check"]
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8
args: ["--extend-ignore=E203,E501"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ sudo make install

## Python3 dependencies - virtual environment

It is advised to start the explorer from within a virtual environment. You can install all dependencies using the `requirements.txt` file:
It is advised to start the explorer from within a virtual environment. You can install all dependencies using the `requirements/all.txt` file:
```
virtualenv env
source env/bin/activate
pip install -r requirements.txt
pip install -r requirements/all.txt
```

## Install and configure memcached
Expand Down
8 changes: 4 additions & 4 deletions api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ def create_app(mock=False):

# Configure app
app.config["API_TITLE"] = "Witnet explorer REST API"
app.config["API_VERSION"] = "v1.0.0"
app.config["API_VERSION"] = "1.0.0"

# Set configurations for OpenAPI documentation
app.config["OPENAPI_VERSION"] = "3.0.3"
app.config["OPENAPI_URL_PREFIX"] = "/api"
app.config["OPENAPI_RAPIDOC_PATH"] = "/documentation"
app.config[
"OPENAPI_RAPIDOC_URL"
] = "https://cdn.jsdelivr.net/npm/rapidoc/dist/rapidoc-min.js"
app.config["OPENAPI_RAPIDOC_URL"] = (
"https://cdn.jsdelivr.net/npm/rapidoc/dist/rapidoc-min.js"
)
app.config["OPENAPI_RAPIDOC_CONFIG"] = {
"allow-authentication": "false",
"allow-spec-file-download": "true",
Expand Down
4 changes: 2 additions & 2 deletions api/blueprints/address/blocks_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get(self, args, pagination_parameters):
if cached_blocks:
logger.info(f"Found {len(cached_blocks)} blocks for {arg_address} in cache")
pagination_parameters.item_count = len(cached_blocks)
return cached_blocks[start:stop], 200, {"X-Version": "v1.0.0"}
return cached_blocks[start:stop], 200, {"X-Version": "1.0.0"}
# Query the database and build the requested view (slow)
else:
logger.info(
Expand All @@ -90,4 +90,4 @@ def get(self, args, pagination_parameters):
message=f"Incorrect message format for block data for {arg_address}.",
)
pagination_parameters.item_count = len(blocks)
return blocks[start:stop], 200, {"X-Version": "v1.0.0"}
return blocks[start:stop], 200, {"X-Version": "1.0.0"}
4 changes: 2 additions & 2 deletions api/blueprints/address/data_requests_created_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get(self, args, pagination_parameters):
return (
cached_data_requests_created[start:stop],
200,
{"X-Version": "v1.0.0"},
{"X-Version": "1.0.0"},
)
# Query the database and build the requested view (slow)
else:
Expand Down Expand Up @@ -98,4 +98,4 @@ def get(self, args, pagination_parameters):
message=f"Incorrect message format for data requests created data for {arg_address}.",
)
pagination_parameters.item_count = len(data_requests_created)
return data_requests_created[start:stop], 200, {"X-Version": "v1.0.0"}
return data_requests_created[start:stop], 200, {"X-Version": "1.0.0"}
4 changes: 2 additions & 2 deletions api/blueprints/address/data_requests_solved_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get(self, args, pagination_parameters):
f"Found {len(cached_data_requests_solved)} data requests solved for {arg_address} in cache"
)
pagination_parameters.item_count = len(cached_data_requests_solved)
return cached_data_requests_solved[start:stop], 200, {"X-Version": "v1.0.0"}
return cached_data_requests_solved[start:stop], 200, {"X-Version": "1.0.0"}
# Query the database and build the requested view (slow)
else:
logger.info(
Expand All @@ -92,4 +92,4 @@ def get(self, args, pagination_parameters):
message=f"Incorrect message format for data requests solved data for {arg_address}.",
)
pagination_parameters.item_count = len(data_requests_solved)
return data_requests_solved[start:stop], 200, {"X-Version": "v1.0.0"}
return data_requests_solved[start:stop], 200, {"X-Version": "1.0.0"}
2 changes: 1 addition & 1 deletion api/blueprints/address/details_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ def get(self, args):
404,
message=f"Incorrect message format for details data for {arg_address}.",
)
return details, 200, {"X-Version": "v1.0.0"}
return details, 200, {"X-Version": "1.0.0"}
4 changes: 2 additions & 2 deletions api/blueprints/address/info_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def get(self, args):
]
),
200,
{"X-Version": "v1.0.0"},
{"X-Version": "1.0.0"},
)
except ValidationError as err_info:
logger.error(
Expand All @@ -98,4 +98,4 @@ def get(self, args):
message=f"Incorrect message format for address info for {addresses}.",
)
else:
return [], 200, {"X-Version": "v1.0.0"}
return [], 200, {"X-Version": "1.0.0"}
4 changes: 2 additions & 2 deletions api/blueprints/address/labels_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get(self):
]
),
200,
{"X-Version": "v1.0.0"},
{"X-Version": "1.0.0"},
)
except ValidationError as err_info:
logger.error(
Expand All @@ -79,4 +79,4 @@ def get(self):
message=f"Incorrect message format for address labels for {addresses}.",
)
else:
return [], 200, {"X-Version": "v1.0.0"}
return [], 200, {"X-Version": "1.0.0"}
4 changes: 2 additions & 2 deletions api/blueprints/address/mints_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get(self, args, pagination_parameters):
f"Found {len(cached_mints)} mint transactions for {arg_address} in cache"
)
pagination_parameters.item_count = len(cached_mints)
return cached_mints[start:stop], 200, {"X-Version": "v1.0.0"}
return cached_mints[start:stop], 200, {"X-Version": "1.0.0"}
# Query the database and build the requested view (slow)
else:
logger.info(
Expand All @@ -92,4 +92,4 @@ def get(self, args, pagination_parameters):
message=f"Incorrect message format for mint data for {arg_address}.",
)
pagination_parameters.item_count = len(mints)
return mints[start:stop], 200, {"X-Version": "v1.0.0"}
return mints[start:stop], 200, {"X-Version": "1.0.0"}
2 changes: 1 addition & 1 deletion api/blueprints/address/utxos_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ def get(self, args):
)
abort(404, message=f"Incorrect message format for UTXO data for {address}.")

return address_utxos, 200, {"X-Version": "v1.0.0"}
return address_utxos, 200, {"X-Version": "1.0.0"}
4 changes: 2 additions & 2 deletions api/blueprints/address/value_transfers_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get(self, args, pagination_parameters):
f"Found {len(cached_value_transfers)} value transfers for {arg_address} in cache"
)
pagination_parameters.item_count = len(cached_value_transfers)
return cached_value_transfers[start:stop], 200, {"X-Version": "v1.0.0"}
return cached_value_transfers[start:stop], 200, {"X-Version": "1.0.0"}
# Query the database and build the requested view (slow)
else:
logger.info(
Expand All @@ -92,4 +92,4 @@ def get(self, args, pagination_parameters):
message=f"Incorrect message format for value transfer data for {arg_address}.",
)
pagination_parameters.item_count = len(value_transfers)
return value_transfers[start:stop], 200, {"X-Version": "v1.0.0"}
return value_transfers[start:stop], 200, {"X-Version": "1.0.0"}
6 changes: 3 additions & 3 deletions api/blueprints/misc/home_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def get(self, args):
logger.info("Found home in memcached cache")

if key == "full":
return home, 200, {"X-Version": "v1.0.0"}
return home, 200, {"X-Version": "1.0.0"}
elif key in ("network_stats", "supply_info"):
return {key: home[key]}, 200, {"X-Version": "v1.0.0"}
return {key: home[key]}, 200, {"X-Version": "1.0.0"}
elif key in ("blocks", "data_requests", "value_transfers"):
return (
{f"latest_{key}": home[f"latest_{key}"]},
200,
{"X-Version": "v1.0.0"},
{"X-Version": "1.0.0"},
)
2 changes: 1 addition & 1 deletion api/blueprints/misc/ping_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get(self):
return (
PingResponse().load({"response": "pong"}),
200,
{"X-Version": "v1.0.0"},
{"X-Version": "1.0.0"},
)
except ValidationError as err_info:
logger.error(f"Incorrect message format for ping response: {err_info}")
Expand Down
2 changes: 1 addition & 1 deletion api/blueprints/misc/status_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ def get(self):
else:
logger.info("Found status in memcached cache")

return status, 200, {"X-Version": "v1.0.0"}
return status, 200, {"X-Version": "1.0.0"}
2 changes: 1 addition & 1 deletion api/blueprints/network/balances_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ def get(self, pagination_parameters):
}
),
200,
{"X-Version": "v1.0.0"},
{"X-Version": "1.0.0"},
)
4 changes: 2 additions & 2 deletions api/blueprints/network/blockchain_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get(self, pagination_parameters):
if blockchain:
logger.info(f"Found {cache_key} in memcached cache")
pagination_parameters.item_count = blockchain["total_epochs"]
return blockchain, 200, {"X-Version": "v1.0.0"}
return blockchain, 200, {"X-Version": "1.0.0"}

logger.info(f"Could not find {cache_key} in memcached cache")

Expand Down Expand Up @@ -113,7 +113,7 @@ def get(self, pagination_parameters):
)
abort(404, message="Incorrect message format for blockchain response.")

return blockchain, 200, {"X-Version": "v1.0.0"}
return blockchain, 200, {"X-Version": "1.0.0"}


def get_blockchain_details(database, last_epoch, start, stop, consensus_constants):
Expand Down
2 changes: 1 addition & 1 deletion api/blueprints/network/mempool_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def get(self, args):
else:
logger.info(f"Found {key} in memcached cache")

return mempool, 200, {"X-Version": "v1.0.0"}
return mempool, 200, {"X-Version": "1.0.0"}


def get_historical_mempool(
Expand Down
2 changes: 1 addition & 1 deletion api/blueprints/network/reputation_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ def get(self):
else:
logger.info("Found reputation in our memcached instance.")

return reputation, 200, {"X-Version": "v1.0.0"}
return reputation, 200, {"X-Version": "1.0.0"}
4 changes: 2 additions & 2 deletions api/blueprints/network/statistics_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def get(self, args):
response = cache.get(cache_key)
if response:
logger.info(f"Found response for {cache_key} in cache")
return response, 200, {"X-Version": "v1.0.0"}
return response, 200, {"X-Version": "1.0.0"}

# Rollbacks are saved as a list in the database, so even if epochs are specified, retrieve the complete list
if args["key"] == "list-rollbacks":
Expand Down Expand Up @@ -337,7 +337,7 @@ def get(self, args):
f"Could not save {cache_key} in the memcached instance because its size exceeded 1MB"
)

return response, 200, {"X-Version": "v1.0.0"}
return response, 200, {"X-Version": "1.0.0"}


def calculate_network_start_stop_epoch(
Expand Down
4 changes: 2 additions & 2 deletions api/blueprints/network/supply_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def get(self, args):
"epoch",
"in_flight_requests",
):
return str(int(home["supply_info"][key])), 200, {"X-Version": "v1.0.0"}
return str(int(home["supply_info"][key])), 200, {"X-Version": "1.0.0"}
else:
return (
str(int(home["supply_info"][key] / 1e9)),
200,
{"X-Version": "v1.0.0"},
{"X-Version": "1.0.0"},
)
2 changes: 1 addition & 1 deletion api/blueprints/network/tapi_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ def get(self, args):
f"Returning TAPI's {', '.join(str(tapi['tapi_id']) for tapi in all_tapis)}"
)

return all_tapis, 200, {"X-Version": "v1.0.0"}
return all_tapis, 200, {"X-Version": "1.0.0"}
4 changes: 2 additions & 2 deletions api/blueprints/search/epoch_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get(self, args):
logger.info(
f"Found block {epoch} with hash {cached_block_hash} in memcached cache"
)
return cached_block, 200, {"X-Version": "v1.0.0"}
return cached_block, 200, {"X-Version": "1.0.0"}

# Create consensus constants
consensus_constants = ConsensusConstants(
Expand Down Expand Up @@ -147,7 +147,7 @@ def get(self, args):
}
),
200,
{"X-Version": "v1.0.0"},
{"X-Version": "1.0.0"},
)
except ValidationError as err_info:
logger.error(f"Incorrect message format for block {epoch}: {err_info}")
Expand Down
Loading
Loading