Skip to content

Commit 3c31ca1

Browse files
committed
chore: add helpers to make sortition requests and decode them
1 parent e204f11 commit 3c31ca1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

stackslib/src/net/api/getsortition.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,34 @@ impl HttpResponse for GetSortitionHandler {
377377
Ok(HttpResponsePayload::try_from_json(sortition_info)?)
378378
}
379379
}
380+
381+
impl StacksHttpRequest {
382+
/// Make a new getinfo request to this endpoint
383+
pub fn new_get_sortition(
384+
host: PeerHost,
385+
sort_key: &str,
386+
sort_value: &str,
387+
) -> StacksHttpRequest {
388+
StacksHttpRequest::new_for_peer(
389+
host,
390+
"GET".into(),
391+
format!("/v3/sortitions/{}/{}", sort_key, sort_value),
392+
HttpRequestContents::new(),
393+
)
394+
.expect("FATAL: failed to construct request from infallible data")
395+
}
396+
397+
pub fn new_get_sortition_consensus(host: PeerHost, ch: &ConsensusHash) -> StacksHttpRequest {
398+
Self::new_get_sortition(host, "consensus", &format!("{}", ch))
399+
}
400+
}
401+
402+
impl StacksHttpResponse {
403+
pub fn decode_sortition_info(self) -> Result<Vec<SortitionInfo>, NetError> {
404+
let contents = self.get_http_payload_ok()?;
405+
let response_json: serde_json::Value = contents.try_into()?;
406+
let response: Vec<SortitionInfo> = serde_json::from_value(response_json)
407+
.map_err(|_e| Error::DecodeError(format!("Failed to decode JSON: {:?}", &_e)))?;
408+
Ok(response)
409+
}
410+
}

0 commit comments

Comments
 (0)