ares-api 0.1.0

HTTP server for Ares AI scraper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use std::sync::Arc;

use axum::Router;
use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::middleware;
use axum::response::IntoResponse;
use axum::routing::{delete, get, post, put};
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
use uuid::Uuid;

use ares_client::{HtmdCleaner, OpenAiExtractor, ReqwestFetcher};
use ares_core::job::CreateScrapeJobRequest;
use ares_core::job_queue::JobQueue;
use ares_core::{NullStore, SchemaResolver, ScrapeService};

use crate::auth::require_api_key;
use crate::dto::{
    CreateJobRequest, CreateJobResponse, CreateSchemaRequest, CreateSchemaResponse,
    ExtractionHistoryQuery, ExtractionHistoryResponse, ExtractionResponse, HealthResponse,
    JobListResponse, JobResponse, ListJobsQuery, SchemaDetailResponse, SchemaEntryResponse,
    SchemaListResponse, ScrapeRequest, ScrapeResponse, UpdateSchemaRequest,
};
use crate::error::ApiError;
use crate::openapi::ApiDoc;
use crate::state::AppState;

/// Build the full router with all routes and middleware.
pub fn router(state: Arc<AppState>) -> Router {
    let api = Router::new()
        .route("/v1/scrape", post(scrape))
        .route("/v1/jobs", post(create_job))
        .route("/v1/jobs", get(list_jobs))
        .route("/v1/jobs/{id}", get(get_job))
        .route("/v1/jobs/{id}", delete(cancel_job))
        .route("/v1/jobs/{id}/retry", post(retry_job))
        .route("/v1/extractions", get(get_extractions))
        .route("/v1/schemas", get(list_schemas))
        .route("/v1/schemas", post(create_schema))
        .route("/v1/schemas/{name}/{version}", get(get_schema))
        .route("/v1/schemas/{name}/{version}", put(update_schema_version))
        .route(
            "/v1/schemas/{name}/{version}",
            delete(delete_schema_version),
        )
        .layer(middleware::from_fn_with_state(
            state.clone(),
            require_api_key,
        ));

    let public = Router::new()
        .route("/health", get(health))
        .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()));

    public.merge(api).with_state(state)
}

// ---------------------------------------------------------------------------
// Scrape
// ---------------------------------------------------------------------------

#[utoipa::path(
    post,
    path = "/v1/scrape",
    request_body = ScrapeRequest,
    responses(
        (status = 200, description = "Extraction result", body = ScrapeResponse),
        (status = 400, description = "Bad request", body = crate::dto::ErrorResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "scrape"
)]
pub async fn scrape(
    State(state): State<Arc<AppState>>,
    axum::Json(body): axum::Json<ScrapeRequest>,
) -> Result<impl IntoResponse, ApiError> {
    // Resolve LLM config from request body or environment
    let api_key = std::env::var("ARES_API_KEY").map_err(|_| {
        ares_core::AppError::ConfigError(
            "ARES_API_KEY must be set for scrape endpoints".to_string(),
        )
    })?;

    let model = body.model.unwrap_or_else(|| {
        std::env::var("ARES_MODEL").unwrap_or_else(|_| "gpt-4o-mini".to_string())
    });

    let base_url = body.base_url.unwrap_or_else(|| {
        std::env::var("ARES_BASE_URL").unwrap_or_else(|_| "https://api.openai.com/v1".to_string())
    });

    let save = body.save.unwrap_or(true);

    // Build pipeline components
    let fetcher = ReqwestFetcher::new()?;
    let cleaner = HtmdCleaner::new();
    let extractor = OpenAiExtractor::with_base_url(&api_key, &model, &base_url)?;

    // Run the scrape pipeline
    let result = if save {
        let repo = state.db.extraction_repo();
        let service = ScrapeService::with_store(fetcher, cleaner, extractor, repo, model);
        service
            .scrape(&body.url, &body.schema, &body.schema_name)
            .await?
    } else {
        let service = ScrapeService::with_store(fetcher, cleaner, extractor, NullStore, model);
        service
            .scrape(&body.url, &body.schema, &body.schema_name)
            .await?
    };

    let response = ScrapeResponse {
        extracted_data: result.extracted_data,
        content_hash: result.content_hash,
        data_hash: result.data_hash,
        changed: result.changed,
        extraction_id: result.extraction_id,
    };

    Ok(axum::Json(response))
}

