3 unstable releases

0.3.0 Apr 27, 2020
0.1.1 Aug 13, 2019
0.1.0 Aug 13, 2019

#10 in #playstation

MIT license

59KB
1.5K SLoC

A Simple PSN API wrapper


Features:

Use reqwest as http client connecting to PSN network.
Get PSN user profile, trophies, games info
Receive/send PSN messages.
Get PSN store info.



lib.rs:

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 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:

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.
}

Dependencies

~11–15MB
~272K SLoC