Skip to content
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- `transactions_pool_updates` now returns correct type `TxContentPoolCertsInner`
- `scripts_datum_hash_cbor` now uses correct url path `/scripts/datum/{hash}/cbor`
- `scripts_hash_json` now returns `ScriptJson`
- `scripts_hash_cbor` now returns `ScriptCbor`

### Changed

- `epochs_blocks` and `epochs_blocks_by_pool` now support pagination

## 1.1.0 - 2025-08-05

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ and create a new project to receive an API key.
Add to your project's `Cargo.toml`:

```toml
blockfrost = "1.1.0"
blockfrost = "1.2.0"
```

## Examples
Expand Down
4 changes: 2 additions & 2 deletions examples/all_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ async fn main() -> BlockfrostResult<()> {
let epochs_previous = api.epochs_previous(epoch, pagination).await;
let epochs_stakes = api.epochs_stakes(epoch, pagination).await;
let epochs_stakes_by_pool = api.epochs_stakes_by_pool(epoch, pool_id, pagination).await;
let epochs_blocks = api.epochs_blocks(epoch).await;
let epochs_blocks_by_pool = api.epochs_blocks_by_pool(epoch, pool_id).await;
let epochs_blocks = api.epochs_blocks(epoch, pagination).await;
let epochs_blocks_by_pool = api.epochs_blocks_by_pool(epoch, pool_id, pagination).await;
let epochs_parameters = api.epochs_parameters(epoch).await;

// Pools
Expand Down
15 changes: 10 additions & 5 deletions src/api/endpoints/epochs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ impl BlockfrostAPI {
.await
}

pub async fn epochs_blocks(&self, number: i32) -> BlockfrostResult<Vec<String>> {
self.call_endpoint(format!("/epochs/{number}/blocks").as_str())
pub async fn epochs_blocks(
&self, number: i32, pagination: Pagination,
) -> BlockfrostResult<Vec<String>> {
self.call_paged_endpoint(format!("/epochs/{number}/blocks").as_str(), pagination)
.await
}

pub async fn epochs_blocks_by_pool(
&self, number: i32, pool_id: &str,
&self, number: i32, pool_id: &str, pagination: Pagination,
) -> BlockfrostResult<Vec<String>> {
self.call_endpoint(format!("/epochs/{number}/blocks/{pool_id}").as_str())
.await
self.call_paged_endpoint(
format!("/epochs/{number}/blocks/{pool_id}").as_str(),
pagination,
)
.await
}
}

Expand Down
23 changes: 0 additions & 23 deletions src/api/endpoints/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,6 @@ mod tests {
serde_json::from_value::<Vec<TxMetadataLabelJsonInner>>(json_value).unwrap();
}

#[test]
fn test_test_metadata_txs_labels() {
let json_value = json!([
{
"label": "1990",
"cip10": null,
"count": "1"
},
{
"label": "1967",
"cip10": "nut.link metadata oracles registry",
"count": "3"
},
{
"label": "1968",
"cip10": "nut.link metadata oracles data points",
"count": "16321"
}
]);

serde_json::from_value::<Vec<TxMetadataLabelsInner>>(json_value).unwrap();
}

#[test]
fn test_metadata_txs_by_label_cbor() {
let json_value = json!([
Expand Down
9 changes: 5 additions & 4 deletions src/api/endpoints/scripts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::*;
use blockfrost_openapi::models::{
script::Script, script_redeemers_inner::ScriptRedeemersInner, scripts_inner::ScriptsInner,
script::Script, script_cbor::ScriptCbor, script_json::ScriptJson,
script_redeemers_inner::ScriptRedeemersInner, scripts_inner::ScriptsInner,
};

impl BlockfrostAPI {
Expand All @@ -13,12 +14,12 @@ impl BlockfrostAPI {
.await
}

pub async fn scripts_hash_json(&self, script_hash: &str) -> BlockfrostResult<ScriptsInner> {
pub async fn scripts_hash_json(&self, script_hash: &str) -> BlockfrostResult<ScriptJson> {
self.call_endpoint(format!("/scripts/{script_hash}/json").as_str())
.await
}

pub async fn scripts_hash_cbor(&self, script_hash: &str) -> BlockfrostResult<ScriptsInner> {
pub async fn scripts_hash_cbor(&self, script_hash: &str) -> BlockfrostResult<ScriptCbor> {
self.call_endpoint(format!("/scripts/{script_hash}/cbor").as_str())
.await
}
Expand All @@ -43,7 +44,7 @@ impl BlockfrostAPI {
pub async fn scripts_datum_hash_cbor(
&self, datum_hash: &str,
) -> BlockfrostResult<serde_json::Value> {
self.call_endpoint(format!("/scripts/{datum_hash}/cbor").as_str())
self.call_endpoint(format!("/scripts/datum/{datum_hash}/cbor").as_str())
.await
}
}
Expand Down
135 changes: 115 additions & 20 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,33 @@ where
pub(crate) async fn send_request_unprocessed(
request: RequestBuilder, retry_settings: RetrySettings,
) -> reqwest::Result<Response> {
let retry_codes = [
StatusCode::REQUEST_TIMEOUT,
StatusCode::PAYLOAD_TOO_LARGE,
StatusCode::TOO_MANY_REQUESTS,
StatusCode::INTERNAL_SERVER_ERROR,
StatusCode::BAD_GATEWAY,
StatusCode::SERVICE_UNAVAILABLE,
StatusCode::GATEWAY_TIMEOUT,
];

// Try all attempts except the last one (cloning the request each time)
for _ in 1..retry_settings.amount {
let request = clone_request(&request);
let response = request.send().await;
let retry_codes = [
StatusCode::REQUEST_TIMEOUT,
StatusCode::PAYLOAD_TOO_LARGE,
StatusCode::TOO_MANY_REQUESTS,
StatusCode::INTERNAL_SERVER_ERROR,
StatusCode::BAD_GATEWAY,
StatusCode::SERVICE_UNAVAILABLE,
StatusCode::GATEWAY_TIMEOUT,
];

if let Err(err) = &response {
if let Some(status) = err.status() {
if retry_codes.contains(&status) {
Delay::new(retry_settings.delay).await;
continue;
}
}
let response = clone_request(&request).send().await;

return response;
let should_retry = match &response {
Ok(resp) => retry_codes.contains(&resp.status()),
Err(err) => err.status().is_some_and(|s| retry_codes.contains(&s)),
};

if should_retry {
Delay::new(retry_settings.delay).await;
} else {
return response;
}
}

// final attempt
request.send().await
}

Expand Down Expand Up @@ -215,4 +215,99 @@ mod tests {

assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9], result);
}

#[tokio::test]
async fn test_success_returns_immediately() {
let server = MockServer::start();
let client = Client::new();
let retry_settings = RetrySettings {
amount: 3,
delay: std::time::Duration::from_millis(10),
};

let mock = server.mock(|when, then| {
when.method(GET).path("/test");
then.status(200).body("ok");
});

let request = client.get(server.url("/test"));
let response = send_request_unprocessed(request, retry_settings)
.await
.unwrap();

// 200 should hit only once - no retry for success
assert_eq!(response.status(), 200);
assert_eq!(mock.calls(), 1);
}

#[tokio::test]
async fn test_no_retry_on_client_error() {
let server = MockServer::start();
let client = Client::new();
let retry_settings = RetrySettings {
amount: 3,
delay: std::time::Duration::from_millis(10),
};

let mock = server.mock(|when, then| {
when.method(GET).path("/test");
then.status(404).body("not found");
});

let request = client.get(server.url("/test"));
let response = send_request_unprocessed(request, retry_settings)
.await
.unwrap();

// 404 should hit only once - no retry for success
assert_eq!(response.status(), 404);
assert_eq!(mock.calls(), 1);
}

#[tokio::test]
async fn test_retry_on_server_error() {
let server = MockServer::start();
let client = Client::new();
let retry_settings = RetrySettings {
amount: 3,
delay: std::time::Duration::from_millis(10),
};

let mock = server.mock(|when, then| {
when.method(GET).path("/test");
then.status(503);
});

let request = client.get(server.url("/test"));
let response = send_request_unprocessed(request, retry_settings)
.await
.unwrap();

// 503 is retryable, should hit exactly `amount` times
assert_eq!(response.status(), 503);
assert_eq!(mock.calls(), 3);
}

#[tokio::test]
async fn test_single_attempt_no_retry() {
let server = MockServer::start();
let client = Client::new();
let retry_settings = RetrySettings {
amount: 1,
delay: std::time::Duration::from_millis(10),
};

let mock = server.mock(|when, then| {
when.method(GET).path("/test");
then.status(503);
});

let request = client.get(server.url("/test"));
let response = send_request_unprocessed(request, retry_settings)
.await
.unwrap();

assert_eq!(response.status(), 503);
assert_eq!(mock.calls(), 1);
}
}
Loading