|
| 1 | +use std::{convert::Infallible, future::ready}; |
| 2 | + |
| 3 | +use http::Request; |
| 4 | +use http_body_util::combinators::BoxBody; |
| 5 | +use hyper::{ |
| 6 | + Response, |
| 7 | + body::{Bytes, Incoming}, |
| 8 | +}; |
| 9 | +use prosa::core::adaptor::Adaptor; |
| 10 | + |
| 11 | +use crate::proc::{FetchAction, FetcherError, FetcherProc}; |
| 12 | + |
| 13 | +/// Trait adaptor for the Fetcher processor. |
| 14 | +pub trait FetcherAdaptor<M>: Adaptor |
| 15 | +where |
| 16 | + M: 'static |
| 17 | + + std::marker::Send |
| 18 | + + std::marker::Sync |
| 19 | + + std::marker::Sized |
| 20 | + + std::clone::Clone |
| 21 | + + std::fmt::Debug |
| 22 | + + prosa_utils::msg::tvf::Tvf |
| 23 | + + std::default::Default, |
| 24 | +{ |
| 25 | + /// Method called when the processor spawns |
| 26 | + /// This method is called only once so the processing will be thread safe |
| 27 | + fn new(proc: &FetcherProc<M>) -> Result<Self, FetcherError<M>> |
| 28 | + where |
| 29 | + Self: std::marker::Sized; |
| 30 | + |
| 31 | + /// Method that indicate what should be done to fetch informations from the remote system |
| 32 | + fn fetch(&mut self) -> Result<FetchAction<M>, FetcherError<M>>; |
| 33 | + |
| 34 | + /// Create an HTTP request to fetch information |
| 35 | + fn create_http_request( |
| 36 | + &self, |
| 37 | + request_builder: http::request::Builder, |
| 38 | + ) -> Result<Request<BoxBody<Bytes, Infallible>>, FetcherError<M>>; |
| 39 | + |
| 40 | + /// Process http response |
| 41 | + fn process_http_response( |
| 42 | + &mut self, |
| 43 | + _response: Response<Incoming>, |
| 44 | + ) -> impl std::future::Future<Output = Result<FetchAction<M>, FetcherError<M>>> + Send { |
| 45 | + ready(Ok(FetchAction::None)) |
| 46 | + } |
| 47 | + |
| 48 | + /// Process service response |
| 49 | + fn process_service_response( |
| 50 | + &mut self, |
| 51 | + _response: prosa::core::msg::ResponseMsg<M>, |
| 52 | + ) -> Result<FetchAction<M>, FetcherError<M>> { |
| 53 | + Ok(FetchAction::None) |
| 54 | + } |
| 55 | + |
| 56 | + /// Method to process incomings error received by the processor |
| 57 | + fn process_service_error( |
| 58 | + &self, |
| 59 | + _error: prosa::core::msg::ErrorMsg<M>, |
| 60 | + ) -> Result<FetchAction<M>, FetcherError<M>> { |
| 61 | + Ok(FetchAction::None) |
| 62 | + } |
| 63 | +} |
0 commit comments