Skip to content

Commit

Permalink
Feature/SK-1109 | Make abstract class Store (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman authored Jan 7, 2025
1 parent 8df89db commit 40764cb
Show file tree
Hide file tree
Showing 20 changed files with 205 additions and 452 deletions.
23 changes: 6 additions & 17 deletions fedn/network/api/v1/client_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
bp = Blueprint("client", __name__, url_prefix=f"/api/{api_version}/clients")



@bp.route("/", methods=["GET"])
@jwt_auth_required(role="admin")
def get_clients():
Expand Down Expand Up @@ -109,14 +108,10 @@ def get_clients():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)
kwargs = request.args.to_dict()

clients = client_store.list(limit, skip, sort_key, sort_order, use_typing=False, **kwargs)

result = clients["result"]

response = {"count": clients["count"], "result": result}
response = client_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -195,13 +190,9 @@ def list_clients():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)
kwargs = get_post_data_to_kwargs(request)
clients = client_store.list(limit, skip, sort_key, sort_order, use_typing=False, **kwargs)

result = clients["result"]

response = {"count": clients["count"], "result": result}
response = client_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -357,17 +348,15 @@ def get_client(id: str):
type: string
"""
try:
client = client_store.get(id, use_typing=False)

response = client
response = client_store.get(id)

return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500

# delete client

@bp.route("/<string:id>", methods=["DELETE"])
@jwt_auth_required(role="admin")
def delete_client(id: str):
Expand Down
24 changes: 7 additions & 17 deletions fedn/network/api/v1/combiner_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,11 @@ def get_combiners():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)

kwargs = request.args.to_dict()

combiners = combiner_store.list(limit, skip, sort_key, sort_order, use_typing=False, **kwargs)

result = combiners["result"]

response = {"count": combiners["count"], "result": result}
response = combiner_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -185,15 +181,11 @@ def list_combiners():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)

kwargs = get_post_data_to_kwargs(request)

combiners = combiner_store.list(limit, skip, sort_key, sort_order, use_typing=False, **kwargs)

result = combiners["result"]

response = {"count": combiners["count"], "result": result}
response = combiner_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -331,15 +323,15 @@ def get_combiner(id: str):
type: string
"""
try:
combiner = combiner_store.get(id, use_typing=False)
response = combiner
response = combiner_store.get(id)

return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500


@bp.route("/<string:id>", methods=["DELETE"])
@jwt_auth_required(role="admin")
def delete_combiner(id: str):
Expand Down Expand Up @@ -421,9 +413,7 @@ def number_of_clients_connected():
combiners = combiners.split(",") if combiners else []
response = client_store.connected_client_count(combiners)

result = {
"result": response
}
result = {"result": response}

return jsonify(result), 200
except Exception:
Expand Down
30 changes: 11 additions & 19 deletions fedn/network/api/v1/model_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,10 @@ def get_models():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)
kwargs = request.args.to_dict()

models = model_store.list(limit, skip, sort_key, sort_order, use_typing=False, **kwargs)

result = models["result"]

response = {"count": models["count"], "result": result}
response = model_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -186,14 +182,10 @@ def list_models():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)
kwargs = get_post_data_to_kwargs(request)

models = model_store.list(limit, skip, sort_key, sort_order, use_typing=False, **kwargs)

result = models["result"]

response = {"count": models["count"], "result": result}
response = model_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -335,7 +327,7 @@ def get_model(id: str):
type: string
"""
try:
model = model_store.get(id, use_typing=False)
model = model_store.get(id)

response = model

Expand Down Expand Up @@ -386,7 +378,7 @@ def patch_model(id: str):
type: string
"""
try:
model = model_store.get(id, use_typing=False)
model = model_store.get(id)

data = request.get_json()
_id = model["id"]
Expand Down Expand Up @@ -451,7 +443,7 @@ def put_model(id: str):
type: string
"""
try:
model = model_store.get(id, use_typing=False)
model = model_store.get(id)
data = request.get_json()
_id = model["id"]

Expand Down Expand Up @@ -511,7 +503,7 @@ def get_descendants(id: str):
try:
limit = get_limit(request.headers)

descendants = model_store.list_descendants(id, limit or 10, use_typing=False)
descendants = model_store.list_descendants(id, limit or 10)

response = descendants

Expand Down Expand Up @@ -580,7 +572,7 @@ def get_ancestors(id: str):

include_self: bool = include_self_param and include_self_param.lower() == "true"

ancestors = model_store.list_ancestors(id, limit or 10, include_self=include_self, reverse=reverse, use_typing=False)
ancestors = model_store.list_ancestors(id, limit or 10, include_self=include_self, reverse=reverse)

response = ancestors

Expand Down Expand Up @@ -626,7 +618,7 @@ def download(id: str):
"""
try:
if minio_repository is not None:
model = model_store.get(id, use_typing=False)
model = model_store.get(id)
model_id = model["model"]

