mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Fix http api tests ci (#7943)
Co-Authored-By: Jimmy Chen <jchen.tc@gmail.com> Co-Authored-By: Michael Sproul <micsproul@gmail.com> Co-Authored-By: Michael Sproul <michael@sigmaprime.io> Co-Authored-By: hopinheimer <knmanas6@gmail.com>
This commit is contained in:
@@ -498,7 +498,7 @@ impl BeaconNodeHttpClient {
|
||||
.post(url)
|
||||
.timeout(timeout.unwrap_or(self.timeouts.default));
|
||||
let response = builder.json(body).send().await?;
|
||||
ok_or_error(response).await
|
||||
success_or_error(response).await
|
||||
}
|
||||
|
||||
/// Generic POST function supporting arbitrary responses and timeouts.
|
||||
@@ -518,7 +518,7 @@ impl BeaconNodeHttpClient {
|
||||
.json(body)
|
||||
.send()
|
||||
.await?;
|
||||
ok_or_error(response).await
|
||||
success_or_error(response).await
|
||||
}
|
||||
|
||||
/// Generic POST function that includes octet-stream content type header.
|
||||
@@ -535,7 +535,7 @@ impl BeaconNodeHttpClient {
|
||||
HeaderValue::from_static("application/octet-stream"),
|
||||
);
|
||||
let response = builder.headers(headers).json(body).send().await?;
|
||||
ok_or_error(response).await
|
||||
success_or_error(response).await
|
||||
}
|
||||
|
||||
/// Generic POST function supporting arbitrary responses and timeouts.
|
||||
@@ -560,7 +560,7 @@ impl BeaconNodeHttpClient {
|
||||
HeaderValue::from_static("application/octet-stream"),
|
||||
);
|
||||
let response = builder.headers(headers).body(body).send().await?;
|
||||
ok_or_error(response).await
|
||||
success_or_error(response).await
|
||||
}
|
||||
|
||||
/// `GET beacon/genesis`
|
||||
@@ -1257,16 +1257,17 @@ impl BeaconNodeHttpClient {
|
||||
&self,
|
||||
block_contents: &PublishBlockRequest<E>,
|
||||
validation_level: Option<BroadcastValidation>,
|
||||
) -> Result<(), Error> {
|
||||
self.post_generic_with_consensus_version(
|
||||
self.post_beacon_blocks_v2_path(validation_level)?,
|
||||
block_contents,
|
||||
Some(self.timeouts.proposal),
|
||||
block_contents.signed_block().message().body().fork_name(),
|
||||
)
|
||||
.await?;
|
||||
) -> Result<Response, Error> {
|
||||
let response = self
|
||||
.post_generic_with_consensus_version(
|
||||
self.post_beacon_blocks_v2_path(validation_level)?,
|
||||
block_contents,
|
||||
Some(self.timeouts.proposal),
|
||||
block_contents.signed_block().message().body().fork_name(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// `POST v2/beacon/blocks`
|
||||
@@ -1274,16 +1275,17 @@ impl BeaconNodeHttpClient {
|
||||
&self,
|
||||
block_contents: &PublishBlockRequest<E>,
|
||||
validation_level: Option<BroadcastValidation>,
|
||||
) -> Result<(), Error> {
|
||||
self.post_generic_with_consensus_version_and_ssz_body(
|
||||
self.post_beacon_blocks_v2_path(validation_level)?,
|
||||
block_contents.as_ssz_bytes(),
|
||||
Some(self.timeouts.proposal),
|
||||
block_contents.signed_block().message().body().fork_name(),
|
||||
)
|
||||
.await?;
|
||||
) -> Result<Response, Error> {
|
||||
let response = self
|
||||
.post_generic_with_consensus_version_and_ssz_body(
|
||||
self.post_beacon_blocks_v2_path(validation_level)?,
|
||||
block_contents.as_ssz_bytes(),
|
||||
Some(self.timeouts.proposal),
|
||||
block_contents.signed_block().message().body().fork_name(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// `POST v2/beacon/blinded_blocks`
|
||||
@@ -1291,16 +1293,17 @@ impl BeaconNodeHttpClient {
|
||||
&self,
|
||||
signed_block: &SignedBlindedBeaconBlock<E>,
|
||||
validation_level: Option<BroadcastValidation>,
|
||||
) -> Result<(), Error> {
|
||||
self.post_generic_with_consensus_version(
|
||||
self.post_beacon_blinded_blocks_v2_path(validation_level)?,
|
||||
signed_block,
|
||||
Some(self.timeouts.proposal),
|
||||
signed_block.message().body().fork_name(),
|
||||
)
|
||||
.await?;
|
||||
) -> Result<Response, Error> {
|
||||
let response = self
|
||||
.post_generic_with_consensus_version(
|
||||
self.post_beacon_blinded_blocks_v2_path(validation_level)?,
|
||||
signed_block,
|
||||
Some(self.timeouts.proposal),
|
||||
signed_block.message().body().fork_name(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// `POST v2/beacon/blinded_blocks`
|
||||
@@ -1308,16 +1311,17 @@ impl BeaconNodeHttpClient {
|
||||
&self,
|
||||
signed_block: &SignedBlindedBeaconBlock<E>,
|
||||
validation_level: Option<BroadcastValidation>,
|
||||
) -> Result<(), Error> {
|
||||
self.post_generic_with_consensus_version_and_ssz_body(
|
||||
self.post_beacon_blinded_blocks_v2_path(validation_level)?,
|
||||
signed_block.as_ssz_bytes(),
|
||||
Some(self.timeouts.proposal),
|
||||
signed_block.message().body().fork_name(),
|
||||
)
|
||||
.await?;
|
||||
) -> Result<Response, Error> {
|
||||
let response = self
|
||||
.post_generic_with_consensus_version_and_ssz_body(
|
||||
self.post_beacon_blinded_blocks_v2_path(validation_level)?,
|
||||
signed_block.as_ssz_bytes(),
|
||||
Some(self.timeouts.proposal),
|
||||
signed_block.message().body().fork_name(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// Path for `v2/beacon/blocks`
|
||||
@@ -2903,3 +2907,20 @@ pub async fn ok_or_error(response: Response) -> Result<Response, Error> {
|
||||
Err(Error::StatusCode(status))
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `Ok(response)` if the response is a success (2xx) response. Otherwise, creates an
|
||||
/// appropriate error message.
|
||||
pub async fn success_or_error(response: Response) -> Result<Response, Error> {
|
||||
let status = response.status();
|
||||
|
||||
if status.is_success() {
|
||||
Ok(response)
|
||||
} else if let Ok(message) = response.json().await {
|
||||
match message {
|
||||
ResponseError::Message(message) => Err(Error::ServerMessage(message)),
|
||||
ResponseError::Indexed(indexed) => Err(Error::ServerIndexedMessage(indexed)),
|
||||
}
|
||||
} else {
|
||||
Err(Error::StatusCode(status))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user