From d6d59c012e01e3345901bbad7c4962007e139296 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 8 Mar 2022 11:08:50 +0100 Subject: [PATCH] Add Cargo.toml metadata and README Signed-off-by: trivernis --- Cargo.toml | 5 +++ README.md | 47 +++++++++++++++++++++++++++ src/endpoints/metadata.rs | 10 ++++++ src/hydrus_serializable/dictionary.rs | 1 + src/lib.rs | 4 ++- tests/endpoints.rs | 10 +++++- 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 README.md diff --git a/Cargo.toml b/Cargo.toml index 0620cc5..d1d124e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,12 @@ [package] name = "hydrus-ptr-client" +description = "A http client for the hydrus ptr" +authors = ["trivernis"] version = "0.1.0" edition = "2021" +license = "apache-2.0" +readme = "README.md" +repository = "https://github.com/Trivernis/hydrus-ptr-client" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..36a4f70 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# Hydrus PTR Client [![](https://img.shields.io/docsrs/hydrus-ptr-client)](https://docs.rs/hydrus-ptr-client) [![](https://img.shields.io/crates/v/hydrus-ptr-client)](https://crates.io/crates/hydrus-ptr-client) + +A rust http client for the hydrus PTR. Completeness is not guaranteed. + +## Usage + +## Fetching metadata and retrieving updates + +```rust +use hydrus_ptr_client::Client; + +#[tokio::main] +async fn main() { + let client = Client::builder().accept_invalid_certs(true).build().unwrap(); + // list of all update files since id = 0 + let metadata = client.get_metadata(0).await.unwrap(); + let first_update_file = metadata.update_hashes().swap_remove(0); + let update = client.get_update(first_update_file).await.unwrap(); + println!("Got update {:?}", update); +} +``` + +## Streaming updates + +```rust +use hydrus_ptr_client::Client; +use futures_util::StreamExt; + +#[tokio::main] +async fn main() { + let client = Client::builder().accept_invalid_certs(true).build().unwrap(); + // streams all update since id = 0 + let mut update_stream = client.stream_updates(0).await.unwrap(); + + while let Some(result) = update_stream.next().await { + match result { + Ok(update) => println!("We got an update {:?}", update), + Err(e) => println!("Oh no, an error occurred {}", e), + } + break; + } +} +``` + +## License + +Apache-2.0 \ No newline at end of file diff --git a/src/endpoints/metadata.rs b/src/endpoints/metadata.rs index a06ac41..76690cb 100644 --- a/src/endpoints/metadata.rs +++ b/src/endpoints/metadata.rs @@ -32,3 +32,13 @@ impl FromJson for MetadataResponse { Ok(MetadataResponse(metadata)) } } + +impl MetadataResponse { + pub fn update_hashes(&self) -> Vec<&String> { + self.0 + .entries + .iter() + .flat_map(|e| e.update_hashes.iter().collect::>()) + .collect() + } +} diff --git a/src/hydrus_serializable/dictionary.rs b/src/hydrus_serializable/dictionary.rs index 9581bd7..51eb3f1 100644 --- a/src/hydrus_serializable/dictionary.rs +++ b/src/hydrus_serializable/dictionary.rs @@ -1,3 +1,4 @@ +#![allow(unused)] use crate::constants::HYDRUS_TYPE_DICTIONARY; use crate::hydrus_serializable::HydrusSerializable; use crate::{Error, Result}; diff --git a/src/lib.rs b/src/lib.rs index d28159a..4456cf9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,11 @@ +#![doc=include_str!("../README.md")] + mod client; mod client_builder; pub(crate) mod constants; mod endpoints; mod error; -pub mod hydrus_serializable; +pub(crate) mod hydrus_serializable; pub use client::*; pub use client_builder::*; diff --git a/tests/endpoints.rs b/tests/endpoints.rs index 1c08003..d732ff6 100644 --- a/tests/endpoints.rs +++ b/tests/endpoints.rs @@ -11,7 +11,8 @@ async fn test_options() { #[tokio::test] async fn test_metadata() { let client = common::get_client(); - client.get_metadata(0).await.unwrap(); + let metadata = client.get_metadata(0).await.unwrap(); + assert!(metadata.update_hashes().len() > 0); } const DEFINITIONS_UPDATE_HASH: &str = @@ -32,8 +33,10 @@ async fn test_update_stream() { let client = common::get_client(); // 3230 let mut update_stream = client.stream_updates(0).await.unwrap(); let mut retry_count = 3; + let mut test_count = 25; while let Some(update) = update_stream.next().await { + test_count -= 1; if let Err(e) = update { if retry_count > 0 { retry_count -= 1; @@ -45,6 +48,11 @@ async fn test_update_stream() { e ) } + } else { + retry_count = 3; + if test_count <= 0 { + break; + } } } }