Skip to content

Commit e610f5d

Browse files
committed
Fix ruff issues
1 parent e569af4 commit e610f5d

File tree

3 files changed

+68
-70
lines changed

3 files changed

+68
-70
lines changed

src/server/models.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
class PatternType(str, Enum):
1515
"""Enumeration for pattern types used in file filtering."""
16-
16+
1717
INCLUDE = "include"
1818
EXCLUDE = "exclude"
1919

2020

2121
class IngestRequest(BaseModel):
2222
"""Request model for the /api/ingest endpoint.
23-
23+
2424
Attributes
2525
----------
2626
input_text : str
@@ -33,32 +33,34 @@ class IngestRequest(BaseModel):
3333
Glob/regex pattern string for file filtering.
3434
token : str | None
3535
GitHub personal access token (PAT) for accessing private repositories.
36+
3637
"""
37-
38+
3839
input_text: str = Field(..., description="Git repository URL or slug to ingest")
3940
max_file_size: int = Field(..., ge=0, le=500, description="File size slider position (0-500)")
4041
pattern_type: PatternType = Field(default=PatternType.EXCLUDE, description="Pattern type for file filtering")
4142
pattern: str = Field(default="", description="Glob/regex pattern for file filtering")
4243
token: str | None = Field(default=None, description="GitHub PAT for private repositories")
43-
44-
@field_validator('input_text')
44+
45+
@field_validator("input_text")
4546
@classmethod
46-
def validate_input_text(cls, v):
47+
def validate_input_text(cls, v: str) -> str:
4748
"""Validate that input_text is not empty."""
4849
if not v.strip():
49-
raise ValueError('input_text cannot be empty')
50+
err = "input_text cannot be empty"
51+
raise ValueError(err)
5052
return v.strip()
51-
52-
@field_validator('pattern')
53+
54+
@field_validator("pattern")
5355
@classmethod
54-
def validate_pattern(cls, v):
56+
def validate_pattern(cls, v: str) -> str:
5557
"""Validate pattern field."""
5658
return v.strip() if v else ""
5759

5860

5961
class IngestSuccessResponse(BaseModel):
6062
"""Success response model for the /api/ingest endpoint.
61-
63+
6264
Attributes
6365
----------
6466
result : Literal[True]
@@ -81,8 +83,9 @@ class IngestSuccessResponse(BaseModel):
8183
The pattern used for filtering.
8284
token : str | None
8385
The token used (if any).
86+
8487
"""
85-
88+
8689
result: Literal[True] = True
8790
repo_url: str = Field(..., description="Original repository URL")
8891
short_repo_url: str = Field(..., description="Short repository URL (user/repo)")
@@ -97,7 +100,7 @@ class IngestSuccessResponse(BaseModel):
97100

98101
class IngestErrorResponse(BaseModel):
99102
"""Error response model for the /api/ingest endpoint.
100-
103+
101104
Attributes
102105
----------
103106
error : str
@@ -112,8 +115,9 @@ class IngestErrorResponse(BaseModel):
112115
The pattern that was used.
113116
token : str | None
114117
The token that was used (if any).
118+
115119
"""
116-
120+
117121
error: str = Field(..., description="Error message")
118122
repo_url: str = Field(..., description="Repository URL that failed")
119123
default_file_size: int = Field(..., description="File size slider position used")

src/server/query_processor.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from pathlib import Path
6-
from typing import TYPE_CHECKING, Any, cast
6+
from typing import Any, cast
77

88
from gitingest.clone import clone_repo
99
from gitingest.ingestion import ingest_query
@@ -15,13 +15,8 @@
1515
)
1616
from server.server_utils import Colors, log_slider_to_size
1717

18-
if TYPE_CHECKING:
19-
from fastapi import Request
20-
from starlette.templating import _TemplateResponse
21-
2218

