psn_api_rs 0.3.0

A simple PSN Network API wrapper
Documentation
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! # A simple PSN API wrapper.
//! It uses an async http client(hyper::Client in this case) to communicate wih the official PSN API.
//!
//! Some basics:
//! The crate use `npsso` code to login in to PSN Network and get a pair of `access_token` and `refresh_token` in response.
//! [How to obtain uuid and two_step tokens](https://tusticles.com/psn-php/first_login.html)
//! The `access_token` last about an hour before expire and it's needed to call most other PSN APIs(The PSN store API doesn't need any token to access though).
//! The `refresh_token` last much longer and it's used to generate a new access_token after/before it is expired.
//!
//! * Note:
//! There is a rate limiter for the official PSN API so better not make lots of calls in short time.
//! The proxy example (The best practice to use this libaray) shows how to make high concurrency possible and combat the rate limiter effectivly.
//!
//! # Basic Example:
//!```no_run
//!use psn_api_rs::{psn::PSN, types::PSNInner, traits::PSNRequest, models::PSNUser};
//!
//!#[tokio::main]
//!async fn main() -> std::io::Result<()> {
//!    let refresh_token = String::from("your refresh token");
//!    let npsso = String::from("your npsso");
//!
//!    let client = PSN::new_client().expect("Failed to build http client");
//!
//!    // construct a PSNInner object,add credentials and call auth to generate tokens.
//!    let mut psn_inner = PSNInner::new();
//!    psn_inner.set_region("us".to_owned()) // <- set to a psn region server suit your case. you can leave it as default which is hk
//!            .set_lang("en".to_owned()) // <- set to a language you want the response to be. default is en
//!            .set_self_online_id(String::from("Your Login account PSN online_id")) // <- this is used to generate new message thread. safe to leave unset if you don't need to send any PSN message.
//!            .add_refresh_token(refresh_token) // <- If refresh_token is provided then it's safe to ignore add_npsso and call .auth() directly.
//!            .add_npsso(npsso); // <- npsso is used only when refresh_token is not working or not provided.
//!
//!    psn_inner = psn_inner
//!            .auth()
//!            .await
//!            .unwrap_or_else(|e| panic!("{:?}", e));
//!
//!    println!(
//!        "Authentication Success! These are your info from PSN network: \r\n{:#?} ",
//!        psn_inner
//!    );
//!
//!    let user = psn_inner
//!            .get_profile::<PSNUser>(&client, "Hakoom")
//!            .await
//!            .unwrap_or_else(|e| panic!("{:?}", e));
//!
//!    println!(
//!        "Example finished. Got user info : \r\n{:#?}",
//!        user
//!    );
//!
//!    Ok(())
//!    // psn struct is dropped at this point so it's better to store your access_token and refresh_token here to make them reusable.
//!}
//!```

#[macro_use]
extern crate serde_derive;

pub mod metas;
pub mod models;
pub mod traits;
pub mod types;

mod private_model;

#[cfg(feature = "default")]
pub mod psn {
    use crossbeam_queue::SegQueue;
    use derive_more::Display;
    use reqwest::{Client, ClientBuilder, Error, Proxy};
    use serde::de::DeserializeOwned;
    use tang_rs::{Builder, Manager, ManagerFuture, Pool, PoolRef};

    use crate::traits::PSNRequest;
    use crate::types::PSNInner;

    #[derive(Debug, Clone)]
    pub struct PSN {
        inner: Pool<PSNInnerManager>,
        client: Client,
        proxy_pool: Option<Pool<ProxyPoolManager>>,
    }

    /// You can override `PSNRequest` trait to impl your own error type.
    #[derive(Debug, Display)]
    pub enum PSNError {
        #[display(fmt = "No http client is available and/or new client can't be made.")]
        NoClient,
        #[display(fmt = "No psn object is available")]
        NoPSNInner,
        #[display(fmt = "Failed to login in to PSN")]
        AuthenticationFail,
        #[display(fmt = "Request is timeout")]
        TimeOut,
        #[display(fmt = "Error from Reqwest: {}", _0)]
        FromReqwest(Error),
        #[display(fmt = "Error from PSN response: {}", _0)]
        FromPSN(String),
        #[display(fmt = "Error from Local: {}", _0)]
        FromStd(std::io::Error),
    }

    pub struct PSNInnerManager {
        inner: SegQueue<PSNInner>,
        client: Client,
    }

    impl PSNInnerManager {
        fn new() -> Self {
            PSNInnerManager {
                inner: SegQueue::new(),
                client: ClientBuilder::new()
                    .build()
                    .expect("Failed to build http client for PSNInnerManager"),
            }
        }

