Skip to content

Commit

Permalink
Tidied up response codes: 202 for the upload of metadata and data, 20…
Browse files Browse the repository at this point in the history
…0 for status updates
  • Loading branch information
wpfl-dbt committed Feb 14, 2025
1 parent 0610a4d commit 7b1f91f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/matchbox/common/dtos.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,13 @@ class UploadStatus(BaseModel):
"complete": 200,
"failed": 400,
"awaiting_upload": 202,
"queued": 202,
"processing": 202,
"queued": 200,
"processing": 200,
}

def get_http_code(self) -> int:
def get_http_code(self, status: bool) -> int:
if self.status == "failed":
return 400
return self._status_code_mapping[self.status]

@classmethod
Expand Down
25 changes: 17 additions & 8 deletions src/matchbox/server/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def get_count(e: BackendCountableType) -> int:

@app.post(
"/upload/{upload_id}",
responses={400: {"model": UploadStatus, **UploadStatus.status_400_examples()}},
responses={
400: {"model": UploadStatus, **UploadStatus.status_400_examples()},
},
status_code=status.HTTP_202_ACCEPTED,
)
async def upload_file(
Expand Down Expand Up @@ -180,16 +182,24 @@ async def upload_file(
metadata_store=metadata_store,
)

return metadata_store.get(upload_id).status
source_cache = metadata_store.get(upload_id)

# Check for error in async task
if source_cache.status.status == "failed":
raise HTTPException(
status_code=400,
detail=source_cache.status.model_dump(),
)
else:
return source_cache.status


@app.get(
"/upload/{upload_id}/status",
responses={
200: {"model": UploadStatus},
202: {"model": UploadStatus},
400: {"model": UploadStatus, **UploadStatus.status_400_examples()},
},
status_code=status.HTTP_200_OK,
)
async def get_upload_status(
upload_id: str,
Expand All @@ -216,10 +226,7 @@ async def get_upload_status(
).model_dump(),
)

return JSONResponse(
status_code=source_cache.status.get_http_code(),
content=source_cache.status.model_dump(),
)
return source_cache.status


# Retrieval
Expand All @@ -237,6 +244,7 @@ async def query(
threshold: int | None = None,
limit: int | None = None,
) -> ParquetResponse:
"""Query Matchbox for matches based on a source address."""
source_address = SourceAddress(
full_name=full_name, warehouse_hash=warehouse_hash_b64
)
Expand Down Expand Up @@ -280,6 +288,7 @@ async def match(
resolution_name: str,
threshold: int | None = None,
) -> list[Match]:
"""Match a source primary key against a list of target addresses."""
targets = [
SourceAddress(full_name=n, warehouse_hash=w)
for n, w in zip(target_full_names, target_warehouse_hashes_b64, strict=True)
Expand Down
4 changes: 2 additions & 2 deletions test/client/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def test_index_success(MockSource: Mock, matchbox_api: MockRouter):
# Mock the data upload
upload_route = matchbox_api.post("/upload/test-upload-id").mock(
return_value=Response(
200,
202,
json=UploadStatus(
id="test-upload-id", status="complete", entity=BackendUploadType.INDEX
).model_dump(),
Expand Down Expand Up @@ -530,7 +530,7 @@ def test_index_with_columns(

upload_route = matchbox_api.post("/upload/test-upload-id").mock(
return_value=Response(
200,
202,
json=UploadStatus(
id="test-upload-id", status="complete", entity=BackendUploadType.INDEX
).model_dump(),
Expand Down
2 changes: 1 addition & 1 deletion test/client/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_results_getter(matchbox_api: MockRouter):

# Mock the GET /models/{name}/results endpoint
route = matchbox_api.get(f"/models/{dummy.model.metadata.name}/results").mock(
return_value=Response(202, content=table_to_buffer(dummy.data).read())
return_value=Response(200, content=table_to_buffer(dummy.data).read())
)

# Get results
Expand Down
6 changes: 3 additions & 3 deletions test/server/api/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_upload_status_check(metadata_store: Mock, _: Mock):
response = client.get(f"/upload/{update_id}/status")

# Should return current status
assert response.status_code == 202
assert response.status_code == 200
assert response.json()["status"] == "processing"
metadata_store.update_status.assert_not_called()

Expand Down Expand Up @@ -511,7 +511,7 @@ async def test_complete_source_upload_process(get_backend: Mock, s3: S3Client):
current_attempt = 0
while current_attempt < max_attempts:
response = client.get(f"/upload/{upload_id}/status")
assert response.status_code == 200 or response.status_code == 202
assert response.status_code == 200

status = response.json()["status"]
if status == "complete":
Expand Down Expand Up @@ -723,7 +723,7 @@ async def test_complete_model_upload_process(

while current_attempt < max_attempts:
response = client.get(f"/upload/{upload_id}/status")
assert response.status_code == 200 or response.status_code == 202
assert response.status_code == 200

status = response.json()["status"]
if status == "complete":
Expand Down

0 comments on commit 7b1f91f

Please sign in to comment.