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
use reqwest::header::{HeaderValue, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use url::Url;

/// Blank meme template that can be captioned with text boxes
#[derive(Debug, Deserialize)]
pub struct MemeTemplate {
    id: String,
    name: String,
    url: Url,
    width: u32,
    height: u32,
    box_count: u32,
}

impl MemeTemplate {
    /// Returns the identifier of this meme template.
    ///
    /// This equals the required `template_id` input parameter for the `/caption_image` API endpoint.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns the name of this meme template.
    ///
    /// This is a human readable english string such as "Grumpy Cat".
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the URL of the blank image for this meme template.
    ///
    /// This is an image with the dimensions given in `width` and `height`,
    /// without any caption boxes.
    pub fn url(&self) -> &Url {
        &self.url
    }

    /// Returns the width in pixels of the blank image for this meme template.
    pub fn width(&self) -> u32 {
        self.width
    }

    /// Returns the height in pixels of the blank image for this meme template.
    pub fn height(&self) -> u32 {
        self.height
    }

    /// Returns the number of caption boxes that this meme templates uses.
    ///
    /// Some memes have more than just a top and bottom text by default.
    pub fn box_count(&self) -> u32 {
        self.box_count
    }
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Response<T> {
    SuccessResponse {
        success: bool,
        data: T,
    },
    FailureResponse {
        success: bool,
        error_message: String,
    },
}

impl<T> Response<T> {
    fn convert(self) -> Result<T> {
        match self {
            Response::SuccessResponse { data, .. } => Ok(data),
            Response::FailureResponse { error_message, .. } => Err(Error::ApiError(error_message)),
        }
    }
}

/// Font for [`CaptionBoxesRequest`](crate::CaptionBoxesRequest)
///
/// API defaults to `Impact`
#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum CaptionFont {
    Impact,
    Arial,
}

/// Text and other fields for image caption
#[derive(Debug, Serialize)]
pub struct CaptionBox {
    text: String,
    x: Option<u32>,
    y: Option<u32>,
    width: Option<u32>,
    height: Option<u32>,
    color: Option<String>,
    outline_color: Option<String>,
}

/// Builder for [`CaptionBox`](crate::CaptionBox)
pub struct CaptionBoxBuilder {
    text: String,
    x: Option<u32>,
    y: Option<u32>,
    width: Option<u32>,
    height: Option<u32>,
    color: Option<String>,
    outline_color: Option<String>,
}

impl CaptionBoxBuilder {
    pub fn new<S: Into<String>>(text: S) -> Self {
        CaptionBoxBuilder {
            text: text.into(),
            x: None,
            y: None,
            width: None,
            height: None,
            color: None,
            outline_color: None,
        }
    }

    pub fn dimension(mut self, x: u32, y: u32, width: u32, height: u32) -> Self {
        self.x = Some(x);
        self.y = Some(y);
        self.width = Some(width);
        self.height = Some(height);
        self
    }

    pub fn color<S: Into<String>>(mut self, color: S) -> Self {
        self.color = Some(color.into());
        self
    }

    pub fn outline_color<S: Into<String>>(mut self, outline_color: S) -> Self {
        self.outline_color = Some(outline_color.into());
        self
    }

    pub fn build(self) -> CaptionBox {
        CaptionBox {
            text: self.text,
            x: self.x,
            y: self.y,
            width: self.width,
            height: self.height,
            color: self.color,
            outline_color: self.outline_color,
        }
    }
}

/// Request data to caption a meme template
#[derive(Debug, Serialize)]
pub struct CaptionBoxesRequest {
    template_id: String,
    font: Option<CaptionFont>,
    max_font_size: Option<u32>,
    boxes: Vec<CaptionBox>,
}

/// Builder for [`CaptionBoxesRequest`](crate::CaptionBoxesRequest)
pub struct CaptionBoxesRequestBuilder {
    template_id: String,
    font: Option<CaptionFont>,
    max_font_size: Option<u32>,
    boxes: Vec<CaptionBox>,
}

impl CaptionBoxesRequestBuilder {
    pub fn new<S: Into<String>>(template_id: S) -> Self {
        CaptionBoxesRequestBuilder {
            template_id: template_id.into(),
            font: None,
            max_font_size: None,
            boxes: Vec::with_capacity(2),
        }
    }

    pub fn font(mut self, font: CaptionFont) -> Self {
        self.font = Some(font);
        self
    }

    pub fn max_font_size(mut self, max_font_size: u32) -> Self {
        self.max_font_size = Some(max_font_size);
        self
    }

    pub fn caption_box(mut self, caption_box: CaptionBox) -> Self {
        self.boxes.push(caption_box);
        self
    }

    pub fn build(self) -> CaptionBoxesRequest {
        CaptionBoxesRequest {
            template_id: self.template_id,
            font: self.font,
            max_font_size: self.max_font_size,
            boxes: self.boxes,
        }
    }
}

/// A captioned meme template
#[derive(Debug, Deserialize)]
pub struct CaptionImageResponse {
    url: Url,
    page_url: Url,
}

impl CaptionImageResponse {
    /// Returns the URL of the generated captioned image
    pub fn url(&self) -> &Url {
        &self.url
    }

    /// Returns the URL of the generated captioned image page
    pub fn page_url(&self) -> &Url {
        &self.page_url
    }
}

/// [`Error`](std:error:Error) implementation for all crate errors
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// Error internal to `reqwest`
    #[error("HTTP request/response error")]
    Reqwest(#[from] reqwest::Error),

    /// Error internal to `serde_qs`
    #[error("form querystring de/serialization error")]
    SerdeQs(#[from] serde_qs::Error),

    /// API error message from `api.imgflip.com`
    #[error("API error: {0}")]
    ApiError(String),
}

/// [`Result`](std::result::Result) alias with crate's [`Error`](crate::Error)
pub type Result<T> = std::result::Result<T, crate::Error>;

/// Client for `api.imgflip.com` that obtains blank meme templates
///
/// You should resuse `Client` instances, since they do internal connection pooling.
/// # Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
/// let client = imgflip::Client::new();
/// let memes = client.memes().await?;
/// println!("much memes. very easy. wow.\n{:?}", memes);
/// # Ok(())
/// # }
/// ```
pub struct Client {
    client: reqwest::Client,
}

impl Client {
    /// Creates a new instance with default values
    pub fn new() -> Self {
        Client {
            client: reqwest::Client::new(),
        }
    }

    async fn client_memes(client: &reqwest::Client) -> Result<Vec<MemeTemplate>> {
        #[derive(Debug, Deserialize)]
        struct MemeTemplatesData {
            memes: Vec<MemeTemplate>,
        }

        client
            .get("https://api.imgflip.com/get_memes")
            .send()
            .await?
            .error_for_status()?
            .json::<Response<MemeTemplatesData>>()
            .await?
            .convert()
            .map(|r| r.memes)
    }

    /// Calls the `/get_memes` endpoint to return a list of popular meme templates
    pub async fn memes(&self) -> Result<Vec<MemeTemplate>> {
        Self::client_memes(&self.client).await
    }
}

/// Client for `api.imgflip.com` that can caption meme templates
///
/// Unlike [`Client`](imgflip::Client) this requires an account on [imgflip.com](https://imgflip.com/signup).
pub struct AccountClient {
    username: String,
    password: String,
    client: reqwest::Client,
}

impl AccountClient {
    /// Creates a new instance for the given account
    pub fn new<S: Into<String>>(username: S, password: S) -> Self {
        AccountClient {
            client: reqwest::Client::new(),
            username: username.into(),
            password: password.into(),
        }
    }

    /// Calls the `/caption_image` endpoint to add caption boxes to a meme template
    pub async fn caption_image(
        &self,
        image_caption: CaptionBoxesRequest,
    ) -> Result<CaptionImageResponse> {
        #[derive(Debug, Serialize)]
        struct RequestAuthWrapper<T> {
            #[serde(flatten)]
            request: T,
            username: String,
            password: String,
        }

        self.client
            .post("https://api.imgflip.com/caption_image")
            .header(
                CONTENT_TYPE,
                HeaderValue::from_static("application/x-www-form-urlencoded"),
            )
            .body(serde_qs::to_string(&RequestAuthWrapper {
                request: image_caption,
                username: self.username.clone(),
                password: self.password.clone(),
            })?)
            .send()
            .await?
            .error_for_status()?
            .json::<Response<CaptionImageResponse>>()
            .await?
            .convert()
    }

    /// Calls the `/get_memes` endpoint to return a list of popular meme templates
    pub async fn memes(&self) -> Result<Vec<MemeTemplate>> {
        Client::client_memes(&self.client).await
    }
}