kproc 0.7.0

Knowledge Processing library.
Documentation
//! Processor to retrieve data from HTTP servers

use isahc::AsyncReadResponseExt;
use kproc_pmacros::Processor;

use crate::processor::{InputStreams, OutputStreams};
use crate::{processor, Result};

/// Input for the HTTP client
#[derive(Clone, InputStreams)]
pub struct Inputs
{
  /// URL to retrieve
  pub url: String,
}

/// Output for the HTTP client
#[derive(Clone, OutputStreams)]
pub struct Outputs
{
  /// Source URL
  pub url: String,
  /// Page data
  pub data: String,
  /// Status code
  pub status: u16,
}

/// Processor for the HTTP client
#[derive(Processor)]
#[streams(Inputs, Outputs)]
pub struct Http {}

impl processor::ValueProcessor for Http
{
  async fn process_one(&self, inputs: Self::Inputs) -> Result<Self::Outputs>
  {
    let mut response = isahc::get_async(inputs.url.clone()).await?;

    Ok(Outputs {
      url: inputs.url,
      data: response.text().await?,
      status: response.status().as_u16(),
    })
  }
}