file = minio_repository.get_artifact_stream(model_id, modelstorage_config["storage_config"]["storage_bucket"])
Expand Down Expand Up @@ -680,7 +672,7 @@ def get_parameters(id: str):
"""
try:
if minio_repository is not None:
model = model_store.get(id, use_typing=False)
model = model_store.get(id)
model_id = model["model"]

file = minio_repository.get_artifact_stream(model_id, modelstorage_config["storage_config"]["storage_bucket"])
Expand Down
30 changes: 7 additions & 23 deletions fedn/network/api/v1/package_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@

from fedn.common.config import FEDN_COMPUTE_PACKAGE_DIR
from fedn.network.api.auth import jwt_auth_required
from fedn.network.api.v1.shared import (api_version, get_post_data_to_kwargs,
get_typed_list_headers, get_use_typing,
package_store, repository)
from fedn.network.api.v1.shared import api_version, get_post_data_to_kwargs, get_typed_list_headers, package_store, repository
from fedn.network.storage.statestore.stores.shared import EntityNotFound

bp = Blueprint("package", __name__, url_prefix=f"/api/{api_version}/packages")



@bp.route("/", methods=["GET"])
@jwt_auth_required(role="admin")
def get_packages():
Expand Down Expand Up @@ -119,14 +116,10 @@ def get_packages():
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)
kwargs = request.args.to_dict()

packages = package_store.list(limit, skip, sort_key, sort_order, use_typing=True, **kwargs)

result = [package.__dict__ for package in packages["result"]]

response = {"count": packages["count"], "result": result}
response = package_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -207,14 +200,10 @@ def list_packages():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)
kwargs = get_post_data_to_kwargs(request)

packages = package_store.list(limit, skip, sort_key, sort_order, use_typing=True, **kwargs)

result = [package.__dict__ for package in packages["result"]]

response = {"count": packages["count"], "result": result}
response = package_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -379,10 +368,7 @@ def get_package(id: str):
type: string
"""
try:
use_typing: bool = get_use_typing(request.headers)
package = package_store.get(id, use_typing=use_typing)

response = package.__dict__ if use_typing else package
response = package_store.get(id)

return jsonify(response), 200
except EntityNotFound:
Expand Down Expand Up @@ -420,9 +406,7 @@ def get_active_package():
type: string
"""
try:
use_typing: bool = get_use_typing(request.headers)
package = package_store.get_active(use_typing=use_typing)
response = package.__dict__ if use_typing else package
response = package_store.get_active()

return jsonify(response), 200
except EntityNotFound:
Expand Down
18 changes: 5 additions & 13 deletions fedn/network/api/v1/prediction_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from fedn.network.api.auth import jwt_auth_required
from fedn.network.api.shared import control
from fedn.network.api.v1.shared import api_version, mdb, get_typed_list_headers, get_post_data_to_kwargs
from fedn.network.api.v1.shared import api_version, get_post_data_to_kwargs, get_typed_list_headers, mdb
from fedn.network.storage.statestore.stores.model_store import ModelStore
from fedn.network.storage.statestore.stores.prediction_store import PredictionStore
from fedn.network.storage.statestore.stores.shared import EntityNotFound
Expand Down Expand Up @@ -170,14 +170,10 @@ def get_predictions():
type: string
"""
try:
limit, skip, sort_key, sort_order, use_typing = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)
kwargs = request.args.to_dict()

predictions = prediction_store.list(limit, skip, sort_key, sort_order, use_typing=use_typing, **kwargs)

result = [prediction.__dict__ for prediction in predictions["result"]] if use_typing else predictions["result"]

response = {"count": predictions["count"], "result": result}
response = prediction_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -268,14 +264,10 @@ def list_predictions():
type: string
"""
try:
limit, skip, sort_key, sort_order, use_typing = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)
kwargs = get_post_data_to_kwargs(request)

predictions = prediction_store.list(limit, skip, sort_key, sort_order, use_typing=use_typing, **kwargs)

result = [prediction.__dict__ for prediction in predictions["result"]] if use_typing else predictions["result"]

response = {"count": predictions["count"], "result": result}
response = prediction_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down
18 changes: 5 additions & 13 deletions fedn/network/api/v1/round_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,11 @@ def get_rounds():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)

kwargs = request.args.to_dict()

rounds = round_store.list(limit, skip, sort_key, sort_order, use_typing=False, **kwargs)

result = rounds["result"]

response = {"count": rounds["count"], "result": result}
response = round_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -169,15 +165,11 @@ def list_rounds():
type: string
"""
try:
limit, skip, sort_key, sort_order, _ = get_typed_list_headers(request.headers)
limit, skip, sort_key, sort_order = get_typed_list_headers(request.headers)

kwargs = get_post_data_to_kwargs(request)

rounds = round_store.list(limit, skip, sort_key, sort_order, use_typing=False, **kwargs)

result = rounds["result"]

response = {"count": rounds["count"], "result": result}
response = round_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
Expand Down Expand Up @@ -305,7 +297,7 @@ def get_round(id: str):
type: string
"""
try:
round = round_store.get(id, use_typing=False)
round = round_store.get(id)
response = round

return jsonify(response), 200
Expand Down
Loading

0 comments on commit 40764cb

Please sign in to comment.