api_video 0.3.3

This is unofficial client for api.video written in Rust
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
pub mod rapi {

    use reqwest::{self, multipart, Body, StatusCode};
    use serde_json::{self, json};
    use tokio::fs::File;
    use tokio_util::codec::{BytesCodec, FramedRead};

    #[warn(dead_code)]
    pub struct ApiVideo {
        pub production: bool,
        pub token: String,
    }

    #[warn(dead_code)]
    struct Uri {
        pro: String,
        sand: String,
    }

    #[warn(dead_code)]
    impl ApiVideo {
        async fn geturl(&self) -> String {
            let z = Uri {
                pro: "https://ws.api.video".to_string(),
                sand: "https://sandbox.api.video".to_string(),
            };

            let z = match self.production {
                true => z.pro,
                false => z.sand,
            };
            return z;
        }

        async fn video_object(
            &self,
            url: String,
            public: bool,
            panoramic: bool,
            mp4support: bool,
            title: String,
        ) -> String {
            let z = json!({"public":public,"panoramic":panoramic,"mp4Support":mp4support,"title":title});

            let client = reqwest::Client::new();
            let res: serde_json::Value = client
                .post(url)
                .bearer_auth(&self.token)
                .header("Content-Type", "application/json")
                .json(&z)
                .send()
                .await
                .unwrap()
                .json()
                .await
                .unwrap();

            let mut videoid = String::new();
            for a in res["videoId"].to_string().chars() {
                if a != '"' {
                    videoid.push(a);
                }
            }
            return videoid;
        }

        /// Upload video to api.video
        /// # Example
        /// video_upload("myfirstvideo","G:\video.mp4").await;

        pub async fn video_upload(&self, title: String, full_file_path: String) -> String {
            let z = &self.geturl().await;

            let url = format!("{}/videos", z);

            let videoid = match self.production {
                true => self.video_object(url, true, false, true, title).await,
                false => self.video_object(url, true, false, true, title).await,
            };

            let f = File::open(full_file_path.clone()).await.unwrap();
            let file_len = f.metadata().await.unwrap().len();

            if file_len < 209715200 {
                let file = File::open(full_file_path).await.unwrap();
                let stream = FramedRead::new(file, BytesCodec::new());
                let file_body = Body::wrap_stream(stream);

                let some_file = multipart::Part::stream(file_body).file_name("foo");

                let form = multipart::Form::new().part("file", some_file);

                let url = format!("{}/videos/{}/source", z, videoid);
                let client = reqwest::Client::new();
                client
                    .post(url)
                    .bearer_auth(&self.token)
                    .multipart(form)
                    .send()
                    .await
                    .unwrap();
            }

            let z = format!("https://embed.api.video/vod/{}", videoid);
            return z;
        }

        /// Get all videos
        /// # Example
        /// get_all_video().await;

        pub async fn get_all_video(&self) -> serde_json::Value {
            let z = &self.geturl().await;
            let url = format!("{}/videos", z);
            let client = reqwest::Client::new();
            let res: serde_json::Value = client
                .get(url)
                .bearer_auth(&self.token)
                .header("accept", "application/json")
                .send()
                .await
                .unwrap()
                .json()
                .await
                .unwrap();

            return res;
        }

        /// Delete a video
        /// # Example
        /// del_video("123".to_string()).await;

        pub async fn del_video(&self, videoid: String) -> StatusCode {
            let url = format!("https://ws.api.video/videos/{}", videoid);
            let client = reqwest::Client::new();
            let res = client
                .delete(url)
                .bearer_auth(&self.token)
                .send()
                .await
                .unwrap();
            return res.status();
        }

        /// Thumbnail Upload
        /// # Example
        /// thumbnail_upload("123".to_string(),"/123".to_string()).await;

        pub async fn thumbnail_upload(
            &self,
            videoid: String,
            full_file_path: String,
        ) -> serde_json::Value {
            let z = &self.geturl().await;

            let url = format!("{}/videos/{}/thumbnail", z, videoid);

            //let f = File::open(full_file_path.clone()).await.unwrap();
            //let file_len = f.metadata().await.unwrap().len();

            let file = File::open(full_file_path).await.unwrap();
            let stream = FramedRead::new(file, BytesCodec::new());
            let file_body = Body::wrap_stream(stream);

            let some_file = multipart::Part::stream(file_body).file_name("foo");

            let form = multipart::Form::new().part("file", some_file);

            let client = reqwest::Client::new();
            let res = client
                .post(url)
                .bearer_auth(&self.token)
                .multipart(form)
                .send()
                .await
                .unwrap()
                .json()
                .await
                .unwrap();
            return res;
        }

        /// Watermark Upload
        /// # Example
        /// watermark_upload(,"/123".to_string());