// ---------------------------------------------------------------------------
// Jobs
// ---------------------------------------------------------------------------

#[utoipa::path(
    post,
    path = "/v1/jobs",
    request_body = CreateJobRequest,
    responses(
        (status = 202, description = "Job created", body = CreateJobResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "jobs"
)]
pub async fn create_job(
    State(state): State<Arc<AppState>>,
    axum::Json(body): axum::Json<CreateJobRequest>,
) -> Result<impl IntoResponse, ApiError> {
    let request = CreateScrapeJobRequest::new(
        body.url,
        body.schema_name,
        body.schema,
        body.model,
        body.base_url,
    );
    let request = match body.max_retries {
        Some(max) => request.with_max_retries(max),
        None => request,
    };

    let job = state.db.job_repo().create_job(request).await?;

    let response = CreateJobResponse {
        job_id: job.id,
        status: job.status.to_string(),
    };

    Ok((StatusCode::ACCEPTED, axum::Json(response)))
}

#[utoipa::path(
    get,
    path = "/v1/jobs",
    params(ListJobsQuery),
    responses(
        (status = 200, description = "List of jobs", body = JobListResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "jobs"
)]
pub async fn list_jobs(
    State(state): State<Arc<AppState>>,
    Query(query): Query<ListJobsQuery>,
) -> Result<impl IntoResponse, ApiError> {
    let status_filter = query
        .status
        .map(|s| {
            s.parse()
                .map_err(|e: String| ares_core::error::AppError::Generic(e))
        })
        .transpose()?;

    let limit = query.limit.unwrap_or(20).min(100);
    let offset = query.offset.unwrap_or(0);
    let jobs = state
        .db
        .job_repo()
        .list_jobs(status_filter, limit, offset)
        .await?;
    let total = state.db.job_repo().count_jobs(status_filter).await? as usize;

    let response = JobListResponse {
        jobs: jobs.into_iter().map(JobResponse::from).collect(),
        total,
        limit,
        offset,
    };

    Ok(axum::Json(response))
}

