1+ from threading import Lock
12from typing import List , Optional
23
34import requests
45import structlog
5- from fastapi import APIRouter , HTTPException , Response
6+ from fastapi import APIRouter , Depends , HTTPException , Response
67from fastapi .responses import StreamingResponse
78from fastapi .routing import APIRoute
89from pydantic import ValidationError
910
1011from codegate import __version__
1112from codegate .api import v1_models , v1_processing
1213from codegate .db .connection import AlreadyExistsError , DbReader
14+ from codegate .providers import crud as provendcrud
1315from codegate .workspaces import crud
1416
1517logger = structlog .get_logger ("codegate" )
1618
19+ mtx = Lock ()
20+
1721v1 = APIRouter ()
1822wscrud = crud .WorkspaceCrud ()
1923
@@ -26,26 +30,24 @@ def uniq_name(route: APIRoute):
2630
2731
2832@v1 .get ("/provider-endpoints" , tags = ["Providers" ], generate_unique_id_function = uniq_name )
29- async def list_provider_endpoints (name : Optional [str ] = None ) -> List [v1_models .ProviderEndpoint ]:
33+ async def list_provider_endpoints (
34+ name : Optional [str ] = None ,
35+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
36+ ) -> List [v1_models .ProviderEndpoint ]:
3037 """List all provider endpoints."""
31- # NOTE: This is a dummy implementation. In the future, we should have a proper
32- # implementation that fetches the provider endpoints from the database.
33- return [
34- v1_models .ProviderEndpoint (
35- id = 1 ,
36- name = "dummy" ,
37- description = "Dummy provider endpoint" ,
38- endpoint = "http://example.com" ,
39- provider_type = v1_models .ProviderType .openai ,
40- auth_type = v1_models .ProviderAuthType .none ,
41- )
42- ]
38+ try :
39+ return pcrud .list_endpoints ()
40+ except Exception :
41+ raise HTTPException (status_code = 500 , detail = "Internal server error" )
4342
4443
4544@v1 .get (
4645 "/provider-endpoints/{provider_id}" , tags = ["Providers" ], generate_unique_id_function = uniq_name
4746)
48- async def get_provider_endpoint (provider_id : int ) -> v1_models .ProviderEndpoint :
47+ async def get_provider_endpoint (
48+ provider_id : int ,
49+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
50+ ) -> v1_models .ProviderEndpoint :
4951 """Get a provider endpoint by ID."""
5052 # NOTE: This is a dummy implementation. In the future, we should have a proper
5153 # implementation that fetches the provider endpoint from the database.
@@ -65,7 +67,10 @@ async def get_provider_endpoint(provider_id: int) -> v1_models.ProviderEndpoint:
6567 generate_unique_id_function = uniq_name ,
6668 status_code = 201 ,
6769)
68- async def add_provider_endpoint (request : v1_models .ProviderEndpoint ) -> v1_models .ProviderEndpoint :
70+ async def add_provider_endpoint (
71+ request : v1_models .ProviderEndpoint ,
72+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
73+ ) -> v1_models .ProviderEndpoint :
6974 """Add a provider endpoint."""
7075 # NOTE: This is a dummy implementation. In the future, we should have a proper
7176 # implementation that adds the provider endpoint to the database.
@@ -76,7 +81,9 @@ async def add_provider_endpoint(request: v1_models.ProviderEndpoint) -> v1_model
7681 "/provider-endpoints/{provider_id}" , tags = ["Providers" ], generate_unique_id_function = uniq_name
7782)
7883async def update_provider_endpoint (
79- provider_id : int , request : v1_models .ProviderEndpoint
84+ provider_id : int ,
85+ request : v1_models .ProviderEndpoint ,
86+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
8087) -> v1_models .ProviderEndpoint :
8188 """Update a provider endpoint by ID."""
8289 # NOTE: This is a dummy implementation. In the future, we should have a proper
@@ -87,7 +94,10 @@ async def update_provider_endpoint(
8794@v1 .delete (
8895 "/provider-endpoints/{provider_id}" , tags = ["Providers" ], generate_unique_id_function = uniq_name
8996)
90- async def delete_provider_endpoint (provider_id : int ):
97+ async def delete_provider_endpoint (
98+ provider_id : int ,
99+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
100+ ):
91101 """Delete a provider endpoint by id."""
92102 # NOTE: This is a dummy implementation. In the future, we should have a proper
93103 # implementation that deletes the provider endpoint from the database.
@@ -99,7 +109,10 @@ async def delete_provider_endpoint(provider_id: int):
99109 tags = ["Providers" ],
100110 generate_unique_id_function = uniq_name ,
101111)
102- async def list_models_by_provider (provider_name : str ) -> List [v1_models .ModelByProvider ]:
112+ async def list_models_by_provider (
113+ provider_name : str ,
114+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
115+ ) -> List [v1_models .ModelByProvider ]:
103116 """List models by provider."""
104117 # NOTE: This is a dummy implementation. In the future, we should have a proper
105118 # implementation that fetches the models by provider from the database.
@@ -111,7 +124,9 @@ async def list_models_by_provider(provider_name: str) -> List[v1_models.ModelByP
111124 tags = ["Providers" ],
112125 generate_unique_id_function = uniq_name ,
113126)
114- async def list_all_models_for_all_providers () -> List [v1_models .ModelByProvider ]:
127+ async def list_all_models_for_all_providers (
128+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
129+ ) -> List [v1_models .ModelByProvider ]:
115130 """List all models for all providers."""
116131 # NOTE: This is a dummy implementation. In the future, we should have a proper
117132 # implementation that fetches all the models for all providers from the database.
@@ -394,7 +409,10 @@ async def delete_workspace_custom_instructions(workspace_name: str):
394409 tags = ["Workspaces" , "Muxes" ],
395410 generate_unique_id_function = uniq_name ,
396411)
397- async def get_workspace_muxes (workspace_name : str ) -> List [v1_models .MuxRule ]:
412+ async def get_workspace_muxes (
413+ workspace_name : str ,
414+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
415+ ) -> List [v1_models .MuxRule ]:
398416 """Get the mux rules of a workspace.
399417
400418 The list is ordered in order of priority. That is, the first rule in the list
@@ -422,7 +440,11 @@ async def get_workspace_muxes(workspace_name: str) -> List[v1_models.MuxRule]:
422440 generate_unique_id_function = uniq_name ,
423441 status_code = 204 ,
424442)
425- async def set_workspace_muxes (workspace_name : str , request : List [v1_models .MuxRule ]):
443+ async def set_workspace_muxes (
444+ workspace_name : str ,
445+ request : List [v1_models .MuxRule ],
446+ pcrud : provendcrud .ProviderCrud = Depends (provendcrud .ProviderCrud ),
447+ ):
426448 """Set the mux rules of a workspace."""
427449 # TODO: This is a dummy implementation. In the future, we should have a proper
428450 # implementation that sets the mux rules in the database.
0 commit comments