giphy_api/
gifs.rs

1use crate::Client;
2use crate::ClientResult;
3
4pub struct Gifs {
5    pub client: Client,
6}
7
8impl Gifs {
9    #[doc(hidden)]
10    pub fn new(client: Client) -> Self {
11        Gifs { client }
12    }
13
14    /**
15     * Get GIFs by ID.
16     *
17     * This function performs a `GET` to the `/gifs` endpoint.
18     *
19     * A multiget version of the get GIF by ID endpoint.
20     *
21     *
22     * **Parameters:**
23     *
24     * * `ids: &str` -- Filters results by specified GIF IDs, separated by commas.
25     */
26    pub async fn get(
27        &self,
28        ids: &str,
29    ) -> ClientResult<crate::Response<crate::types::GetGifsByResponse>> {
30        let mut query_args: Vec<(String, String)> = Default::default();
31        if !ids.is_empty() {
32            query_args.push(("ids".to_string(), ids.to_string()));
33        }
34        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
35        let url = self.client.url(&format!("/gifs?{}", query_), None);
36        self.client
37            .get(
38                &url,
39                crate::Message {
40                    body: None,
41                    content_type: None,
42                },
43            )
44            .await
45    }
46    /**
47     * Random GIF.
48     *
49     * This function performs a `GET` to the `/gifs/random` endpoint.
50     *
51     * Returns a random GIF, limited by tag. Excluding the tag parameter will return a random GIF from the GIPHY catalog.
52     *
53     *
54     * **Parameters:**
55     *
56     * * `tag: &str` -- The unique bit.ly URL for this GIF.
57     * * `rating: &str` -- The unique bit.ly URL for this GIF.
58     */
59    pub async fn random(
60        &self,
61        tag: &str,
62        rating: &str,
63    ) -> ClientResult<crate::Response<crate::types::RandomGifResponse>> {
64        let mut query_args: Vec<(String, String)> = Default::default();
65        if !rating.is_empty() {
66            query_args.push(("rating".to_string(), rating.to_string()));
67        }
68        if !tag.is_empty() {
69            query_args.push(("tag".to_string(), tag.to_string()));
70        }
71        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
72        let url = self.client.url(&format!("/gifs/random?{}", query_), None);
73        self.client
74            .get(
75                &url,
76                crate::Message {
77                    body: None,
78                    content_type: None,
79                },
80            )
81            .await
82    }
83    /**
84     * Search GIFs.
85     *
86     * This function performs a `GET` to the `/gifs/search` endpoint.
87     *
88     * Search all GIPHY GIFs for a word or phrase. Punctuation will be stripped and ignored.  Use a plus or url encode for phrases. Example paul+rudd, ryan+gosling or american+psycho.
89     *
90     *
91     * **Parameters:**
92     *
93     * * `q: &str` -- The unique bit.ly URL for this GIF.
94     * * `limit: i64` -- The maximum number of records to return.
95     * * `offset: i64` -- An optional results offset.
96     * * `rating: &str` -- The unique bit.ly URL for this GIF.
97     * * `lang: &str` -- Specify default language for regional content; use a 2-letter ISO 639-1 language code.
98     */
99    pub async fn search(
100        &self,
101        q: &str,
102        limit: i64,
103        offset: i64,
104        rating: &str,
105        lang: &str,
106    ) -> ClientResult<crate::Response<crate::types::GetGifsByResponse>> {
107        let mut query_args: Vec<(String, String)> = Default::default();
108        if !lang.is_empty() {
109            query_args.push(("lang".to_string(), lang.to_string()));
110        }
111        if limit > 0 {
112            query_args.push(("limit".to_string(), limit.to_string()));
113        }
114        if offset > 0 {
115            query_args.push(("offset".to_string(), offset.to_string()));
116        }
117        if !q.is_empty() {
118            query_args.push(("q".to_string(), q.to_string()));
119        }
120        if !rating.is_empty() {
121            query_args.push(("rating".to_string(), rating.to_string()));
122        }
123        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
124        let url = self.client.url(&format!("/gifs/search?{}", query_), None);
125        self.client
126            .get(
127                &url,
128                crate::Message {
129                    body: None,
130                    content_type: None,
131                },
132            )
133            .await
134    }
135    /**
136     * Translate phrase to GIF.
137     *
138     * This function performs a `GET` to the `/gifs/translate` endpoint.
139     *
140     * The translate API draws on search, but uses the GIPHY `special sauce` to handle translating from one vocabulary to another. In this case, words and phrases to GIF
141     *
142     *
143     * **Parameters:**
144     *
145     * * `s: &str` -- The unique bit.ly URL for this GIF.
146     */
147    pub async fn translate(
148        &self,
149        s: &str,
150    ) -> ClientResult<crate::Response<crate::types::RandomGifResponse>> {
151        let mut query_args: Vec<(String, String)> = Default::default();
152        if !s.is_empty() {
153            query_args.push(("s".to_string(), s.to_string()));
154        }
155        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
156        let url = self
157            .client
158            .url(&format!("/gifs/translate?{}", query_), None);
159        self.client
160            .get(
161                &url,
162                crate::Message {
163                    body: None,
164                    content_type: None,
165                },
166            )
167            .await
168    }
169    /**
170     * Trending GIFs.
171     *
172     * This function performs a `GET` to the `/gifs/trending` endpoint.
173     *
174     * Fetch GIFs currently trending online. Hand curated by the GIPHY editorial team.  The data returned mirrors the GIFs showcased on the GIPHY homepage. Returns 25 results by default.
175     *
176     *
177     * **Parameters:**
178     *
179     * * `limit: i64` -- The maximum number of records to return.
180     * * `offset: i64` -- An optional results offset.
181     * * `rating: &str` -- The unique bit.ly URL for this GIF.
182     */
183    pub async fn trending(
184        &self,
185        limit: i64,
186        offset: i64,
187        rating: &str,
188    ) -> ClientResult<crate::Response<crate::types::GetGifsByResponse>> {
189        let mut query_args: Vec<(String, String)> = Default::default();
190        if limit > 0 {
191            query_args.push(("limit".to_string(), limit.to_string()));
192        }
193        if offset > 0 {
194            query_args.push(("offset".to_string(), offset.to_string()));
195        }
196        if !rating.is_empty() {
197            query_args.push(("rating".to_string(), rating.to_string()));
198        }
199        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
200        let url = self.client.url(&format!("/gifs/trending?{}", query_), None);
201        self.client
202            .get(
203                &url,
204                crate::Message {
205                    body: None,
206                    content_type: None,
207                },
208            )
209            .await
210    }
211    /**
212     * Get GIF by Id.
213     *
214     * This function performs a `GET` to the `/gifs/{gifId}` endpoint.
215     *
216     * Returns a GIF given that GIF's unique ID
217     *
218     *
219     * **Parameters:**
220     *
221     * * `gif_id: i64` -- Filters results by specified GIF ID.
222     */
223    pub async fn get_gifs(
224        &self,
225        gif_id: i64,
226    ) -> ClientResult<crate::Response<crate::types::RandomGifResponse>> {
227        let url = self.client.url(
228            &format!(
229                "/gifs/{}",
230                crate::progenitor_support::encode_path(&gif_id.to_string()),
231            ),
232            None,
233        );
234        self.client
235            .get(
236                &url,
237                crate::Message {
238                    body: None,
239                    content_type: None,
240                },
241            )
242            .await
243    }
244}