Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

5 changes: 5 additions & 0 deletions url_finder/src/api/api_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The service is using [CID Contact](https://cid.contact) as source of HTTP entry
handle_find_url_sp_client,
handle_find_retri_by_client_and_sp,
handle_find_retri_by_sp,
handle_find_client,

handle_create_job,
handle_get_job,
Expand All @@ -52,6 +53,10 @@ The service is using [CID Contact](https://cid.contact) as source of HTTP entry
FindRetriByClientAndSpPath,
FindRetriByClientAndSpResponse,

// Client
FindByClientPath,
FindByClientResponse,

// Job
CreateJobPayload,
CreateJobResponse,
Expand Down
61 changes: 40 additions & 21 deletions url_finder/src/api/create_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::ResultCode;

#[derive(Deserialize, Serialize, ToSchema)]
pub struct CreateJobPayload {
provider: String,
provider: Option<String>,
client: Option<String>,
}

Expand All @@ -33,13 +33,14 @@ pub struct CreateJobResponse {
id: Option<Uuid>,
}

/// Create a job to find working urls or retrievability for a given SP and Client address
/// Create a job to find working urls or retrievability for
/// Either by SP or Client address or both
#[utoipa::path(
post,
path = "/jobs",
request_body(content = CreateJobPayload),
description = r#"
**Create a job to find working urls or retrievability for a given SP and Client address**
**Create a job to find working urls or retrievability for either by SP or Client address or both**
"#,
responses(
(status = 200, description = "Successful job creation", body = CreateJobResponse),
Expand All @@ -60,28 +61,38 @@ pub async fn handle_create_job(
&payload.provider, &payload.client
);

// validate provider and client addresses
let address_pattern = Regex::new(r"^f0\d{1,8}$").unwrap();
if !address_pattern.is_match(&payload.provider)
|| (payload.client.is_some() && !address_pattern.is_match(payload.client.as_ref().unwrap()))
{
if payload.provider.is_none() && payload.client.is_none() {
return Err(bad_request(
"Invalid provider or client address".to_string(),
"Either provider, client address or both must be provided".to_string(),
));
}

// Verify that we have http endpoint for the provider
let _ = match provider_endpoints::get_provider_endpoints(&payload.provider).await {
Ok((result_code, endpoints)) if endpoints.is_none() => {
debug!("No endpoints found");
return Ok(ok_response(CreateJobResponse {
result: result_code,
id: None,
}));
}
Err(e) => return Err(internal_server_error(e.to_string())),
Ok(result) => result,
};
if let Some(client) = &payload.client {
validate_address(client).map_err(|e| {
error!("Invalid client address: {}", e);
bad_request(e)
})?;
}

if let Some(provider) = &payload.provider {
validate_address(provider).map_err(|e| {
error!("Invalid provider address: {}", e);
bad_request(e)
})?;

// Verify that we have http endpoint for the provider
let _ = match provider_endpoints::get_provider_endpoints(provider).await {
Ok((result_code, endpoints)) if endpoints.is_none() => {
debug!("No endpoints found");
return Ok(ok_response(CreateJobResponse {
result: result_code,
id: None,
}));
}
Err(e) => return Err(internal_server_error(e.to_string())),
Ok(result) => result,
};
}

let job = state
.job_repo
Expand All @@ -97,3 +108,11 @@ pub async fn handle_create_job(
id: Some(job.id),
}))
}

fn validate_address(address: &str) -> Result<(), String> {
let address_pattern = Regex::new(r"^f0\d{1,8}$").unwrap();
if !address_pattern.is_match(address) {
return Err("Invalid provider or client address".to_string());
}
Ok(())
}
Loading