Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

use http status codes #138

Merged
merged 1 commit into from
Sep 4, 2023
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
3 changes: 2 additions & 1 deletion app/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import subprocess
import xml.etree.ElementTree as ET
from http import HTTPStatus

import docker
import requests
Expand Down Expand Up @@ -210,7 +211,7 @@ def check_dns_exists():
url = config.dns_exists_url()
try:
response = requests.get(url)
if response.status_code == 200 and response.content:
if response.status_code == HTTPStatus.OK and response.content:
json_response = response.json()
if "domain" in json_response:
cached_domain = json_response["domain"]
Expand Down
74 changes: 40 additions & 34 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
from enum import Enum
from http import HTTPStatus

import docker
from fastapi import APIRouter, HTTPException, Request
Expand Down Expand Up @@ -58,7 +59,7 @@ async def registries_all():
"/registries/",
response_model=schemas.RegistryResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
Copy link
Contributor Author

@casperdcl casperdcl Sep 4, 2023

Choose a reason for hiding this comment

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

fyi since statuses are IntEnum, this change should be fine (no need to use HTTPStatus.BAD_REQUEST.value i.e. int explicitly)

>>> BAD_REQUEST == 400 == BAD_REQUEST.value
True

"model": schemas.ErrorResponse,
"description": "Failed to add registry.",
}
Expand All @@ -71,13 +72,13 @@ async def add_registry(body: schemas.RegistryInput):
return response
else:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Registry {body.url} already exists!"},
)
except Exception as error:
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to add registry {error}"},
) from error

Expand All @@ -86,7 +87,7 @@ async def add_registry(body: schemas.RegistryInput):
"/registries/",
response_model=schemas.SuccessResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Registry not found.",
}
Expand All @@ -97,15 +98,15 @@ async def remove_registry(url: str):
response = services.delete_registry(url)
if response is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={
"message": f"Cannot to remove registry {url} because it does not exists!"
},
)
except Exception as error:
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to remove registry {url}"},
) from error
return {"message": f"Registry {url} removed successfully."}
Expand All @@ -120,7 +121,7 @@ async def services_all():
"/services/",
response_model=schemas.ServiceResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Failed to stop container or service not found.",
}
Expand All @@ -134,13 +135,13 @@ async def add_service(body: schemas.ServiceInput):
return response
else:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Service {service['id']} already exists!"},
)
except Exception as error:
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to add service {error}"},
) from error

Expand All @@ -149,13 +150,18 @@ async def add_service(body: schemas.ServiceInput):
"/services/{service_id}",
response_model=schemas.ServiceResponse,
responses={
400: {"model": schemas.ErrorResponse, "description": "Service not found."}
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Service not found.",
}
},
)
async def service_by_id(service_id: str):
service_object = services.get_service_by_id(service_id)
if service_object is None:
raise HTTPException(status_code=400, detail={"message": "Service not found."})
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail={"message": "Service not found."}
)
return service_object


Expand All @@ -171,7 +177,7 @@ async def services_by_interface(interface_id: str):
"/download-service/{service_id}",
response_model=schemas.SuccessResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Failed to download image or service not found.",
}
Expand All @@ -181,7 +187,7 @@ async def download_service(service_id: str):
service_object = services.get_service_by_id(service_id)
if service_object is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": "Service not found."},
)

Expand All @@ -191,7 +197,7 @@ async def download_service(service_id: str):
except Exception as error:
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to download image {error}"},
) from error
return {"message": "Download image successfully."}
Expand All @@ -202,7 +208,7 @@ async def download_service_stream(service_id: str):
service_object = services.get_service_by_id(service_id)
if service_object is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": "Service not found."},
)

Expand Down Expand Up @@ -251,7 +257,7 @@ async def download_service_stream_sse(service_id: str, request: Request):
service_object = services.get_service_by_id(service_id)
if service_object is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": "Service not found."},
)
event_generator = generator(service_object, request)
Expand All @@ -262,7 +268,7 @@ async def download_service_stream_sse(service_id: str, request: Request):
"/run-service/",
response_model=schemas.SuccessResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Failed to start container or service not found.",
}
Expand All @@ -277,14 +283,14 @@ async def run_service(body: schemas.RunServiceInput):

