Skip to content

Commit 95c1af0

Browse files
committed
Create UserState version of _with_hash for measurement submission
1 parent 3090ffb commit 95c1af0

1 file changed

Lines changed: 62 additions & 15 deletions

File tree

ooniauth-py/src/protocol.rs

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -310,26 +310,40 @@ impl UserState {
310310
age_range: (u32, u32),
311311
min_measurement_count: u32,
312312
) -> OoniResult<SubmitRequest> {
313-
let probe_cc = probe_cc.to_str(py).expect("unable to get string");
314-
let probe_asn = probe_asn.to_str(py).expect("unable to get string");
315313
let measurement_hash = base64_32_arg(py, &measurement_hash, "measurement_hash")?;
316314

317-
let mut rng = rand::thread_rng();
318-
let ((result, client_state), nym) = self.state.submit_request(
319-
&mut rng,
320-
probe_cc.into(),
321-
probe_asn.into(),
315+
self.make_submit_request_impl(
316+
py,
317+
probe_cc,
318+
probe_asn,
322319
&measurement_hash,
323-
age_range.0..age_range.1,
324-
min_measurement_count..u32::MAX,
325-
)?;
320+
age_range,
321+
min_measurement_count,
322+
)
323+
}
326324

327-
self.submit_client_state = Some(client_state);
325+
/// Creates a submit request computing the hash from the input measurement.
326+
/// Computes the hash internally using the [submit_measurement_hash] function
327+
pub fn make_submit_request_with_hash(
328+
&mut self,
329+
py: Python<'_>,
330+
probe_cc: Py<PyString>,
331+
probe_asn: Py<PyString>,
332+
measurement: Py<PyString>,
333+
age_range: (u32, u32),
334+
min_measurement_count: u32,
335+
) -> OoniResult<SubmitRequest> {
336+
let measurement_str = py_string_arg(py, &measurement, "measurement")?;
337+
let measurement_hash = core_submit_measurement_hash(measurement_str.as_bytes());
328338

329-
Ok(SubmitRequest {
330-
nym: to_pystring(py, &nym),
331-
request: to_pystring(py, &result),
332-
})
339+
self.make_submit_request_impl(
340+
py,
341+
probe_cc,
342+
probe_asn,
343+
&measurement_hash,
344+
age_range,
345+
min_measurement_count,
346+
)
333347
}
334348

335349
/// Handle a submit response sent by the server, updating your credentials
@@ -383,6 +397,39 @@ impl UserState {
383397
}
384398
}
385399

400+
// Methods in this implementation block are not exposed to Python
401+
impl UserState {
402+
fn make_submit_request_impl(
403+
&mut self,
404+
py: Python<'_>,
405+
probe_cc: Py<PyString>,
406+
probe_asn: Py<PyString>,
407+
measurement_hash: &[u8; 32],
408+
age_range: (u32, u32),
409+
min_measurement_count: u32,
410+
) -> OoniResult<SubmitRequest> {
411+
let probe_cc = py_string_arg(py, &probe_cc, "probe_cc")?;
412+
let probe_asn = py_string_arg(py, &probe_asn, "probe_asn")?;
413+
414+
let mut rng = rand::thread_rng();
415+
let ((result, client_state), nym) = self.state.submit_request(
416+
&mut rng,
417+
probe_cc.into(),
418+
probe_asn.into(),
419+
measurement_hash,
420+
age_range.0..age_range.1,
421+
min_measurement_count..u32::MAX,
422+
)?;
423+
424+
self.submit_client_state = Some(client_state);
425+
426+
Ok(SubmitRequest {
427+
nym: to_pystring(py, &nym),
428+
request: to_pystring(py, &result),
429+
})
430+
}
431+
}
432+
386433
#[gen_stub_pyclass]
387434
#[pyclass]
388435
pub struct SubmitRequest {

0 commit comments

Comments
 (0)