        pub async fn watermark_upload(&self, full_file_path: String) -> serde_json::Value {
            let z = &self.geturl().await;

            let url = format!("{}/watermarks", z);

            //let f = File::open(full_file_path.clone()).await.unwrap();
            //let file_len = f.metadata().await.unwrap().len();

            let file = File::open(full_file_path).await.unwrap();
            let stream = FramedRead::new(file, BytesCodec::new());
            let file_body = Body::wrap_stream(stream);

            let some_file = multipart::Part::stream(file_body).file_name("foo");

            let form = multipart::Form::new().part("file", some_file);

            let client = reqwest::Client::new();
            let res = client
                .post(url)
                .bearer_auth(&self.token)
                .multipart(form)
                .send()
                .await
                .unwrap()
                .json()
                .await
                .unwrap();
            return res;
        }

        /// Get Watermark
        /// # Example
        /// get_watermark().await;

        pub async fn get_watermark(&self) -> serde_json::Value {
            let z = &self.geturl().await;

            let url = format!("{}/watermarks", z);

            let client = reqwest::Client::new();
            let res = client
                .get(url)
                .bearer_auth(&self.token)
                .send()
                .await
                .unwrap()
                .json()
                .await
                .unwrap();
            return res;
        }

        /// Delete Watermark
        /// # Example
        /// watermark_delete("watermarkid".to_string()).await;

        pub async fn watermark_delete(&self, watermarkid: String) -> StatusCode {
            let z = &self.geturl().await;

            let url = format!("{}/watermarks/{}", z, watermarkid);

            //let f = File::open(full_file_path.clone()).await.unwrap();
            //let file_len = f.metadata().await.unwrap().len();

            let client = reqwest::Client::new();
            let res = client
                .delete(url)
                .bearer_auth(&self.token)
                .send()
                .await
                .unwrap();
            return res.status();
        }

        /// Get Caption
        /// # Example
        /// get_caption("videoid".to_string(),"language".to_string()).await;

        pub async fn get_caption(&self, videoid: String, lang: String) -> serde_json::Value {
            let z = &self.geturl().await;

            let url = format!("{}/videos/{}/captions/{}", z, videoid, lang);

            let client = reqwest::Client::new();
            let res = client
                .get(url)
                .bearer_auth(&self.token)
                .send()
                .await
                .unwrap()
                .json()
                .await
                .unwrap();
            return res;
        }

        /// Upload Caption
        /// # Example
        /// caption_upload("videoid".to_string(),"language".to_string(),"full file path".to_string()).await;

        pub async fn caption_upload(
            &self,
            videoid: String,
            lang: String,
            full_file_path: String,
        ) -> serde_json::Value {
            let z = &self.geturl().await;

            let url = format!("{}/videos/{}/captions/{}", z, videoid, lang);

            //let f = File::open(full_file_path.clone()).await.unwrap();
            //let file_len = f.metadata().await.unwrap().len();

            let file = File::open(full_file_path).await.unwrap();
            let stream = FramedRead::new(file, BytesCodec::new());
            let file_body = Body::wrap_stream(stream);

            let some_file = multipart::Part::stream(file_body).file_name("foo");

            let form = multipart::Form::new().part("file", some_file);

            let client = reqwest::Client::new();
            let res = client
                .post(url)
                .bearer_auth(&self.token)
                .multipart(form)
                .send()
                .await
                .unwrap()
                .json()
                .await
                .unwrap();
            return res;
        }

        /// Delete Caption
        /// # Example
        /// caption_delete("videoid".to_string(),"language".to_string()).await;

        pub async fn caption_delete(&self, videoid: String, lang: String) -> StatusCode {
            let z = &self.geturl().await;

            let url = format!("{}/videos/{}/captions/{}", z, videoid, lang);

            let client = reqwest::Client::new();
            let res = client
                .delete(url)
                .bearer_auth(&self.token)
                .send()
                .await
                .unwrap();

            return res.status();
        }
    }
}

pub mod rapi_sync {

    use reqwest::blocking::multipart;
    use reqwest::StatusCode;
    use serde_json::{self, json};

    #[warn(dead_code)]
    pub struct ApiVideo {
        pub production: bool,
        pub token: String,
    }

    #[warn(dead_code)]
    struct Uri {
        pro: String,
        sand: String,
    }

    #[warn(dead_code)]
    impl ApiVideo {
        fn geturl(&self) -> String {
            let z = Uri {
                pro: "https://ws.api.video".to_string(),
                sand: "https://sandbox.api.video".to_string(),
            };

            let z = match self.production {
                true => z.pro,
                false => z.sand,
            };
            return z;
        }

        fn video_object(
            &self,
            url: String,
            public: bool,
            panoramic: bool,
            mp4support: bool,
            title: String,
        ) -> String {
            let z = json!({"public":public,"panoramic":panoramic,"mp4Support":mp4support,"title":title});

            let client = reqwest::blocking::Client::new();
            let res: serde_json::Value = client
                .post(url)
                .bearer_auth(&self.token)
                .header("Content-Type", "application/json")
                .json(&z)
                .send()
                .unwrap()
                .json()
                .unwrap();

            let mut videoid = String::new();
            for a in res["videoId"].to_string().chars() {
                if a != '"' {
                    videoid.push(a);
                }
            }
            return videoid;
        }