if service_object is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": "Service not found."},
)

port = services.run_container_with_retries(service_object)
if port is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={
"message": f"Failed to create container for {service_object['id']}"
},
Expand All @@ -299,7 +305,7 @@ async def run_service(body: schemas.RunServiceInput):
"/stop-service/{service_id}",
response_model=schemas.SuccessResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Failed to stop container or service not found.",
}
Expand All @@ -309,7 +315,7 @@ async def stop_service(service_id: str):
service_object = services.get_service_by_id(service_id)
if service_object is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": "Service not found."},
)

Expand All @@ -322,7 +328,7 @@ async def stop_service(service_id: str):
return {"message": f"Container {service_object['id']} not found."}
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to stop container {error}"},
) from error
return {"message": f"Service {service_object['id']} successfully."}
Expand All @@ -332,7 +338,7 @@ async def stop_service(service_id: str):
"/restart-service/{service_id}",
response_model=schemas.SuccessResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Failed to restart container or service not found.",
}
Expand All @@ -342,7 +348,7 @@ async def restart_service(service_id: str):
service_object = services.get_service_by_id(service_id)
if service_object is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": "Service not found."},
)

Expand All @@ -359,7 +365,7 @@ async def restart_service(service_id: str):
return {"message": f"Container {service_object['id']} not found."}
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to restart container {error}"},
) from error
return {"message": f"Service {service_object['id']} restarted successfully."}
Expand All @@ -369,7 +375,7 @@ async def restart_service(service_id: str):
"/stop-all-services/",
response_model=schemas.SuccessResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Failed to stop container or service not found.",
}
Expand All @@ -380,7 +386,7 @@ async def stop_all_services():
services.stop_all_running_services()
except Exception as error:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to stop container {error}"},
) from error
return {"message": "All services stopped successfully."}
Expand All @@ -391,7 +397,7 @@ async def remove_service(service_id):
service_object = services.get_service_by_id(service_id)
if service_object is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": "Service not found."},
)

Expand All @@ -403,7 +409,7 @@ async def remove_service(service_id):
logger.warning(error)
return {"message": f"Image {service_object['dockerImage']} not found."}
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to remove image {error}"},
) from error
return {"message": f"Service {service_object['id']} removed successfully."}
Expand All @@ -417,7 +423,7 @@ async def remove_volume(volume_name):
except Exception as error:
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to remove image {error}"},
) from error
return {"message": f"Volume {volume_name} removed successfully."}
Expand All @@ -430,7 +436,7 @@ async def system_prune():
except Exception as error:
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to prune docker {error}"},
) from error
return {"message": "System pruned successfully."}
Expand All @@ -440,7 +446,7 @@ async def system_prune():
"/stats/{service_id}",
response_model=schemas.ContainerStatsResponse,
responses={
400: {
HTTPStatus.BAD_REQUEST: {
"model": schemas.ErrorResponse,
"description": "Failed to get stats or service not found.",
}
Expand All @@ -450,7 +456,7 @@ async def stats_by_service(service_id: str):
service_object = services.get_service_by_id(service_id)
if service_object is None:
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": "Service not found."},
)

Expand All @@ -461,7 +467,7 @@ async def stats_by_service(service_id: str):
except Exception as error:
logger.error(error)
raise HTTPException(
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
detail={"message": f"Failed to remove image {error}"},
) from error

Expand Down
4 changes: 3 additions & 1 deletion resources/mocks/generic/check.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from http import HTTPStatus

import requests

response = requests.post(
Expand All @@ -8,6 +10,6 @@
},
stream=True,
)
if response.status_code == 200:
if response.status_code == HTTPStatus.OK:
for chunk in response.iter_content(chunk_size=1024):
print(chunk)
Loading