Skip to content

Commit

Permalink
Merge pull request #3 from rahulkp220/feat/enum_for_sorting
Browse files Browse the repository at this point in the history
Use enum for sort
  • Loading branch information
rahulkp220 authored Dec 24, 2023
2 parents d6f627e + 2ac9170 commit d603eb2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ordiscan::{GetInscriptionInfoParams, GetListOfInscriptionParams, Ordiscan};
use ordiscan::{GetInscriptionInfoParams, GetListOfInscriptionParams, Ordiscan, Sort};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -22,8 +22,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let list_of_inscriptions = ordiclient
.get_list_of_inscriptions(GetListOfInscriptionParams {
address: Some("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh"),
sort: Sort::InscriptionNumberDesc,
content_type: None,
sort: None,
after_number: None,
before_number: None,
})
Expand Down
29 changes: 25 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Ordiscan {
pub struct GetListOfInscriptionParams<'a> {
pub address: Option<&'a str>,
pub content_type: Option<&'a str>,
pub sort: Option<&'a str>,
pub sort: Sort,
pub after_number: Option<usize>,
pub before_number: Option<usize>,
}
Expand All @@ -34,6 +34,21 @@ pub struct GetInscriptionInfoParams<'a> {
pub number: Option<usize>,
}

#[derive(Debug)]
pub enum Sort {
InscriptionNumberDesc,
InscriptionNumberAsc,
}

impl Sort {
pub fn as_str(&self) -> &'static str {
match self {
Sort::InscriptionNumberDesc => "inscription_number_desc", // default
Sort::InscriptionNumberAsc => "inscription_number_asc",
}
}
}

impl<'a> Ordiscan {
// create a new Ordiscan client
pub fn new(key: String) -> reqwest::Result<Self> {
Expand Down Expand Up @@ -86,9 +101,15 @@ impl<'a> Ordiscan {
params: GetListOfInscriptionParams<'a>,
) -> Result<Vec<schema::InscriptionInfo>> {
let header = format!("Bearer {}", self.api_key);
let sort = params.sort.unwrap_or("inscription_number_desc");
let mut url =
Url::parse(format!("{}/inscriptions?sort={}", API_BASE_URL, sort).as_str()).unwrap();
let mut url = Url::parse(
format!(
"{}/inscriptions?sort={}",
API_BASE_URL,
params.sort.as_str()
)
.as_str(),
)
.unwrap();

// TODO make this look better
// dynamically create query params
Expand Down

0 comments on commit d603eb2

Please sign in to comment.