1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::common::clients::RestClient;
use crate::common::data::Snapshot as Data;
use crate::common::load::SnapshotRandomizer;
use jortestkit::load::{Request, RequestFailure, RequestGenerator};
use std::time::Duration;

#[derive(Clone, Debug)]
pub struct VitRestRequestGenerator {
    rest_client: RestClient,
    snapshot_randomizer: SnapshotRandomizer,
}

impl VitRestRequestGenerator {
    pub fn new(snapshot: Data, mut rest_client: RestClient) -> Self {
        rest_client.disable_log();

        Self {
            snapshot_randomizer: SnapshotRandomizer::new(snapshot),
            rest_client,
        }
    }
}

impl RequestGenerator for VitRestRequestGenerator {
    fn next(&mut self) -> std::result::Result<Request, RequestFailure> {
        self.rest_client
            .set_api_token(self.snapshot_randomizer.random_token());

        match self.snapshot_randomizer.random_usize() % 3 {
            0 => self
                .rest_client
                .health()
                .map(|_| Request {
                    ids: vec![Option::None],
                    duration: Duration::ZERO,
                })
                .map_err(|e| RequestFailure::General(format!("Health: {}", e))),
            1 => self
                .rest_client
                .proposals()
                .map(|_| Request {
                    ids: vec![Option::None],
                    duration: Duration::ZERO,
                })
                .map_err(|e| RequestFailure::General(format!("Proposals: {}", e))),
            2 => self
                .rest_client
                .proposal(&self.snapshot_randomizer.random_proposal_id().to_string())
                .map(|_| Request {
                    ids: vec![Option::None],
                    duration: Duration::ZERO,
                })
                .map_err(|e| RequestFailure::General(format!("Proposals by id: {}", e))),
            3 => self
                .rest_client
                .fund(&self.snapshot_randomizer.random_fund_id().to_string())
                .map(|_| Request {
                    ids: vec![Option::None],
                    duration: Duration::ZERO,
                })
                .map_err(|e| RequestFailure::General(format!("Funds by id: {}", e))),
            _ => unreachable!(),
        }
    }

    fn split(self) -> (Self, Option<Self>) {
        todo!()
    }
}