11// Copyright (c) 2024 Cloudflare, Inc. All rights reserved.
22// SPDX-License-Identifier: BSD-3-Clause
33
4- use anyhow:: { anyhow, Context as _ } ;
4+ use anyhow:: { anyhow, Context } ;
55use daphne:: {
66 constants:: DapMediaType ,
77 error:: aborts:: ProblemDetails ,
@@ -12,7 +12,7 @@ use daphne::{
1212 DapVersion ,
1313} ;
1414use daphne_service_utils:: { bearer_token:: BearerToken , http_headers} ;
15- use prio:: codec:: { ParameterizedDecode as _ , ParameterizedEncode as _} ;
15+ use prio:: codec:: { ParameterizedDecode , ParameterizedEncode as _} ;
1616use reqwest:: header;
1717use url:: Url ;
1818
@@ -43,30 +43,30 @@ impl HttpClient {
4343 . send ( )
4444 . await
4545 . context ( "sending AggregationJobInitReq" ) ?;
46- if resp . status ( ) == 400 {
47- let text = resp . text ( ) . await ? ;
48- let problem_details : ProblemDetails =
49- serde_json :: from_str ( & text ) . with_context ( || {
50- format ! ( "400 Bad Request: failed to parse problem details document: {text:?}" )
51- } ) ? ;
52- Err ( anyhow ! ( "400 Bad Request: {problem_details:?}" ) )
53- } else if resp . status ( ) == 500 {
54- Err ( anyhow:: anyhow! (
55- "500 Internal Server Error: {}" ,
56- resp . text ( ) . await ?
57- ) )
58- } else if !resp . status ( ) . is_success ( ) {
59- Err ( response_to_anyhow ( resp ) . await ) . context ( "while running an AggregationJobInitReq" )
60- } else {
61- AggregationJobResp :: get_decoded_with_param (
62- & version ,
63- & resp
64- . bytes ( )
65- . await
66- . context ( "transfering bytes from the AggregateInitReq" ) ? ,
67- )
68- . with_context ( || "failed to parse response to AggregateInitReq from Helper" )
69- }
46+ handle_response ( resp , & version ) . await
47+ }
48+
49+ pub async fn poll_aggregation_job_init (
50+ & self ,
51+ url : Url ,
52+ version : DapVersion ,
53+ opts : Options < ' _ > ,
54+ ) -> anyhow:: Result < AggregationJobResp > {
55+ let resp = self
56+ . get ( url )
57+ . headers ( construct_request_headers (
58+ DapMediaType :: AggregationJobInitReq
59+ . as_str_for_version ( version )
60+ . with_context ( || {
61+ format ! ( "AggregationJobInitReq media type is not defined for {version}" )
62+ } ) ? ,
63+ version ,
64+ opts ,
65+ ) ? )
66+ . send ( )
67+ . await
68+ . context ( "polling aggregation job init req" ) ? ;
69+ handle_response ( resp , & version ) . await
7070 }
7171
7272 pub async fn get_aggregate_share (
@@ -144,3 +144,32 @@ fn construct_request_headers(
144144 }
145145 Ok ( headers)
146146}
147+
148+ async fn handle_response < R , P > ( resp : reqwest:: Response , params : & P ) -> anyhow:: Result < R >
149+ where
150+ R : ParameterizedDecode < P > ,
151+ {
152+ if resp. status ( ) == 400 {
153+ let text = resp. text ( ) . await ?;
154+ let problem_details: ProblemDetails = serde_json:: from_str ( & text) . with_context ( || {
155+ format ! ( "400 Bad Request: failed to parse problem details document: {text:?}" )
156+ } ) ?;
157+ Err ( anyhow ! ( "400 Bad Request: {problem_details:?}" ) )
158+ } else if resp. status ( ) == 500 {
159+ Err ( anyhow:: anyhow!(
160+ "500 Internal Server Error: {}" ,
161+ resp. text( ) . await ?
162+ ) )
163+ } else if !resp. status ( ) . is_success ( ) {
164+ Err ( response_to_anyhow ( resp) . await ) . context ( "while running an AggregationJobInitReq" )
165+ } else {
166+ R :: get_decoded_with_param (
167+ params,
168+ & resp
169+ . bytes ( )
170+ . await
171+ . context ( "transfering bytes from the AggregateInitReq" ) ?,
172+ )
173+ . with_context ( || "failed to parse response to AggregateInitReq from Helper" )
174+ }
175+ }
0 commit comments