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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! proxer api library


// #![warn(missing_docs)]



#[macro_use]
extern crate serde_derive;

extern crate chrono;
extern crate colored;
extern crate reqwest;
extern crate serde_json;
extern crate serde;

pub mod error;
pub mod response;
pub mod request;
pub mod api;
pub mod prelude;
pub mod client;

pub use client::Client;
use std::collections::HashMap;
use colored::Colorize;



/// Every struct that is an endpoint, implements this trait.
pub trait Endpoint {
	type ResponseType: std::fmt::Debug + Clone;
	fn get_params_mut(&mut self) -> &mut HashMap<String, String>;
	fn send(self) -> Result<Self::ResponseType, error::Error>;


}














pub trait Pageable<E: Endpoint + Clone + std::fmt::Debug>
where
	<E as Endpoint>::ResponseType: IntoIterator + Clone + std::fmt::Debug,
{
	fn pager(self) -> Pager<E>;
}



//#[derive(Debug, Clone)]
pub struct Pager<T: Endpoint + Clone + std::fmt::Debug>
where
	<T as Endpoint>::ResponseType: IntoIterator + Clone + std::fmt::Debug,
{
	limit: usize,
	current_page: usize,
	shifted: usize,
	endpoint: T,
	data: Vec<<<T as Endpoint>::ResponseType as IntoIterator>::Item>,
}




impl<T: Endpoint + Clone + std::fmt::Debug> Pager<T>
where
	<T as Endpoint>::ResponseType: IntoIterator + Clone + std::fmt::Debug,
{
	pub fn new(mut endpoint: T, start: usize, limit: usize) -> Self
	{
		endpoint
			.get_params_mut()
			.insert("limit".to_string(), limit.to_string());

		endpoint
			.get_params_mut()
			.insert("p".to_string(), start.to_string());


		Self {
			data: Vec::new(),
			shifted: limit,
			limit: limit,
			endpoint: endpoint,
			current_page: start,
		}
	}
}





impl<E> Iterator for Pager<E>
where
	E: Endpoint,
	E: Clone,
	E: std::fmt::Debug,
	<E as Endpoint>::ResponseType: IntoIterator,
{
	type Item = Result<<<E as Endpoint>::ResponseType as IntoIterator>::Item, error::Error>;

	fn next(&mut self) -> Option<Self::Item>
	{
		match self.data.pop()
		{
			Some(i) => {
				std::thread::sleep(std::time::Duration::new(0, 000_500_000));
				println!("Iter: returning from buffer");
				self.shifted += 1;
				Some(Ok(i))
			},
			None => {
				//if false {
				if self.shifted < self.limit {
					println!("Iter: last fetch was smaller than limit");
					return None;
				}
				else {
					println!("Iter: fetching new data");
					self.endpoint
						.get_params_mut()
						.insert("p".to_string(), self.current_page.to_string());

					self.current_page += 1;

					println!("{}", format!("{:#?}", self.endpoint).cyan().bold());
					let res = self.endpoint.clone().send().unwrap();


					for var in res.into_iter() {
						self.data.push(var);
					}
					self.shifted = 0;

					std::thread::sleep(std::time::Duration::new(1, 5_000_000));
					self.next()
				}
			}
		}
	}
}