Skip to content

Commit

Permalink
refactor: pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvolek committed Dec 12, 2023
1 parent 1f05286 commit d16b701
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/api/endpoints/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{BlockfrostAPI, BlockfrostResult, Pagination};
use crate::{BlockfrostAPI, BlockfrostResult, pagination::Pagination};
use blockfrost_openapi::models::{
account_addresses_assets_inner::AccountAddressesAssetsInner,
account_addresses_content_inner::AccountAddressesContentInner,
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use blockfrost_openapi::models::{
address_utxo_content_inner::AddressUtxoContentInner,
};

use crate::*;
use crate::{pagination::Pagination, *};

impl BlockfrostAPI {
pub async fn addresses(&self, address: &str) -> BlockfrostResult<AddressContent> {
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/assets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
asset_addresses_inner::AssetAddressesInner, asset_history_inner::AssetHistoryInner,
asset_policy_inner::AssetPolicyInner, asset_transactions_inner::AssetTransactionsInner,
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
block_content::BlockContent, block_content_addresses_inner::BlockContentAddressesInner,
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/epochs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
epoch_content::EpochContent, epoch_param_content::EpochParamContent,
epoch_stake_content_inner::EpochStakeContentInner,
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
mempool_content_inner::MempoolContentInner, mempool_tx_content::MempoolTxContent,
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
tx_metadata_label_cbor_inner::TxMetadataLabelCborInner,
tx_metadata_label_json_inner::TxMetadataLabelJsonInner,
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
metrics_endpoints_inner::MetricsEndpointsInner, metrics_inner::MetricsInner,
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/nutlink.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
nutlink_address::NutlinkAddress, nutlink_address_ticker_inner::NutlinkAddressTickerInner,
nutlink_address_ticker_inner_payload::NutlinkAddressTickerInnerPayload,
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/pools.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
pool::Pool, pool_delegators_inner::PoolDelegatorsInner, pool_history_inner::PoolHistoryInner,
pool_list_retire_inner::PoolListRetireInner, pool_metadata::PoolMetadata,
Expand Down
2 changes: 1 addition & 1 deletion src/api/endpoints/scripts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::*;
use crate::{pagination::Pagination, *};
use blockfrost_openapi::models::{
script::Script, script_redeemers_inner::ScriptRedeemersInner, scripts_inner::ScriptsInner,
};
Expand Down
3 changes: 2 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
pub(super) mod endpoints;
use crate::{
pagination::Pagination,
request::{fetch_all_pages, send_get_request},
url::Url,
utils::build_header_map,
utils::create_client_with_project_id,
BlockFrostSettings, BlockfrostError, Pagination,
BlockFrostSettings, BlockfrostError,
};
use reqwest::ClientBuilder;

Expand Down
1 change: 1 addition & 0 deletions src/ipfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl BlockfrostIPFS {
/// [`HeaderValue::from_str`]: reqwest::header::HeaderValue::from_str
pub fn new(project_id: &str, settings: IpfsSettings) -> Self {
let client = create_client_with_project_id(project_id);

Self {
client,
settings,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![doc = include_str!("../README.md")]
mod api;
mod ipfs;
mod pagination;
mod request;
mod settings;
mod url;
Expand All @@ -14,6 +15,7 @@ pub mod types;
pub use api::*;
pub use error::*;
pub use ipfs::BlockfrostIPFS;
pub use pagination::Pagination;
pub use settings::*;
pub use types::*;

Expand Down
49 changes: 49 additions & 0 deletions src/pagination.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#[derive(Clone, Copy)]
pub struct Pagination {
pub fetch_all: bool,
pub count: usize,
pub page: usize,
pub order: Order,
}

impl Default for Pagination {
fn default() -> Self {
Pagination {
fetch_all: false,
count: 100,
page: 1,
order: Order::Asc,
}
}
}

impl Pagination {
pub fn new(order: Order, page: usize, count: usize) -> Self {
Pagination {
fetch_all: false,
order,
page,
count,
}
}

pub fn all() -> Self {
Pagination {
fetch_all: true,
..Default::default()
}
}

pub fn order_to_string(&self) -> String {
match self.order {
Order::Asc => "asc".to_string(),
Order::Desc => "desc".to_string(),
}
}
}

#[derive(Clone, Copy)]
pub enum Order {
Asc,
Desc,
}
4 changes: 2 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
json_error, process_error_response, reqwest_error, url::Url, BlockfrostError, Pagination,
RetrySettings,
json_error, pagination::Pagination, process_error_response, reqwest_error, url::Url,
BlockfrostError, RetrySettings,
};
use futures::future;
use reqwest::{Client, RequestBuilder, Response, StatusCode};
Expand Down
90 changes: 0 additions & 90 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//! Definitions for common types returned in requests.

use serde::{Deserialize, Serialize};

// Use this module as an interface to export all types declared inside of endpoints/
//
// These are not used in here, just exporting
Expand All @@ -10,33 +6,6 @@ pub use crate::{
ipfs::{IpfsAdd, IpfsPinList, IpfsPinState, IpfsPinUpdate},
};

/// Inner member of [`Address`], [`AddressTotal`], [`AddressUtxo`] and [`Transaction`].
///
/// # Format:
///
/// The `unit` String can be "lovelace" or other, in the latter case, the String will be made of
/// a concatenation of the asset `policy_id` and hex-encoded `asset_name`.
///
/// # Example:
///
/// ```
/// # use blockfrost::Amount;
/// let unit = "lovelace".to_string();
/// let quantity = "700".to_string();
///
/// // Amount: 700 lovelaces
/// let amount = Amount { unit, quantity };
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Amount {
/// The unit of the value.
///
/// Format: "Lovelace" or concatenation of asset `policy_id` and hex-encoded `asset_name`.
pub unit: String,
/// The quantity of the unit.
pub quantity: String,
}

/// Enum for any possible JSON value.
///
/// Declared as the following:
Expand Down Expand Up @@ -71,62 +40,3 @@ pub type Float = f64;
/// [`EpochParameters`]
/// [`AssetDetails`]
pub type JsonMap = serde_json::Map<String, JsonValue>;

/// Inner enum for [`PoolUpdate`] and [`AccountRegistration`].
///
/// Action in the certificate.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum ActionType {
Registered,
Deregistered,
}
#[derive(Clone, Copy)]
pub enum Order {
Asc,
Desc,
}

#[derive(Clone, Copy)]
pub struct Pagination {
pub fetch_all: bool,
pub count: usize,
pub page: usize,
pub order: Order,
}

impl Default for Pagination {
fn default() -> Self {
Pagination {
fetch_all: false,
count: 100,
page: 1,
order: Order::Asc,
}
}
}

impl Pagination {
pub fn new(order: Order, page: usize, count: usize) -> Self {
Pagination {
fetch_all: false,
order,
page,
count,
}
}

pub fn all() -> Self {
Pagination {
fetch_all: true,
..Default::default()
}
}

pub fn order_to_string(&self) -> String {
match self.order {
Order::Asc => "asc".to_string(),
Order::Desc => "desc".to_string(),
}
}
}
4 changes: 3 additions & 1 deletion src/url.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{Pagination, CARDANO_MAINNET_URL, CARDANO_PREPROD_URL, CARDANO_PREVIEW_URL};
use crate::{
pagination::Pagination, CARDANO_MAINNET_URL, CARDANO_PREPROD_URL, CARDANO_PREVIEW_URL,
};
use std::error::Error;
use url::{form_urlencoded, Url as UrlI};

Expand Down

0 comments on commit d16b701

Please sign in to comment.