1use super::CatalogEndpoint;
4use openlark_core::api::{ApiRequest, HttpMethod};
5
6#[derive(Debug, Clone, PartialEq)]
9#[cfg_attr(test, derive(strum_macros::EnumIter))]
10pub enum CcmDriveExplorerApiOld {
11 RootFolderMeta,
13 FolderMeta(String), File(String), FileSpreadsheets(String), FileCopy(String), FileDocs(String), FolderChildren(String), Folder(String), }
28
29impl CcmDriveExplorerApiOld {
30 pub fn to_url(&self) -> String {
32 match self {
33 CcmDriveExplorerApiOld::RootFolderMeta => {
34 "/open-apis/drive/explorer/v2/root_folder/meta".to_string()
35 }
36 CcmDriveExplorerApiOld::FolderMeta(folder_token) => {
37 format!("/open-apis/drive/explorer/v2/folder/{folder_token}/meta")
38 }
39 CcmDriveExplorerApiOld::File(folder_token) => {
40 format!("/open-apis/drive/explorer/v2/file/{folder_token}")
41 }
42 CcmDriveExplorerApiOld::FileSpreadsheets(spreadsheet_token) => {
43 format!("/open-apis/drive/explorer/v2/file/spreadsheets/{spreadsheet_token}")
44 }
45 CcmDriveExplorerApiOld::FileCopy(file_token) => {
46 format!("/open-apis/drive/explorer/v2/file/copy/files/{file_token}")
47 }
48 CcmDriveExplorerApiOld::FileDocs(doc_token) => {
49 format!("/open-apis/drive/explorer/v2/file/docs/{doc_token}")
50 }
51 CcmDriveExplorerApiOld::FolderChildren(folder_token) => {
52 format!("/open-apis/drive/explorer/v2/folder/{folder_token}/children")
53 }
54 CcmDriveExplorerApiOld::Folder(folder_token) => {
55 format!("/open-apis/drive/explorer/v2/folder/{folder_token}")
56 }
57 }
58 }
59
60 pub fn to_request<R>(&self) -> ApiRequest<R> {
62 <Self as CatalogEndpoint>::to_request(self)
63 }
64}
65
66impl CatalogEndpoint for CcmDriveExplorerApiOld {
67 fn to_url(&self) -> String {
68 CcmDriveExplorerApiOld::to_url(self)
69 }
70
71 fn method(&self) -> HttpMethod {
72 match self {
73 Self::RootFolderMeta | Self::FolderMeta(_) | Self::FolderChildren(_) => HttpMethod::Get,
74 Self::File(_) | Self::FileCopy(_) | Self::Folder(_) => HttpMethod::Post,
75 Self::FileSpreadsheets(_) | Self::FileDocs(_) => HttpMethod::Delete,
76 }
77 }
78
79 }
81
82#[derive(Debug, Clone, PartialEq)]
85#[cfg_attr(test, derive(strum_macros::EnumIter))]
86pub enum CcmDriveExplorerApi {
87 RootFolderMeta,
89 FolderMeta(String), File(String), FileCopy(String), FileDocs(String), FileSpreadsheets(String), FolderChildren(String), Folder,
103}
104
105impl CcmDriveExplorerApi {
106 pub fn to_url(&self) -> String {
108 match self {
109 CcmDriveExplorerApi::RootFolderMeta => {
110 "/open-apis/drive/v1/explorer/root_folder/meta".to_string()
111 }
112 CcmDriveExplorerApi::FolderMeta(folder_token) => {
113 format!("/open-apis/drive/v1/explorer/folder/{folder_token}/meta")
114 }
115 CcmDriveExplorerApi::File(file_token) => {
116 format!("/open-apis/drive/v1/explorer/file/{file_token}")
117 }
118 CcmDriveExplorerApi::FileCopy(file_token) => {
119 format!("/open-apis/drive/v1/explorer/file/copy/files/{file_token}")
120 }
121 CcmDriveExplorerApi::FileDocs(file_token) => {
122 format!("/open-apis/drive/v1/explorer/file/docs/{file_token}")
123 }
124 CcmDriveExplorerApi::FileSpreadsheets(file_token) => {
125 format!("/open-apis/drive/v1/explorer/file/spreadsheets/{file_token}")
126 }
127 CcmDriveExplorerApi::FolderChildren(folder_token) => {
128 format!("/open-apis/drive/v1/explorer/folder/{folder_token}/children")
129 }
130 CcmDriveExplorerApi::Folder => "/open-apis/drive/v1/explorer/folder".to_string(),
131 }
132 }
133
134 pub fn to_url_with_params(&self, params: &[(&str, String)]) -> String {
136 let base_url = self.to_url();
137 if params.is_empty() {
138 return base_url;
139 }
140
141 let query_string = params
142 .iter()
143 .map(|(key, value)| format!("{}={}", key, simple_url_encode(value)))
144 .collect::<Vec<_>>()
145 .join("&");
146
147 format!("{base_url}?{query_string}")
148 }
149
150 pub fn to_request<R>(&self) -> ApiRequest<R> {
152 <Self as CatalogEndpoint>::to_request(self)
153 }
154}
155
156impl CatalogEndpoint for CcmDriveExplorerApi {
157 fn to_url(&self) -> String {
158 CcmDriveExplorerApi::to_url(self)
159 }
160
161 fn method(&self) -> HttpMethod {
162 match self {
163 Self::RootFolderMeta
164 | Self::FolderMeta(_)
165 | Self::File(_)
166 | Self::FileDocs(_)
167 | Self::FileSpreadsheets(_)
168 | Self::FolderChildren(_) => HttpMethod::Get,
169 Self::FileCopy(_) | Self::Folder => HttpMethod::Post,
170 }
171 }
172
173 }
175
176fn simple_url_encode(input: &str) -> String {
178 input
179 .chars()
180 .map(|c| match c {
181 'A'..='Z' | 'a'..='z' | '0'..='9' | '-' | '_' | '.' | '~' => c.to_string(),
182 _ => format!("%{:02X}", c as u8),
183 })
184 .collect()
185}
186
187#[derive(Debug, Clone, PartialEq)]
190#[cfg_attr(test, derive(strum_macros::EnumIter))]
191pub enum PermissionApi {
192 MemberPermitted,
194 MemberTransfer,
196 Public,
198}
199
200impl PermissionApi {
201 pub fn to_url(&self) -> String {
203 match self {
204 PermissionApi::MemberPermitted => {
205 "/open-apis/drive/v1/permission/member/permitted".to_string()
206 }
207 PermissionApi::MemberTransfer => {
208 "/open-apis/drive/v1/permission/member/transfer".to_string()
209 }
210 PermissionApi::Public => "/open-apis/drive/v1/permission/v2/public/".to_string(),
211 }
212 }
213
214 pub fn to_request<R>(&self) -> ApiRequest<R> {
216 <Self as CatalogEndpoint>::to_request(self)
217 }
218}
219
220impl CatalogEndpoint for PermissionApi {
221 fn to_url(&self) -> String {
222 PermissionApi::to_url(self)
223 }
224
225 fn method(&self) -> HttpMethod {
226 HttpMethod::Post
227 }
228
229 }
231
232#[derive(Debug, Clone, PartialEq)]
235#[cfg_attr(test, derive(strum_macros::EnumIter))]
236pub enum PermissionApiOld {
237 MemberPermitted,
239 MemberTransfer,
241 Public,
243}
244
245impl PermissionApiOld {
246 pub fn to_url(&self) -> String {
248 match self {
249 PermissionApiOld::MemberPermitted => {
250 "/open-apis/drive/v1/permission/member/permitted".to_string()
251 }
252 PermissionApiOld::MemberTransfer => {
253 "/open-apis/drive/v1/permission/member/transfer".to_string()
254 }
255 PermissionApiOld::Public => "/open-apis/drive/v1/permission/v2/public/".to_string(),
256 }
257 }
258
259 pub fn to_request<R>(&self) -> ApiRequest<R> {
261 <Self as CatalogEndpoint>::to_request(self)
262 }
263}
264
265impl CatalogEndpoint for PermissionApiOld {
266 fn to_url(&self) -> String {
267 PermissionApiOld::to_url(self)
268 }
269
270 fn method(&self) -> HttpMethod {
271 HttpMethod::Post
272 }
273
274 }
276
277#[derive(Debug, Clone, PartialEq)]
279#[cfg_attr(test, derive(strum_macros::EnumIter))]
280pub enum DriveApi {
281 ListFiles,
284 CreateFolder,
286 TaskCheck,
288 BatchQueryMetas,
290 GetFileStatistics(String), ListFileViewRecords(String), CopyFile(String), MoveFile(String), DeleteFile(String), CreateShortcut,
302 UploadFile,
304 UploadPrepare,
306 UploadPart,
308 UploadFinish,
310 DownloadFile(String), CreateImportTask,
314 GetImportTask(String), CreateExportTask,
318 GetExportTask(String), DownloadExportFile(String), UploadMedia,
324 UploadMediaPrepare,
326 UploadMediaPart,
328 UploadMediaFinish,
330 DownloadMedia(String), GetMediaTempDownloadUrls,
334 CreateFileVersion(String), ListFileVersions(String), GetFileVersion(String, String), DeleteFileVersion(String, String), SubscribeFile(String), GetFileSubscribe(String), DeleteFileSubscribe(String), CreatePermissionMember(String), BatchCreatePermissionMember(String), UpdatePermissionMember(String, String), ListPermissionMembers(String), DeletePermissionMember(String, String), TransferOwner(String), AuthPermissionMember(String), UpdatePublicPermission(String), GetPublicPermission(String), CreatePublicPassword(String), UpdatePublicPassword(String), DeletePublicPassword(String), ListFileComments(String), BatchQueryComments(String), PatchComment(String, String), CreateComment(String), GetComment(String, String), ListCommentReplies(String, String), CreateCommentReply(String, String), UpdateCommentReply(String, String, String), DeleteCommentReply(String, String, String), UserSubscription,
392 UserRemoveSubscription,
394 UserSubscriptionStatus,
396 GetFileSubscription(String, String), CreateFileSubscription(String), UpdateFileSubscription(String, String), ListFileLikes(String), GetPublicPermissionV2(String), UpdatePublicPermissionV2(String), UpdateCommentReaction(String), MediaUploadTasks,
416 MediaUploadTask(String), CreateMediaShareLink(String), GetPublicPassword(String), }
423
424impl DriveApi {
425 pub fn to_url(&self) -> String {
427 match self {
428 DriveApi::ListFiles => "/open-apis/drive/v1/files".to_string(),
430 DriveApi::CreateFolder => "/open-apis/drive/v1/files/create_folder".to_string(),
431 DriveApi::TaskCheck => "/open-apis/drive/v1/files/task_check".to_string(),
432 DriveApi::BatchQueryMetas => "/open-apis/drive/v1/metas/batch_query".to_string(),
433 DriveApi::GetFileStatistics(file_token) => {
434 format!("/open-apis/drive/v1/files/{file_token}/statistics")
435 }
436 DriveApi::ListFileViewRecords(file_token) => {
437 format!("/open-apis/drive/v1/files/{file_token}/view_records")
438 }
439 DriveApi::CopyFile(file_token) => {
440 format!("/open-apis/drive/v1/files/{file_token}/copy")
441 }
442 DriveApi::MoveFile(file_token) => {
443 format!("/open-apis/drive/v1/files/{file_token}/move")
444 }
445 DriveApi::DeleteFile(file_token) => {
446 format!("/open-apis/drive/v1/files/{file_token}")
447 }
448 DriveApi::CreateShortcut => "/open-apis/drive/v1/files/create_shortcut".to_string(),
449 DriveApi::UploadFile => "/open-apis/drive/v1/files/upload_all".to_string(),
450 DriveApi::UploadPrepare => "/open-apis/drive/v1/files/upload_prepare".to_string(),
451 DriveApi::UploadPart => "/open-apis/drive/v1/files/upload_part".to_string(),
452 DriveApi::UploadFinish => "/open-apis/drive/v1/files/upload_finish".to_string(),
453 DriveApi::DownloadFile(file_token) => {
454 format!("/open-apis/drive/v1/files/{file_token}/download")
455 }
456
457 DriveApi::CreateImportTask => "/open-apis/drive/v1/import_tasks".to_string(),
459 DriveApi::GetImportTask(ticket) => {
460 format!("/open-apis/drive/v1/import_tasks/{ticket}")
461 }
462 DriveApi::CreateExportTask => "/open-apis/drive/v1/export_tasks".to_string(),
463 DriveApi::GetExportTask(ticket) => {
464 format!("/open-apis/drive/v1/export_tasks/{ticket}")
465 }
466 DriveApi::DownloadExportFile(file_token) => {
467 format!("/open-apis/drive/v1/export_tasks/file/{file_token}/download")
468 }
469
470 DriveApi::UploadMedia => "/open-apis/drive/v1/medias/upload_all".to_string(),
472 DriveApi::UploadMediaPrepare => "/open-apis/drive/v1/medias/upload_prepare".to_string(),
473 DriveApi::UploadMediaPart => "/open-apis/drive/v1/medias/upload_part".to_string(),
474 DriveApi::UploadMediaFinish => "/open-apis/drive/v1/medias/upload_finish".to_string(),
475 DriveApi::DownloadMedia(file_token) => {
476 format!("/open-apis/drive/v1/medias/{file_token}/download")
477 }
478 DriveApi::GetMediaTempDownloadUrls => {
479 "/open-apis/drive/v1/medias/batch_get_tmp_download_url".to_string()
480 }
481
482 DriveApi::CreateFileVersion(file_token) => {
484 format!("/open-apis/drive/v1/files/{file_token}/versions")
485 }
486 DriveApi::ListFileVersions(file_token) => {
487 format!("/open-apis/drive/v1/files/{file_token}/versions")
488 }
489 DriveApi::GetFileVersion(file_token, version_id) => {
490 format!("/open-apis/drive/v1/files/{file_token}/versions/{version_id}")
491 }
492 DriveApi::DeleteFileVersion(file_token, version_id) => {
493 format!("/open-apis/drive/v1/files/{file_token}/versions/{version_id}")
494 }
495
496 DriveApi::SubscribeFile(file_token) => {
498 format!("/open-apis/drive/v1/files/{file_token}/subscribe")
499 }
500 DriveApi::GetFileSubscribe(file_token) => {
501 format!("/open-apis/drive/v1/files/{file_token}/get_subscribe")
502 }
503 DriveApi::DeleteFileSubscribe(file_token) => {
504 format!("/open-apis/drive/v1/files/{file_token}/delete_subscribe")
505 }
506
507 DriveApi::CreatePermissionMember(token) => {
509 format!("/open-apis/drive/v1/permissions/{token}/members")
510 }
511 DriveApi::BatchCreatePermissionMember(token) => {
512 format!("/open-apis/drive/v1/permissions/{token}/members/batch_create")
513 }
514 DriveApi::UpdatePermissionMember(token, member_id) => {
515 format!("/open-apis/drive/v1/permissions/{token}/members/{member_id}")
516 }
517 DriveApi::ListPermissionMembers(token) => {
518 format!("/open-apis/drive/v1/permissions/{token}/members")
519 }
520 DriveApi::DeletePermissionMember(token, member_id) => {
521 format!("/open-apis/drive/v1/permissions/{token}/members/{member_id}")
522 }
523 DriveApi::TransferOwner(token) => {
524 format!("/open-apis/drive/v1/permissions/{token}/members/transfer_owner")
525 }
526 DriveApi::AuthPermissionMember(token) => {
527 format!("/open-apis/drive/v1/permissions/{token}/members/auth")
528 }
529
530 DriveApi::UpdatePublicPermission(token) => {
532 format!("/open-apis/drive/v1/permissions/{token}/public")
533 }
534 DriveApi::GetPublicPermission(token) => {
535 format!("/open-apis/drive/v1/permissions/{token}/public")
536 }
537 DriveApi::CreatePublicPassword(token) => {
538 format!("/open-apis/drive/v1/permissions/{token}/public/password")
539 }
540 DriveApi::UpdatePublicPassword(token) => {
541 format!("/open-apis/drive/v1/permissions/{token}/public/password")
542 }
543 DriveApi::DeletePublicPassword(token) => {
544 format!("/open-apis/drive/v1/permissions/{token}/public/password")
545 }
546
547 DriveApi::ListFileComments(file_token) => {
549 format!("/open-apis/drive/v1/files/{file_token}/comments")
550 }
551 DriveApi::BatchQueryComments(file_token) => {
552 format!("/open-apis/drive/v1/files/{file_token}/comments/batch_query")
553 }
554 DriveApi::PatchComment(file_token, comment_id) => {
555 format!("/open-apis/drive/v1/files/{file_token}/comments/{comment_id}")
556 }
557 DriveApi::CreateComment(file_token) => {
558 format!("/open-apis/drive/v1/files/{file_token}/comments")
559 }
560 DriveApi::GetComment(file_token, comment_id) => {
561 format!("/open-apis/drive/v1/files/{file_token}/comments/{comment_id}")
562 }
563 DriveApi::ListCommentReplies(file_token, comment_id) => {
564 format!("/open-apis/drive/v1/files/{file_token}/comments/{comment_id}/replies")
565 }
566 DriveApi::CreateCommentReply(file_token, comment_id) => {
567 format!("/open-apis/drive/v1/files/{file_token}/comments/{comment_id}/replies")
568 }
569 DriveApi::UpdateCommentReply(file_token, comment_id, reply_id) => {
570 format!(
571 "/open-apis/drive/v1/files/{file_token}/comments/{comment_id}/replies/{reply_id}"
572 )
573 }
574 DriveApi::DeleteCommentReply(file_token, comment_id, reply_id) => {
575 format!(
576 "/open-apis/drive/v1/files/{file_token}/comments/{comment_id}/replies/{reply_id}"
577 )
578 }
579 DriveApi::UserSubscription => "/open-apis/drive/v1/user/subscription".to_string(),
580 DriveApi::UserRemoveSubscription => {
581 "/open-apis/drive/v1/user/remove_subscription".to_string()
582 }
583 DriveApi::UserSubscriptionStatus => {
584 "/open-apis/drive/v1/user/subscription_status".to_string()
585 }
586
587 DriveApi::GetFileSubscription(file_token, subscription_id) => {
589 format!("/open-apis/drive/v1/files/{file_token}/subscriptions/{subscription_id}")
590 }
591 DriveApi::CreateFileSubscription(file_token) => {
592 format!("/open-apis/drive/v1/files/{file_token}/subscriptions")
593 }
594 DriveApi::UpdateFileSubscription(file_token, subscription_id) => {
595 format!("/open-apis/drive/v1/files/{file_token}/subscriptions/{subscription_id}")
596 }
597
598 DriveApi::ListFileLikes(file_token) => {
600 format!("/open-apis/drive/v2/files/{file_token}/likes")
601 }
602 DriveApi::GetPublicPermissionV2(token) => {
603 format!("/open-apis/drive/v2/permissions/{token}/public")
604 }
605 DriveApi::UpdatePublicPermissionV2(token) => {
606 format!("/open-apis/drive/v2/permissions/{token}/public")
607 }
608 DriveApi::UpdateCommentReaction(file_token) => {
609 format!("/open-apis/drive/v2/files/{file_token}/comments/reaction")
610 }
611
612 DriveApi::MediaUploadTasks => "/open-apis/drive/v1/medias/upload_tasks".to_string(),
614 DriveApi::MediaUploadTask(task_id) => {
615 format!("/open-apis/drive/v1/medias/upload_tasks/{task_id}")
616 }
617 DriveApi::CreateMediaShareLink(file_token) => {
618 format!("/open-apis/drive/v1/medias/{file_token}/share_link")
619 }
620 DriveApi::GetPublicPassword(file_token) => {
621 format!("/open-apis/drive/v1/publics/{file_token}/password")
622 }
623 }
624 }
625
626 pub fn to_request<R>(&self) -> ApiRequest<R> {
628 <Self as CatalogEndpoint>::to_request(self)
629 }
630
631 pub fn to_request_with_url<R>(&self, url: impl Into<String>) -> ApiRequest<R> {
633 <Self as CatalogEndpoint>::to_request_with_url(self, url)
634 }
635}
636
637impl CatalogEndpoint for DriveApi {
638 fn to_url(&self) -> String {
639 DriveApi::to_url(self)
640 }
641
642 fn method(&self) -> HttpMethod {
643 match self {
644 Self::ListFiles
645 | Self::TaskCheck
646 | Self::GetFileStatistics(_)
647 | Self::ListFileViewRecords(_)
648 | Self::GetImportTask(_)
649 | Self::GetExportTask(_)
650 | Self::DownloadFile(_)
651 | Self::DownloadExportFile(_)
652 | Self::DownloadMedia(_)
653 | Self::GetMediaTempDownloadUrls
654 | Self::ListFileVersions(_)
655 | Self::GetFileVersion(_, _)
656 | Self::GetFileSubscribe(_)
657 | Self::ListPermissionMembers(_)
658 | Self::AuthPermissionMember(_)
659 | Self::GetPublicPermission(_)
660 | Self::ListFileComments(_)
661 | Self::GetComment(_, _)
662 | Self::ListCommentReplies(_, _)
663 | Self::GetFileSubscription(_, _)
664 | Self::ListFileLikes(_)
665 | Self::GetPublicPermissionV2(_)
666 | Self::MediaUploadTask(_)
667 | Self::GetPublicPassword(_) => HttpMethod::Get,
668 Self::UserSubscriptionStatus => HttpMethod::Get,
669 Self::CreateFolder
670 | Self::CopyFile(_)
671 | Self::MoveFile(_)
672 | Self::CreateShortcut
673 | Self::UploadFile
674 | Self::UploadPrepare
675 | Self::UploadPart
676 | Self::UploadFinish
677 | Self::CreateImportTask
678 | Self::CreateExportTask
679 | Self::UploadMedia
680 | Self::UploadMediaPrepare
681 | Self::UploadMediaPart
682 | Self::UploadMediaFinish
683 | Self::CreateFileVersion(_)
684 | Self::SubscribeFile(_)
685 | Self::CreatePermissionMember(_)
686 | Self::BatchCreatePermissionMember(_)
687 | Self::TransferOwner(_)
688 | Self::CreatePublicPassword(_)
689 | Self::CreateComment(_)
690 | Self::CreateCommentReply(_, _)
691 | Self::UserSubscription
692 | Self::UpdateCommentReaction(_)
693 | Self::CreateFileSubscription(_)
694 | Self::MediaUploadTasks
695 | Self::CreateMediaShareLink(_)
696 | Self::BatchQueryMetas
697 | Self::BatchQueryComments(_) => HttpMethod::Post,
698 Self::UpdatePermissionMember(_, _)
699 | Self::UpdatePublicPassword(_)
700 | Self::UpdateCommentReply(_, _, _) => HttpMethod::Put,
701 Self::UpdatePublicPermission(_)
702 | Self::PatchComment(_, _)
703 | Self::UpdateFileSubscription(_, _)
704 | Self::UpdatePublicPermissionV2(_) => HttpMethod::Patch,
705 Self::DeleteFile(_)
706 | Self::DeleteFileVersion(_, _)
707 | Self::DeleteFileSubscribe(_)
708 | Self::DeletePermissionMember(_, _)
709 | Self::DeletePublicPassword(_)
710 | Self::DeleteCommentReply(_, _, _) => HttpMethod::Delete,
711 Self::UserRemoveSubscription => HttpMethod::Delete,
712 }
713 }
714
715 }
717
718#[cfg(test)]
719mod tests {
720 use super::*;
721 use crate::common::api_endpoints::test_support::catalog_semantics_snapshot;
722
723 #[test]
724 fn drive_catalog_semantics_snapshots() {
725 insta::assert_snapshot!(
726 "ccm_drive_explorer_old_catalog_semantics",
727 catalog_semantics_snapshot::<CcmDriveExplorerApiOld>()
728 );
729 insta::assert_snapshot!(
730 "ccm_drive_explorer_catalog_semantics",
731 catalog_semantics_snapshot::<CcmDriveExplorerApi>()
732 );
733 insta::assert_snapshot!(
734 "permission_catalog_semantics",
735 catalog_semantics_snapshot::<PermissionApi>()
736 );
737 insta::assert_snapshot!(
738 "permission_old_catalog_semantics",
739 catalog_semantics_snapshot::<PermissionApiOld>()
740 );
741 insta::assert_snapshot!(
742 "drive_catalog_semantics",
743 catalog_semantics_snapshot::<DriveApi>()
744 );
745 }
746}