Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GET /tasks/:task_id #383

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions documentation/public-task-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Accidentally modifying any of these plaintext values would make report generatio

## Proposed improvement

We add an infinitely cacheable (`Cache-Control: public, max-age=604800`) endpoint `GET {divviup-api url}/api/tasks/:task_id`. When a task is found with the provided task identifier, the divviup-api server responds with the following json:
We add an infinitely cacheable (`Cache-Control: public, max-age=604800`) endpoint `GET {divviup-api url}/tasks/:task_id`. When a task is found with the provided task identifier, the divviup-api server responds with the following json:

```json
{
Expand All @@ -33,15 +33,14 @@ We add an infinitely cacheable (`Cache-Control: public, max-age=604800`) endpoin
"leader": "https://dap.xxqbi.example/",
"helper": "https://dap.xxqbi.example/",
"time_precision_seconds": 1080,
"protocol": "DAP-04"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is removed because we have no way of determining per-task protocol currently. We will likely want to add that eventually

}
```

and the client can be configured like:

```js
import DivviupClient from "@divviup/client";
const client = new DivviupClient("https://api.divviup.org/api/tasks/5YXXYPFzt1a8cuo8AlKqs6oKbt3FIrkn3Q8JseJKRYs");
const client = new DivviupClient("https://api.divviup.org/tasks/5YXXYPFzt1a8cuo8AlKqs6oKbt3FIrkn3Q8JseJKRYs");
```

or, optionally, the following shortcut is also supported:
Expand Down
1 change: 1 addition & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn routes(config: &Config) -> impl Handler {
redirect(config.app_url.to_string()),
),
)
.get("/tasks/:task_id", api(tasks::public_show))
.any(
&[Get, Post, Delete, Patch],
"/api/*",
Expand Down
17 changes: 17 additions & 0 deletions src/routes/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
Crypter, Db, Error, Permissions, PermissionsActor,
};
use sea_orm::{ActiveModelTrait, EntityTrait, ModelTrait};
use serde_json::json;
use std::time::Duration;
use time::OffsetDateTime;
use trillium::{Conn, Handler, Status};
Expand Down Expand Up @@ -90,6 +91,22 @@ pub async fn show(
Ok(Json(task))
}

pub async fn public_show(conn: &mut Conn, db: Db) -> Result<impl Handler, Error> {
let task_id = conn.param("task_id").ok_or(Error::NotFound)?;
let task = Tasks::find_by_id(task_id)
.one(&db)
.await?
.ok_or(Error::NotFound)?;
let [leader, helper] = task.aggregators(&db).await?;
Ok(Json(json!({
"id": task.id,
"vdaf": task.vdaf,
"leader": leader.dap_url,
"helper": helper.dap_url,
"time_precision_seconds": task.time_precision_seconds,
})))
}

pub async fn update(
_: &mut Conn,
(task, Json(update), db): (Task, Json<UpdateTask>, Db),
Expand Down
26 changes: 26 additions & 0 deletions tests/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,29 @@ mod collector_auth_tokens {
Ok(())
}
}

mod public_show {
use divviup_api::entity::task::vdaf::{Sum, Vdaf};
use test_support::{assert_eq, test, *};
#[test(harness = set_up)]
async fn as_completely_unauthenticated_user(app: DivviupApi) -> TestResult {
let account = fixtures::account(&app).await;
let task = fixtures::task(&app, &account).await;
let mut task = task.into_active_model();
task.vdaf = ActiveValue::Set(Vdaf::Sum(Sum { bits: Some(8) }));
let task = task.update(app.db()).await?;
let [leader, helper] = task.aggregators(app.db()).await?;
assert_ok!(
get(format!("/tasks/{}", task.id)).run_async(&app).await,
serde_json::to_string(&json!({
"id": task.id,
"leader": leader.dap_url,
"helper": helper.dap_url,
"time_precision_seconds": task.time_precision_seconds,
"vdaf": { "type":"sum", "bits": 8 }
}))
.unwrap()
);
Ok(())
}
}
Loading