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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! proxer api library
#[macro_use]
extern crate serde_derive;
extern crate chrono;
extern crate reqwest;
extern crate serde_json;
extern crate serde;
extern crate serde_urlencoded;
#[macro_use]
extern crate log;
#[macro_use]
extern crate hyper;

pub mod error;
pub mod response;
pub mod api;
pub mod prelude;
pub mod client;
pub mod tests;
pub mod parameter;

pub use client::Client;

use parameter::PageableParameter;
pub use prelude::*;
use serde::Serialize;
use serde::de::DeserializeOwned;

use serde_json::Value;
use std::fmt;



/// Every struct that is an endpoint, implements this trait.
pub trait Endpoint {
	type Parameter: Serialize + fmt::Debug + Clone;
	type ResponseType: std::fmt::Debug + Clone + DeserializeOwned;
	const URL: &'static str;


	fn new(Client, Self::Parameter) -> Self;

	fn params_mut(&mut self) -> &mut Self::Parameter;
	fn client(&self) -> Client;
	fn url(&self) -> String;
	fn parse(&self, Value) -> Result<Self::ResponseType, error::Error>;
}






pub trait PageableEndpoint<E>
where
	E: Endpoint + Clone + fmt::Debug,
	<E as Endpoint>::Parameter: PageableParameter + Clone + fmt::Debug,
	<E as Endpoint>::ResponseType: IntoIterator + Clone + std::fmt::Debug,
{
	fn pager(self, client: Client) -> Pager<E>;

	// fn pager(_self: E, client: Client) -> Pager<'de, E> {
	// 	Pager::new(
	// 		client,
	// 		_self.clone(),
	// 		None,
	// 		Some(1_000)
	// 	)
	// }
}



//#[derive(Debug, Clone)]
pub struct Pager<T>
where
	T: Endpoint + Clone + std::fmt::Debug,
	<T as Endpoint>::ResponseType: IntoIterator + Clone + std::fmt::Debug,
	<T as Endpoint>::Parameter: PageableParameter + Clone + std::fmt::Debug,
{
	client: Client,
	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,
	<T as Endpoint>::Parameter: PageableParameter + Clone + std::fmt::Debug,
{
	pub fn new(
		client: Client,
		mut endpoint: T,
		start: Option<usize>,
		limit: Option<usize>
	) -> Self
	{

		match (endpoint.params_mut().page_mut(), start) {
			(&mut None, None) => *endpoint.params_mut().page_mut() = Some(0),
			(&mut None, Some(_)) => *endpoint.params_mut().page_mut() = start,
			_ => {}
		}

		match (endpoint.params_mut().limit_mut(), limit) {
			(&mut None, None) => *endpoint.params_mut().limit_mut() = Some(750),
			(&mut None, Some(_)) => *endpoint.params_mut().limit_mut() = limit,
			_ => {}
		}

		debug!(
			"initialize new pager: page: {}, limit {}",
			endpoint.params_mut().page_mut().unwrap(),
			endpoint.params_mut().limit_mut().unwrap(),
		);


		Self {
			client: client,
			data: Vec::new(),
			shifted: 0,
			endpoint: endpoint,
		}
	}

	fn page_mut(&mut self) -> usize {
		self.endpoint.params_mut().page_mut().unwrap()
	}

	fn limit_mut(&mut self) -> usize {
		self.endpoint.params_mut().limit_mut().unwrap()
	}
}





impl<E> Iterator for Pager<E>
where
	E: Endpoint,
	E: Clone,
	E: std::fmt::Debug,
	<E as Endpoint>::Parameter: PageableParameter + Clone + 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) => {
				debug!("returning from buffer");
				self.shifted += 1;

				debug!("buffer: remaining/shifted: {}/{}",
					self.data.len(),
					self.shifted,
				);
				Some(Ok(i))
			}
			None => {
				debug!("expected shifted: {}", self.page_mut() * self.limit_mut());
				debug!("actually shifted: {}", self.shifted);

				if self.page_mut() * self.limit_mut() > self.shifted {
					debug!("reached the last page");
					return None;
				}



				debug!("fetching new data");
				let res = self.client.execute(self.endpoint.clone()).unwrap();



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

				debug!("filled buffer with {} entries", self.data.len());


				*self.endpoint.params_mut().page_mut() = Some(self.page_mut() + 1);

				self.next()

			}
		}
	}
}