#[utoipa::path(
    get,
    path = "/v1/jobs/{id}",
    params(
        ("id" = Uuid, Path, description = "Job ID")
    ),
    responses(
        (status = 200, description = "Job details", body = JobResponse),
        (status = 404, description = "Not found", body = crate::dto::ErrorResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "jobs"
)]
pub async fn get_job(
    State(state): State<Arc<AppState>>,
    Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, ApiError> {
    let job = state.db.job_repo().get_job(id).await?;

    match job {
        Some(job) => Ok(axum::Json(JobResponse::from(job)).into_response()),
        None => {
            let body = crate::dto::ErrorResponse {
                error: "not_found".to_string(),
                message: format!("Job not found: {id}"),
            };
            Ok((StatusCode::NOT_FOUND, axum::Json(body)).into_response())
        }
    }
}

#[utoipa::path(
    delete,
    path = "/v1/jobs/{id}",
    params(
        ("id" = Uuid, Path, description = "Job ID")
    ),
    responses(
        (status = 204, description = "Job cancelled"),
        (status = 404, description = "Not found", body = crate::dto::ErrorResponse),
        (status = 409, description = "Conflict", body = crate::dto::ErrorResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "jobs"
)]
pub async fn cancel_job(
    State(state): State<Arc<AppState>>,
    Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, ApiError> {
    // Check the job exists first
    let job = state.db.job_repo().get_job(id).await?;
    match job {
        Some(job) if job.status.is_terminal() => {
            let body = crate::dto::ErrorResponse {
                error: "conflict".to_string(),
                message: format!("Job {id} is already in terminal state: {}", job.status),
            };
            Ok((StatusCode::CONFLICT, axum::Json(body)).into_response())
        }
        Some(_) => {
            state.db.job_repo().cancel_job(id).await?;
            Ok(StatusCode::NO_CONTENT.into_response())
        }
        None => {
            let body = crate::dto::ErrorResponse {
                error: "not_found".to_string(),
                message: format!("Job not found: {id}"),
            };
            Ok((StatusCode::NOT_FOUND, axum::Json(body)).into_response())
        }
    }
}

#[utoipa::path(
    post,
    path = "/v1/jobs/{id}/retry",
    params(
        ("id" = Uuid, Path, description = "Job ID")
    ),
    responses(
        (status = 200, description = "Job retried", body = JobResponse),
        (status = 404, description = "Not found", body = crate::dto::ErrorResponse),
        (status = 409, description = "Conflict", body = crate::dto::ErrorResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "jobs"
)]
pub async fn retry_job(
    State(state): State<Arc<AppState>>,
    Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, ApiError> {
    // Attempt the atomic retry first to avoid TOCTOU races.
    let retried = state.db.job_repo().retry_job(id).await?;

    match retried {
        Some(job) => Ok(axum::Json(JobResponse::from(job)).into_response()),
        None => {
            // No row updated: either the job doesn't exist or isn't retryable.
            // Follow-up read to distinguish 404 vs 409.
            let job = state.db.job_repo().get_job(id).await?;
            match job {
                None => {
                    let body = crate::dto::ErrorResponse {
                        error: "not_found".to_string(),
                        message: format!("Job not found: {id}"),
                    };
                    Ok((StatusCode::NOT_FOUND, axum::Json(body)).into_response())
                }
                Some(job) => {
                    let body = crate::dto::ErrorResponse {
                        error: "conflict".to_string(),
                        message: format!("Job {id} is not in a retryable state: {}", job.status),
                    };
                    Ok((StatusCode::CONFLICT, axum::Json(body)).into_response())
                }
            }
        }
    }
}

#[utoipa::path(
    get,
    path = "/v1/extractions",
    params(ExtractionHistoryQuery),
    responses(
        (status = 200, description = "Extraction history", body = ExtractionHistoryResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "extractions"
)]
pub async fn get_extractions(
    State(state): State<Arc<AppState>>,
    Query(query): Query<ExtractionHistoryQuery>,
) -> Result<impl IntoResponse, ApiError> {
    let limit = query.limit.unwrap_or(10).min(100);
    let offset = query.offset.unwrap_or(0);
    let extractions = state
        .db
        .extraction_repo()
        .get_history(&query.url, &query.schema_name, limit, offset)
        .await?;
    let total = state
        .db
        .extraction_repo()
        .count_history(&query.url, &query.schema_name)
        .await? as usize;

    let response = ExtractionHistoryResponse {
        extractions: extractions
            .into_iter()
            .map(ExtractionResponse::from)
            .collect(),
        total,
        limit,
        offset,
    };

    Ok(axum::Json(response))
}

// ---------------------------------------------------------------------------
// Schemas
// ---------------------------------------------------------------------------

#[utoipa::path(
    get,
    path = "/v1/schemas",
    responses(
        (status = 200, description = "List of schemas", body = SchemaListResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "schemas"
)]
pub async fn list_schemas(
    State(state): State<Arc<AppState>>,
) -> Result<impl IntoResponse, ApiError> {
    let resolver = SchemaResolver::new(&state.schemas_dir);
    let entries = resolver.list_schemas()?;

    let response = SchemaListResponse {
        schemas: entries
            .into_iter()
            .map(|e| SchemaEntryResponse {
                name: e.name,
                latest_version: e.latest_version,
                versions: e.versions,
            })
            .collect(),
    };

    Ok(axum::Json(response))
}

#[utoipa::path(
    get,
    path = "/v1/schemas/{name}/{version}",
    params(
        ("name" = String, Path, description = "Schema name"),
        ("version" = String, Path, description = "Schema version"),
    ),
    responses(
        (status = 200, description = "Schema details", body = SchemaDetailResponse),
        (status = 404, description = "Not found", body = crate::dto::ErrorResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "schemas"
)]
pub async fn get_schema(
    State(state): State<Arc<AppState>>,
    Path((name, version)): Path<(String, String)>,
) -> Result<impl IntoResponse, ApiError> {
    let resolver = SchemaResolver::new(&state.schemas_dir);
    let schema_ref = format!("{name}@{version}");

    match resolver.resolve(&schema_ref) {
        Ok(resolved) => {
            let response = SchemaDetailResponse {
                name,
                version,
                schema: resolved.schema,
            };
            Ok(axum::Json(response).into_response())
        }
        Err(_) => {
            let body = crate::dto::ErrorResponse {
                error: "not_found".to_string(),
                message: format!("Schema not found: {schema_ref}"),
            };
            Ok((StatusCode::NOT_FOUND, axum::Json(body)).into_response())
        }
    }
}

#[utoipa::path(
    post,
    path = "/v1/schemas",
    request_body = CreateSchemaRequest,
    responses(
        (status = 201, description = "Schema created", body = CreateSchemaResponse),
        (status = 400, description = "Bad request", body = crate::dto::ErrorResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "schemas"
)]
pub async fn create_schema(
    State(state): State<Arc<AppState>>,
    axum::Json(body): axum::Json<CreateSchemaRequest>,
) -> Result<impl IntoResponse, ApiError> {
    let resolver = SchemaResolver::new(&state.schemas_dir);
    resolver.create_schema(&body.name, &body.version, &body.schema)?;

    let response = CreateSchemaResponse {
        name: body.name,
        version: body.version,
    };

    Ok((StatusCode::CREATED, axum::Json(response)))
}

#[utoipa::path(
    put,
    path = "/v1/schemas/{name}/{version}",
    params(
        ("name" = String, Path, description = "Schema name"),
        ("version" = String, Path, description = "Schema version"),
    ),
    request_body = UpdateSchemaRequest,
    responses(
        (status = 200, description = "Schema updated", body = SchemaDetailResponse),
        (status = 404, description = "Not found", body = crate::dto::ErrorResponse),
        (status = 400, description = "Bad request", body = crate::dto::ErrorResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "schemas"
)]
pub async fn update_schema_version(
    State(state): State<Arc<AppState>>,
    Path((name, version)): Path<(String, String)>,
    axum::Json(body): axum::Json<UpdateSchemaRequest>,
) -> Result<impl IntoResponse, ApiError> {
    let resolver = SchemaResolver::new(&state.schemas_dir);

    match resolver.update_schema(&name, &version, &body.schema) {
        Ok(()) => {
            let response = SchemaDetailResponse {
                name,
                version,
                schema: body.schema,
            };
            Ok(axum::Json(response).into_response())
        }
        Err(ares_core::AppError::SchemaNotFound { .. }) => {
            let body = crate::dto::ErrorResponse {
                error: "not_found".to_string(),
                message: format!("Schema not found: {name}@{version}"),
            };
            Ok((StatusCode::NOT_FOUND, axum::Json(body)).into_response())
        }
        Err(e) => Err(ApiError::from(e)),
    }
}

#[utoipa::path(
    delete,
    path = "/v1/schemas/{name}/{version}",
    params(
        ("name" = String, Path, description = "Schema name"),
        ("version" = String, Path, description = "Schema version"),
    ),
    responses(
        (status = 204, description = "Schema deleted"),
        (status = 404, description = "Not found", body = crate::dto::ErrorResponse),
        (status = 401, description = "Unauthorized"),
    ),
    security(("bearer" = [])),
    tag = "schemas"
)]
pub async fn delete_schema_version(
    State(state): State<Arc<AppState>>,
    Path((name, version)): Path<(String, String)>,
) -> Result<impl IntoResponse, ApiError> {
    let resolver = SchemaResolver::new(&state.schemas_dir);
    let schema_ref = format!("{name}@{version}");

    match resolver.delete_schema(&name, &version) {
        Ok(()) => Ok(StatusCode::NO_CONTENT.into_response()),
        Err(ares_core::AppError::SchemaNotFound { .. }) => {
            let body = crate::dto::ErrorResponse {
                error: "not_found".to_string(),
                message: format!("Schema not found: {schema_ref}"),
            };
            Ok((StatusCode::NOT_FOUND, axum::Json(body)).into_response())
        }
        Err(e) => Err(ApiError::from(e)),
    }
}

// ---------------------------------------------------------------------------
// Health
// ---------------------------------------------------------------------------

#[utoipa::path(
    get,
    path = "/health",
    responses(
        (status = 200, description = "Service is healthy", body = HealthResponse),
        (status = 503, description = "Service is unhealthy", body = HealthResponse),
    ),
    tag = "system"
)]
pub async fn health(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    let db_status = match state.db.extraction_repo().health_check().await {
        Ok(()) => "ok",
        Err(_) => "error",
    };

    let status = if db_status == "ok" {
        StatusCode::OK
    } else {
        StatusCode::SERVICE_UNAVAILABLE
    };

    let response = HealthResponse {
        status: if db_status == "ok" {
            "healthy"
        } else {
            "unhealthy"
        },
        database: db_status,
    };

    (status, axum::Json(response))
}