        fn add_psn_inner(&self, psn: PSNInner) {
            self.inner.push(psn);
        }
    }

    impl Manager for PSNInnerManager {
        type Connection = PSNInner;
        type Error = PSNError;

        fn connect(&self) -> ManagerFuture<'_, Result<Self::Connection, Self::Error>> {
            Box::pin(async move { self.inner.pop().map_err(|_| PSNError::NoClient) })
        }

        fn is_valid<'a>(
            &'a self,
            conn: &'a mut Self::Connection,
        ) -> ManagerFuture<'a, Result<(), Self::Error>> {
            Box::pin(async move {
                if conn.should_refresh() {
                    conn.gen_access_from_refresh(&self.client).await
                } else {
                    Ok(())
                }
            })
        }

        fn is_closed(&self, _conn: &mut Self::Connection) -> bool {
            false
        }
    }

    pub struct ProxyPoolManager {
        proxies: SegQueue<(String, Option<String>, Option<String>)>,
        marker: &'static str,
    }

    impl ProxyPoolManager {
        fn new() -> Self {
            ProxyPoolManager {
                proxies: SegQueue::new(),
                marker: "www.google.com",
            }
        }

        fn add_proxy(&self, address: &str, username: Option<&str>, password: Option<&str>) {
            self.proxies.push((
                address.into(),
                username.map(Into::into),
                password.map(Into::into),
            ));
        }
    }

    impl Manager for ProxyPoolManager {
        type Connection = Client;
        type Error = PSNError;

        fn connect(&self) -> ManagerFuture<'_, Result<Self::Connection, Self::Error>> {
            Box::pin(async move {
                let (address, username, password) =
                    self.proxies.pop().map_err(|_| PSNError::NoClient)?;
                let proxy = match username {
                    Some(username) => Proxy::all(&address)
                        .map(|p| p.basic_auth(&username, password.as_deref().unwrap_or(""))),
                    None => Proxy::all(&address),
                };

                Client::builder()
                    .proxy(proxy.map_err(|_| PSNError::NoClient)?)
                    .build()
                    .map_err(|_| PSNError::NoClient)
            })
        }

        fn is_valid<'a>(
            &'a self,
            conn: &'a mut Self::Connection,
        ) -> ManagerFuture<'a, Result<(), Self::Error>> {
            Box::pin(async move {
                let _ = conn.get(self.marker).send().await?;
                Ok(())
            })
        }

        fn is_closed(&self, _conn: &mut Self::Connection) -> bool {
            false
        }
    }

    impl From<Error> for PSNError {
        fn from(e: Error) -> Self {
            PSNError::FromReqwest(e)
        }
    }

    impl From<tokio::time::Elapsed> for PSNError {
        fn from(_: tokio::time::Elapsed) -> Self {
            PSNError::TimeOut
        }
    }

    impl PSN {
        /// A shortcut for building a temporary http client
        pub fn new_client() -> Result<Client, PSNError> {
            ClientBuilder::new().build().map_err(|_| PSNError::NoClient)
        }

        /// Accept multiple PSNInner and  use them concurrently with a pool.
        pub async fn new(psn_inner: Vec<PSNInner>) -> Self {
            let mgr = PSNInnerManager::new();

            let size = psn_inner.len() as u8;

            for inner in psn_inner.into_iter() {
                mgr.add_psn_inner(inner);
            }

            let inner_pool = Builder::new()
                .always_check(true)
                .idle_timeout(None)
                .max_lifetime(None)
                .min_idle(size)
                .max_size(size)
                .build(mgr)
                .await
                .expect("Failed to build PSNInner pool");

            PSN {
                inner: inner_pool,
                client: Self::new_client().expect("Failed to build http client"),
                proxy_pool: None,
            }
        }

        /// Add http proxy pool to combat PSN rate limiter. This is not required.
        ///# Example:
        ///```no_run
        ///use psn_api_rs::{psn::PSN, types::PSNInner, traits::PSNRequest};
        ///
        ///#[tokio::main]
        ///async fn main() -> std::io::Result<()> {
        ///    let mut psn_inner = PSNInner::new();
        ///
        ///    psn_inner.add_refresh_token("refresh_token".into())
        ///             .add_npsso("npsso".into());
        ///
        ///    psn_inner = psn_inner
        ///            .auth()
        ///            .await
        ///            .expect("Authentication failed");
        ///
        ///    let proxies = vec![
        ///        // ("address", Some(username), Some(password)),
        ///        ("http://abc.com", None, None),
        ///        ("https://test:1234", None, None),
        ///        ("http://abc.com", Some("user"), Some("pass")),
        ///    ];
        ///
        ///    let psn = PSN::new(vec![psn_inner]).await.init_proxy(proxies).await;
        ///
        ///    Ok(())
        ///}
        ///```
        pub async fn init_proxy(
            mut self,
            proxies: Vec<(&str, Option<&str>, Option<&str>)>,
        ) -> Self {
            let mgr = ProxyPoolManager::new();
            let size = proxies.len() as u8;
            for (address, username, password) in proxies.into_iter() {
                mgr.add_proxy(address, username, password);
            }

            let pool = Builder::new()
                .always_check(false)
                .idle_timeout(None)
                .max_lifetime(None)
                .min_idle(size)
                .max_size(size)
                .build(mgr)
                .await
                .expect("Failed to build proxy pool");

            self.proxy_pool = Some(pool);
            self
        }

        /// Add new proxy into `ProxyPoolManager` on the fly.
        /// The max proxy pool size is determined by the first proxies vector's length passed to 'PSN::init_proxy'(upper limit pool size is u8).
        /// Once you hit the max pool size all additional proxies become backup and can only be activated when an active proxy is dropped(connection broken for example)
        pub fn add_proxy(&self, proxies: Vec<(&str, Option<&str>, Option<&str>)>) {
            if let Some(pool) = &self.proxy_pool {
                for (address, username, password) in proxies.into_iter() {
                    pool.get_manager().add_proxy(address, username, password);
                }
            }
        }

        pub async fn get_profile<T: DeserializeOwned + 'static>(
            &self,
            online_id: &str,
        ) -> Result<T, PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner.get_profile(&client, online_id).await
        }

        pub async fn get_titles<T: DeserializeOwned + 'static>(
            &self,
            online_id: &str,
            offset: u32,
        ) -> Result<T, PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner.get_titles(&client, online_id, offset).await
        }

        pub async fn get_trophy_set<T: DeserializeOwned + 'static>(
            &self,
            online_id: &str,
            np_communication_id: &str,
        ) -> Result<T, PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner
                .get_trophy_set(&client, online_id, np_communication_id)
                .await
        }

        pub async fn get_message_threads<T: DeserializeOwned + 'static>(
            &self,
            offset: u32,
        ) -> Result<T, PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner.get_message_threads(&client, offset).await
        }

        pub async fn get_message_thread<T: DeserializeOwned + 'static>(
            &self,
            thread_id: &str,
        ) -> Result<T, PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner.get_message_thread(&client, thread_id).await
        }

        pub async fn generate_message_thread(&self, online_id: &str) -> Result<(), PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner.generate_message_thread(&client, online_id).await
        }

        pub async fn leave_message_thread(&self, thread_id: &str) -> Result<(), PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner.leave_message_thread(&client, thread_id).await
        }

        pub async fn send_message(
            &self,
            online_id: &str,
            msg: Option<&str>,
            path: Option<&str>,
            thread_id: &str,
        ) -> Result<(), PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner
                .send_message(&client, online_id, msg, path, thread_id)
                .await
        }

        pub async fn search_store_items<T: DeserializeOwned + 'static>(
            &self,
            lang: &str,
            region: &str,
            age: &str,
            name: &str,
        ) -> Result<T, PSNError> {
            let (client, psn_inner) = self.get().await?;

            psn_inner
                .search_store_items(&client, lang, region, age, name)
                .await
        }

        async fn get(&self) -> Result<(Client, PoolRef<'_, PSNInnerManager>), PSNError> {
            let proxy_ref = self.get_proxy_cli().await?;
            let inner_ref = self.inner.get().await?;

            let client = match proxy_ref.as_ref() {
                Some(proxy_ref) => (&**proxy_ref).clone(),
                None => (&self.client).clone(),
            };

            drop(proxy_ref);

            Ok((client, inner_ref))
        }

        async fn get_proxy_cli(&self) -> Result<Option<PoolRef<'_, ProxyPoolManager>>, PSNError> {
            let fut = match self.proxy_pool.as_ref() {
                Some(pool) => pool.get(),
                None => return Ok(None),
            };
            let pool_ref = fut.await?;
            Ok(Some(pool_ref))
        }

        pub fn clients_state(&self) -> u8 {
            self.proxy_pool
                .as_ref()
                .map(|pool| pool.state().idle_connections)
                .unwrap_or(0)
        }
    }
}