2319
async def process_query(
24-
request: Request,
2520
*,
2621
input_text: str,
2722
slider_position: int,
@@ -37,8 +32,6 @@ async def process_query(
3732
3833
Parameters
3934
----------
40-
request : Request
41-
The HTTP request object.
4235
input_text : str
4336
Input text provided by the user, typically a Git repository URL or slug.
4437
slider_position : int

src/server/routers/ingest.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
1-
from fastapi import APIRouter, Request, Form, HTTPException
1+
"""Ingest endpoint for the API."""
2+
3+
from fastapi import APIRouter
24
from fastapi.responses import JSONResponse
3-
from typing import Optional
5+
6+
from server.form_types import IntForm, OptStrForm, StrForm
7+
from server.models import IngestErrorResponse, IngestRequest, IngestSuccessResponse, PatternType
48
from server.query_processor import process_query
59
from server.server_utils import limiter
6-
from server.models import IngestRequest, IngestSuccessResponse, IngestErrorResponse, IngestResponse, PatternType
710

811
router = APIRouter()
912

10-
@router.post("/api/ingest",
11-
responses={
12-
200: {"model": IngestSuccessResponse, "description": "Successful ingestion"},
13-
400: {"model": IngestErrorResponse, "description": "Bad request or processing error"},
14-
500: {"model": IngestErrorResponse, "description": "Internal server error"}
15-
})
13+
14+
@router.post(
15+
"/api/ingest",
16+
responses={
17+
200: {"model": IngestSuccessResponse, "description": "Successful ingestion"},
18+
400: {"model": IngestErrorResponse, "description": "Bad request or processing error"},
19+
500: {"model": IngestErrorResponse, "description": "Internal server error"},
20+
},
21+
)
1622
@limiter.limit("10/minute")
1723
async def api_ingest(
18-
request: Request,
19-
input_text: str = Form(...),
20-
max_file_size: int = Form(...),
21-
pattern_type: str = Form("exclude"),
22-
pattern: str = Form(""),
23-
token: Optional[str] = Form(None),
24-
):
24+
*,
25+
input_text: StrForm,
26+
max_file_size: IntForm,
27+
pattern_type: StrForm = "exclude",
28+
pattern: StrForm = "",
29+
token: OptStrForm = None,
30+
) -> JSONResponse:
2531
"""Ingest a Git repository and return processed content.
26-
32+
2733
This endpoint processes a Git repository by cloning it, analyzing its structure,
2834
and returning a summary with the repository's content. The response includes
2935
file tree structure, processed content, and metadata about the ingestion.
30-
36+
3137
Parameters
3238
----------
33-
input_text : str
39+
input_text : StrForm
3440
Git repository URL or slug to ingest
35-
max_file_size : int
41+
max_file_size : IntForm
3642
Maximum file size slider position (0-500) for filtering files
37-
pattern_type : str
43+
pattern_type : StrForm
3844
Type of pattern to use for file filtering ("include" or "exclude")
39-
pattern : str
45+
pattern : StrForm
4046
Glob/regex pattern string for file filtering
41-
token : str | None
47+
token : OptStrForm
4248
GitHub personal access token (PAT) for accessing private repositories
43-
49+
4450
Returns
4551
-------
4652
JSONResponse
4753
Success response with ingestion results or error response with appropriate HTTP status code
48-
49-
Raises
50-
------
51-
HTTPException
52-
If validation fails or processing encounters an error
54+
5355
"""
5456
try:
5557
# Validate input using Pydantic model
@@ -58,19 +60,18 @@ async def api_ingest(
5860
max_file_size=max_file_size,
5961
pattern_type=PatternType(pattern_type),
6062
pattern=pattern,
61-
token=token
63+
token=token,
6264
)
63-
65+
6466
context = await process_query(
65-
request,
6667
input_text=ingest_request.input_text,
6768
slider_position=ingest_request.max_file_size,
68-
pattern_type=ingest_request.pattern_type.value,
69+
pattern_type=ingest_request.pattern_type,
6970
pattern=ingest_request.pattern,
7071
is_index=True,
7172
token=ingest_request.token,
7273
)
73-
74+
7475
if "error" in context:
7576
# Return structured error response with 400 status code
7677
error_response = IngestErrorResponse(
@@ -79,13 +80,13 @@ async def api_ingest(
7980
default_file_size=context.get("default_file_size", max_file_size),
8081
pattern_type=context.get("pattern_type", pattern_type),
8182
pattern=context.get("pattern", pattern),
82-
token=token
83+
token=token,
8384
)
8485
return JSONResponse(
8586
status_code=400,
86-
content=error_response.model_dump()
87+
content=error_response.model_dump(),
8788
)
88-
89+
8990
# Return structured success response with 200 status code
9091
success_response = IngestSuccessResponse(
9192
result=True,
@@ -97,39 +98,39 @@ async def api_ingest(
9798
default_file_size=context["default_file_size"],
9899
pattern_type=context["pattern_type"],
99100
pattern=context["pattern"],
100-
token=context.get("token")
101+
token=context.get("token"),
101102
)
102103
return JSONResponse(
103104
status_code=200,
104-
content=success_response.model_dump()
105+
content=success_response.model_dump(),
105106
)
106-
107+
107108
except ValueError as ve:
108109
# Handle validation errors with 400 status code
109110
error_response = IngestErrorResponse(
110-
error=f"Validation error: {str(ve)}",
111+
error=f"Validation error: {ve!s}",
111112
repo_url=input_text,
112113
default_file_size=max_file_size,
113114
pattern_type=pattern_type,
114115
pattern=pattern,
115-
token=token
116+
token=token,
116117
)
117118
return JSONResponse(
118119
status_code=400,
119-
content=error_response.model_dump()
120+
content=error_response.model_dump(),
120121
)
121-
122+
122123
except Exception as exc:
123124
# Handle unexpected errors with 500 status code
124125
error_response = IngestErrorResponse(
125-
error=f"Internal server error: {str(exc)}",
126+
error=f"Internal server error: {exc!s}",
126127
repo_url=input_text,
127128
default_file_size=max_file_size,
128129
pattern_type=pattern_type,
129130
pattern=pattern,
130-
token=token
131+
token=token,
131132
)
132133
return JSONResponse(
133134
status_code=500,
134-
content=error_response.model_dump()
135-
)
135+
content=error_response.model_dump(),
136+
)

0 commit comments

Comments
 (0)