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
#![allow(unused_assignments)]
use anyhow::Result;
use serde::de::DeserializeOwned;
use serde::Serialize;

pub mod model;
pub mod request;
pub mod response;
pub mod shared;
pub mod traits;

pub use model::User;
pub use shared::Platform;

pub(crate) const BASE_URL: &str = "https://api.warframe.market/v1";

pub(crate) async fn get_endpoint<T: DeserializeOwned>(
    client: &reqwest::Client,
    url: &str,
    jwt: &str,
) -> Result<T> {
    let mut headers = reqwest::header::HeaderMap::new();
    headers.insert("authorization", jwt.parse()?);
    headers.insert("Content-Type", "application/json".parse()?);

    let raw = client
        .get(format!("{}{}", BASE_URL, url))
        .headers(headers)
        .send()
        .await?
        .text()
        .await?;

    let base: response::ResponseWrapper<T> = serde_json::from_str(&raw)?;

    Ok(base.payload)
}

pub(crate) async fn post_endpoint<T: DeserializeOwned, B: Serialize>(
    client: &reqwest::Client,
    url: &str,
    jwt: &str,
    body: &B,
) -> Result<T> {
    let mut headers = reqwest::header::HeaderMap::new();
    headers.insert("authorization", jwt.parse()?);
    headers.insert("Content-Type", "application/json".parse()?);

    let raw = client
        .post(format!("{}{}", BASE_URL, url))
        .headers(headers)
        .body(serde_json::to_string(body)?)
        .send()
        .await?
        .text()
        .await?;

    let base: response::ResponseWrapper<T> = serde_json::from_str(&raw)?;

    Ok(base.payload)
}

pub(crate) async fn delete_endpoint<T: DeserializeOwned>(
    client: &reqwest::Client,
    url: &str,
    jwt: &str,
) -> Result<T> {
    let mut headers = reqwest::header::HeaderMap::new();
    headers.insert("authorization", jwt.parse()?);
    headers.insert("Content-Type", "application/json".parse()?);

    let raw = client
        .delete(format!("{}{}", BASE_URL, url))
        .headers(headers)
        .send()
        .await?
        .text()
        .await?;

    let base: response::ResponseWrapper<T> = serde_json::from_str(&raw)?;

    Ok(base.payload)
}

pub(crate) async fn put_endpoint<T: Serialize>(
    client: &reqwest::Client,
    url: &str,
    jwt: &str,
    body: &T,
) -> Result<()> {
    let mut headers = reqwest::header::HeaderMap::new();
    headers.insert("authorization", jwt.parse()?);
    headers.insert("Content-Type", "application/json".parse()?);

    let raw = client
        .put(format!("{}{}", BASE_URL, url))
        .headers(headers)
        .body(serde_json::to_string(body)?)
        .send()
        .await?
        .error_for_status()?;

    Ok(())
}