        ///Upload video to api.video
        /// # Example
        /// video_upload("myfirstvideo","G:\video.mp4")

        pub fn video_upload(&self, title: String, full_file_path: String) -> String {
            let z = &self.geturl();

            let url = format!("{}/videos", z);

            let videoid = match self.production {
                true => self.video_object(url, true, false, true, title),
                false => self.video_object(url, true, false, true, title),
            };

            let metadata = std::fs::metadata(full_file_path.clone()).unwrap();
            let file_len = metadata.len();

            if file_len < 209715200 {
                let form = multipart::Form::new().file("file", full_file_path).unwrap();

                let url = format!("{}/videos/{}/source", z, videoid);
                let client = reqwest::blocking::Client::new();
                client
                    .post(url)
                    .bearer_auth(&self.token)
                    .multipart(form)
                    .send()
                    .unwrap();
            }

            let z = format!("https://embed.api.video/vod/{}", videoid);
            return z;
        }

        pub fn get_all_video(&self) -> serde_json::Value {
            let z = &self.geturl();
            let url = format!("{}/videos", z);
            let client = reqwest::blocking::Client::new();
            let res: serde_json::Value = client
                .get(url)
                .bearer_auth(&self.token)
                .header("accept", "application/json")
                .send()
                .unwrap()
                .json()
                .unwrap();

            return res;
        }

        pub fn del_video(&self, videoid: String) -> StatusCode {
            let url = format!("https://ws.api.video/videos/{}", videoid);
            let client = reqwest::blocking::Client::new();
            let res = client.delete(url).bearer_auth(&self.token).send().unwrap();
            return res.status();
        }

        pub fn thumbnail_upload(
            &self,
            videoid: String,
            full_file_path: String,
        ) -> serde_json::Value {
            let z = &self.geturl();

            let url = format!("{}/videos/{}/thumbnail", z, videoid);

            let form = multipart::Form::new().file("file", full_file_path).unwrap();

            let client = reqwest::blocking::Client::new();
            let res = client
                .post(url)
                .bearer_auth(&self.token)
                .multipart(form)
                .send()
                .unwrap()
                .json()
                .unwrap();
            return res;
        }

        pub fn watermark_upload(&self, full_file_path: String) -> serde_json::Value {
            let z = &self.geturl();

            let url = format!("{}/watermarks", z);

            let form = multipart::Form::new().file("file", full_file_path).unwrap();

            let client = reqwest::blocking::Client::new();
            let res = client
                .post(url)
                .bearer_auth(&self.token)
                .multipart(form)
                .send()
                .unwrap()
                .json()
                .unwrap();
            return res;
        }

        pub fn get_watermark(&self) -> serde_json::Value {
            let z = &self.geturl();

            let url = format!("{}/watermarks", z);

            let client = reqwest::blocking::Client::new();
            let res = client
                .get(url)
                .bearer_auth(&self.token)
                .send()
                .unwrap()
                .json()
                .unwrap();
            return res;
        }

        pub fn watermark_delete(&self, watermarkid: String) -> StatusCode {
            let z = &self.geturl();

            let url = format!("{}/watermarks/{}", z, watermarkid);

            let client = reqwest::blocking::Client::new();
            let res = client.delete(url).bearer_auth(&self.token).send().unwrap();
            return res.status();
        }

        pub fn get_caption(&self, videoid: String, lang: String) -> serde_json::Value {
            let z = &self.geturl();

            let url = format!("{}/videos/{}/captions/{}", z, videoid, lang);

            let client = reqwest::blocking::Client::new();
            let res = client
                .get(url)
                .bearer_auth(&self.token)
                .send()
                .unwrap()
                .json()
                .unwrap();
            return res;
        }

        pub fn caption_upload(
            &self,
            videoid: String,
            lang: String,
            full_file_path: String,
        ) -> serde_json::Value {
            let z = &self.geturl();

            let url = format!("{}/videos/{}/captions/{}", z, videoid, lang);

            let form = multipart::Form::new().file("file", full_file_path).unwrap();

            let client = reqwest::blocking::Client::new();
            let res = client
                .post(url)
                .bearer_auth(&self.token)
                .multipart(form)
                .send()
                .unwrap()
                .json()
                .unwrap();
            return res;
        }

        pub fn caption_delete(&self, videoid: String, lang: String) -> StatusCode {
            let z = &self.geturl();

            let url = format!("{}/videos/{}/captions/{}", z, videoid, lang);

            let client = reqwest::blocking::Client::new();
            let res = client.delete(url).bearer_auth(&self.token).send().unwrap();
            return res.status();
        }
    }
}