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
/// Collection of Structs and Enums that dagpi uses.
pub mod models;

/// Blocking client for dagpi
#[cfg(feature = "blocking")]
pub mod blocking;

/// Data Endpoints for the dagpi client
#[cfg(feature = "default")]
pub mod data;
/// Image Endpoints for the dagpi manipulation client
#[cfg(feature = "default")]
pub mod image;

#[cfg(feature = "serenity")]
use typemap_rev::TypeMapKey;

#[cfg(feature = "default")]
use image::Image;

#[cfg(feature = "default")]
use reqwest::header::HeaderMap;
#[cfg(feature = "default")]
use reqwest::header::AUTHORIZATION;
#[cfg(feature = "default")]
use reqwest::Client as HttpClient;

use serde::{Deserialize, Serialize};

#[cfg(feature = "default")]
use crate::data::Data;

#[cfg(feature = "default")]
use std::sync::Arc;

pub use bytes::Bytes;

use std::{
    error::Error,
    fmt::{Display, Formatter, Result as FmtResult},
};

use models::api::MissingParams;

const ENDPOINT: &str = "https://api.dagpi.xyz";
const VERSION: &str = "0.1.3";
#[allow(dead_code)]
const LICENSE: &str = "MIT";

/// Asynchronous client for Dagpi
#[cfg(feature = "default")]
pub struct Client {
    pub token: String,
    pub http: Arc<HttpClient>,
    pub data: Data,
    pub image: Image,
}
#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct MsgResp {
    message: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct DetailResp {
    message: String,
}

#[cfg(feature = "default")]
impl Client {
    /// Initialise a new Async Client
    /// # Example
    /// ```rust
    /// use dagpirs::Client;
    /// use tokio;
    /// #[tokio::main]
    /// async fn main() {
    ///     let token = std::env::var("DAGPI_TOKEN").unwrap();
    ///     let c = Client::new(&token).unwrap();
    /// }
    ///
    pub fn new(token: &str) -> Result<Self, String> {
        let mut headers = HeaderMap::new();

        if token.len() != 64_usize {
            Err("Invalid Token Passed in.".to_string())
        } else {
            headers.insert(
                AUTHORIZATION,
                token.parse().expect("Coudln't parse token into header"),
            );
            let client = Arc::new(
                HttpClient::builder()
                    .default_headers(headers)
                    .user_agent(format!("dagpi.rs v{}", VERSION))
                    .build()
                    .expect("Couldn't construct client"),
            );

            Ok(Client {
                token: token.to_string(),
                data: Data::new(Arc::clone(&client)),
                image: Image::new(Arc::clone(&client)),
                http: client,
            })
        }
    }
}

#[cfg(feature = "serenity")]
impl TypeMapKey for Client {
    type Value = Arc<Self>;
}

pub type HttpResult<S, E> = Result<ApiResponse<S, E>, HttpError>;

/// Result renaming used to difference between an http error and an API error or unsuccessful response
pub type ApiResponse<S, E> = Result<S, E>;

/// Enum Used to store possible API errors
#[derive(Debug)]
pub enum HttpError {
    RequestFailed(reqwest::Error),
    InternalServerError,
    NotFound,
    Unauthorized,
    FileTooLarge,
    ImageUnaccesbile(String),
    ParameterError(MissingParams),
    BadRequest(String),
    RateLimited(i32, i32),
}

impl From<reqwest::Error> for HttpError {
    fn from(e: reqwest::Error) -> Self {
        HttpError::RequestFailed(e)
    }
}

impl Error for HttpError {}

impl Display for HttpError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::RequestFailed(why) => write!(f, "Request failed: {}", why.to_string()),
            Self::Unauthorized => {
                write!(f, "API returned a 403, make sure the token passed is valid")
            }
            Self::NotFound => {
                write!(
                    f,
                    "Resource Wasn't found. Ensure for ImageAPI the route is correct"
                )
            }
            Self::FileTooLarge => {
                write!(f, "Image at url is too large for dagpi to manipulate")
            }
            Self::ImageUnaccesbile(reason) => {
                write!(f, "Api Couldn't Find a valid image. {}", reason)
            }
            Self::ParameterError(reason) => {
                write!(f, "Api Missing Parameters/Invalid Parameters. {:?}", reason)
            }
            Self::BadRequest(reason) => {
                write!(f, "Bad Request: {:?}", reason)
            }
            Self::InternalServerError => write!(f, "Internal server error"),
            Self::RateLimited(limit, used) => {
                write!(
                    f,
                    "You are being ratelimited. You have tried {} which is over your {} limit",
                    used, limit
                )
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::models::{ImageManipulation, ImageManipulationText};

    use super::*;
    use tokio;

    #[test]
    #[should_panic]
    fn valid_token() {
        Client::new("memes").unwrap();
    }

    #[tokio::test]
    #[should_panic]
    async fn unauthorised() {
        let c = Client::new(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        )
        .unwrap();
        let _res = c.data.fact().await.unwrap().unwrap();
    }

    #[tokio::test]
    async fn valid_fact() {
        let token = std::env::var("DAGPI_TOKEN").unwrap();

        let c = Client::new(&token).unwrap();
        if let Ok(_f) = c.data.fact().await.unwrap() {
            assert!(true)
        };
    }

    #[tokio::test]
    async fn bad_image_url() {
        let token = std::env::var("DAGPI_TOKEN").unwrap();

        let c = Client::new(&token).unwrap();
        let _o = match c
            .image
            .image_process("".to_string(), ImageManipulation::America)
            .await
        {
            Err(e) => assert_eq!(
                e.to_string(),
                String::from("Bad Request: \"Your ImageUrl is badly frames\"")
            ),
            Ok(_e) => panic!("WTF no good url"),
        };
    }

    #[tokio::test]
    async fn file_too_large() {
        let token = std::env::var("DAGPI_TOKEN").unwrap();

        let c = Client::new(&token).unwrap();
        let _o = match c
            .image
            .image_process("https://media.discordapp.net/attachments/381963689470984203/820590676911194122/8466462590_bd62f13e8e_o.jpg".to_string(), ImageManipulation::Communism)
            .await
        {
            Err(e) => assert_eq!(
                e.to_string(),
                String::from("Image at url is too large for dagpi to manipulate")
            ),

            Ok(_e) => panic!("WTF no good url"),
        };
    }

    #[tokio::test]
    async fn basic_image_works() {
        let token = std::env::var("DAGPI_TOKEN").unwrap();

        let c = Client::new(&token).unwrap();
        let _o =  c
            .image
            .pride("https://cdn.discordapp.com/avatars/143090142360371200/a_70444022ea3e5d73dd00d59c5578b07e.png".to_string(), models::Pride::Bisexual)
            .await.unwrap().unwrap();
    }

    #[test]
    fn test_blocking_headline() {
        let token = std::env::var("DAGPI_TOKEN").unwrap();

        let c = blocking::Client::new(&token).unwrap();
        if let Ok(_h) = c.data.headline().unwrap() {
            assert!(true)
        }
    }

    #[test]
    fn test_blocking_image() {
        let token = std::env::var("DAGPI_TOKEN").unwrap();

        let c = blocking::Client::new(&token).unwrap();
        if let Ok(_h) = c.image.image_process_text(
            "https://cdn.discordapp.com/avatars/143090142360371200/a_70444022ea3e5d73dd00d59c5578b07e.png".to_string(),
            "Memes".to_string(),
            ImageManipulationText::Captcha,
        ) {
            assert!(true)
        }
    }
}