|
1 | 1 | """Routes to add an FMU project to an existing session.""" |
2 | 2 |
|
3 | 3 | from pathlib import Path |
| 4 | +from typing import Final |
4 | 5 |
|
5 | 6 | from fastapi import APIRouter, HTTPException, Response |
6 | 7 | from fmu.settings import find_nearest_fmu_directory, get_fmu_directory |
|
17 | 18 | add_fmu_project_to_session, |
18 | 19 | remove_fmu_project_from_session, |
19 | 20 | ) |
| 21 | +from fmu_settings_api.v1.responses import GetSessionResponses, Responses |
20 | 22 |
|
21 | 23 | router = APIRouter(prefix="/project", tags=["project"]) |
22 | 24 |
|
| 25 | +ProjectResponses: Final[Responses] = { |
| 26 | + 403: { |
| 27 | + "description": ( |
| 28 | + "The OS returned a permissions error while locating or creating .fmu" |
| 29 | + ), |
| 30 | + "content": { |
| 31 | + "application/json": { |
| 32 | + "example": { |
| 33 | + "examples": [ |
| 34 | + {"detail": "Permission denied locating .fmu"}, |
| 35 | + {"detail": "Permission denied accessing .fmu at {path}"}, |
| 36 | + {"detail": "Permission denied creating .fmu at {path}"}, |
| 37 | + ], |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + 404: { |
| 43 | + "description": ( |
| 44 | + "The .fmu directory was unable to be found at or above a given path, or " |
| 45 | + "the requested path to create a project .fmu directory at does not exist." |
| 46 | + ), |
| 47 | + "content": { |
| 48 | + "application/json": { |
| 49 | + "example": { |
| 50 | + "examples": [ |
| 51 | + {"detail": "No .fmu directory found from {path}"}, |
| 52 | + {"detail": "No .fmu directory found at {path}"}, |
| 53 | + {"detail": "Path {path} does not exist"}, |
| 54 | + ], |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | +} |
23 | 60 |
|
24 | | -@router.get("/", response_model=FMUProject) |
| 61 | +ProjectExistsResponses: Final[Responses] = { |
| 62 | + 409: { |
| 63 | + "description": ( |
| 64 | + "A project .fmu directory already exist at a given location, or may " |
| 65 | + "possibly not be a directory, i.e. it may be a .fmu file." |
| 66 | + ), |
| 67 | + "content": { |
| 68 | + "application/json": { |
| 69 | + "example": { |
| 70 | + "examples": [ |
| 71 | + {"detail": ".fmu exists at {path} but is not a directory"}, |
| 72 | + {"detail": ".fmu already exists at {path}"}, |
| 73 | + ], |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +@router.get( |
| 82 | + "/", |
| 83 | + response_model=FMUProject, |
| 84 | + summary="Returns the paths and configuration of the nearest project .fmu directory", |
| 85 | + description=( |
| 86 | + "If a project is not already attached to the session id it will be " |
| 87 | + "attached after a call to this route. If one is already attached this " |
| 88 | + "route will return data for the project .fmu directory again." |
| 89 | + ), |
| 90 | + responses={ |
| 91 | + **GetSessionResponses, |
| 92 | + **ProjectResponses, |
| 93 | + }, |
| 94 | +) |
25 | 95 | async def get_project(session: SessionDep) -> FMUProject: |
26 | | - """Returns the paths and configuration for the nearest project .fmu directory. |
| 96 | + """Returns the paths and configuration of the nearest project .fmu directory. |
27 | 97 |
|
28 | 98 | This directory is searched for above the current working directory. |
29 | 99 |
|
@@ -62,7 +132,24 @@ async def get_project(session: SessionDep) -> FMUProject: |
62 | 132 | raise HTTPException(status_code=500, detail=str(e)) from e |
63 | 133 |
|
64 | 134 |
|
65 | | -@router.post("/", response_model=FMUProject) |
| 135 | +@router.post( |
| 136 | + "/", |
| 137 | + response_model=FMUProject, |
| 138 | + summary=( |
| 139 | + "Returns the path and configuration of the project .fmu directory at 'path'" |
| 140 | + ), |
| 141 | + description=( |
| 142 | + "Used for when a user selects a project .fmu directory in a directory not " |
| 143 | + "found above the user's current working directory. Will overwrite the " |
| 144 | + "project .fmu directory attached to a session if one exists. If not, it is " |
| 145 | + "added to the session." |
| 146 | + ), |
| 147 | + responses={ |
| 148 | + **GetSessionResponses, |
| 149 | + **ProjectResponses, |
| 150 | + **ProjectExistsResponses, |
| 151 | + }, |
| 152 | +) |
66 | 153 | async def post_project(session: SessionDep, fmu_dir_path: FMUDirPath) -> FMUProject: |
67 | 154 | """Returns the paths and configuration for the project .fmu directory at 'path'.""" |
68 | 155 | path = fmu_dir_path.path |
@@ -91,26 +178,23 @@ async def post_project(session: SessionDep, fmu_dir_path: FMUDirPath) -> FMUProj |
91 | 178 | raise HTTPException(status_code=500, detail=str(e)) from e |
92 | 179 |
|
93 | 180 |
|
94 | | -@router.delete("/", response_model=Message) |
95 | | -async def delete_project_session( |
96 | | - session: ProjectSessionDep, response: Response |
97 | | -) -> Message: |
98 | | - """Deletes a project .fmu session if it exists.""" |
99 | | - try: |
100 | | - await remove_fmu_project_from_session(session.id) |
101 | | - return Message( |
102 | | - message=( |
103 | | - f"FMU directory {session.project_fmu_directory.path} closed " |
104 | | - "successfully" |
105 | | - ), |
106 | | - ) |
107 | | - except SessionNotFoundError as e: |
108 | | - raise HTTPException(status_code=401, detail=str(e)) from e |
109 | | - except Exception as e: |
110 | | - raise HTTPException(status_code=500, detail=str(e)) from e |
111 | | - |
112 | | - |
113 | | -@router.post("/init", response_model=FMUProject) |
| 181 | +@router.post( |
| 182 | + "/init", |
| 183 | + response_model=FMUProject, |
| 184 | + summary=( |
| 185 | + "Initializes a project .fmu directory at 'path' and returns its paths and " |
| 186 | + "configuration" |
| 187 | + ), |
| 188 | + description=( |
| 189 | + "If a project .fmu directory is already attached to the session, this will " |
| 190 | + "switch to use the newly created .fmu directory." |
| 191 | + ), |
| 192 | + responses={ |
| 193 | + **GetSessionResponses, |
| 194 | + **ProjectResponses, |
| 195 | + **ProjectExistsResponses, |
| 196 | + }, |
| 197 | +) |
114 | 198 | async def init_project( |
115 | 199 | session: SessionDep, |
116 | 200 | fmu_dir_path: FMUDirPath, |
@@ -142,3 +226,33 @@ async def init_project( |
142 | 226 | ) from e |
143 | 227 | except Exception as e: |
144 | 228 | raise HTTPException(status_code=500, detail=str(e)) from e |
| 229 | + |
| 230 | + |
| 231 | +@router.delete( |
| 232 | + "/", |
| 233 | + response_model=Message, |
| 234 | + summary="Removes a project .fmu directory from a session", |
| 235 | + description=( |
| 236 | + "This route simply removes an opened project .fmu directory from a session. " |
| 237 | + "It does not affect the user other aside from that." |
| 238 | + ), |
| 239 | + responses={ |
| 240 | + **GetSessionResponses, |
| 241 | + }, |
| 242 | +) |
| 243 | +async def delete_project_session( |
| 244 | + session: ProjectSessionDep, response: Response |
| 245 | +) -> Message: |
| 246 | + """Deletes a project .fmu session if it exists.""" |
| 247 | + try: |
| 248 | + await remove_fmu_project_from_session(session.id) |
| 249 | + return Message( |
| 250 | + message=( |
| 251 | + f"FMU directory {session.project_fmu_directory.path} closed " |
| 252 | + "successfully" |
| 253 | + ), |
| 254 | + ) |
| 255 | + except SessionNotFoundError as e: |
| 256 | + raise HTTPException(status_code=401, detail=str(e)) from e |
| 257 | + except Exception as e: |
| 258 | + raise HTTPException(status_code=500, detail=str(e)) from e |
0 commit comments