Skip to content

Commit

Permalink
Merge pull request #1669 from Infomaniak/sort-api-routes
Browse files Browse the repository at this point in the history
Sort API Routes code
  • Loading branch information
LunarX authored Jan 25, 2024
2 parents 6c73f5c + d7e4b18 commit 9120441
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ object ApiRepository : ApiRepositoryCore() {
}

fun getMailboxes(okHttpClient: OkHttpClient? = null): ApiResponse<List<Mailbox>> {
return callApi(ApiRoutes.mailbox(), GET, okHttpClient = okHttpClient ?: HttpClient.okHttpClient)
return callApi(ApiRoutes.mailboxes(), GET, okHttpClient = okHttpClient ?: HttpClient.okHttpClient)
}

fun addNewMailbox(mailAddress: String, password: String): ApiResponse<MailboxLinkedResult> {
Expand Down
223 changes: 156 additions & 67 deletions app/src/main/java/com/infomaniak/mail/data/api/ApiRoutes.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2022-2023 Infomaniak Network SA
* Copyright (C) 2022-2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -24,134 +24,223 @@ import com.infomaniak.mail.utils.Utils

object ApiRoutes {

fun resource(resource: String) = "$MAIL_API$resource"

fun ping() = "$MAIL_API/api/ping-with-auth"

fun addressBooks() = "$MAIL_API/api/pim/addressbook"

fun contact() = "$MAIL_API/api/pim/contact"

fun contacts() = "${contact()}/all?with=emails,details,others,contacted_times&filters=has_email"

fun mailbox() = "$MAIL_API/api/mailbox?with=unseen,aliases,external_mail_flag_enabled"

fun permissions(linkId: Int, hostingId: Int): String {
return "$MAIL_API/api/mailbox/permissions?user_mailbox_id=$linkId&product_id=$hostingId"
//region API V1
fun getCredentialsPassword(): String {
return "$INFOMANIAK_API_V1/profile/password"
}

private fun ai(): String = "$MAIL_API/api/ai"
private fun mailboxV1(mailboxHostingId: Int, mailboxName: String): String {
return "$INFOMANIAK_API_V1/mail_hostings/$mailboxHostingId/mailboxes/$mailboxName"
}

fun featureFlag(featureName: String, currentMailboxUuid: String): String {
return "$MAIL_API/api/feature-flag/check/$featureName${mailboxUuidParameter(currentMailboxUuid)}"
fun backups(mailboxHostingId: Int, mailboxName: String): String {
return "${mailboxV1(mailboxHostingId, mailboxName)}/backups"
}

private fun apiMailbox(mailboxHostingId: Int, mailboxName: String): String {
return "$INFOMANIAK_API_V1/mail_hostings/$mailboxHostingId/mailboxes/$mailboxName"
fun requestMailboxPassword(mailboxHostingId: Int, mailboxName: String): String {
return "${mailboxV1(mailboxHostingId, mailboxName)}/ask_password"
}

fun signatures(mailboxHostingId: Int, mailboxName: String) = "${apiMailbox(mailboxHostingId, mailboxName)}/signatures"
fun signatures(mailboxHostingId: Int, mailboxName: String): String {
return "${mailboxV1(mailboxHostingId, mailboxName)}/signatures"
}

fun signature(mailboxHostingId: Int, mailboxName: String, signatureId: Int): String {
return "${signatures(mailboxHostingId, mailboxName)}/$signatureId"
}
//endregion

fun manageMailboxes() = "$MAIL_API/api/securedProxy/profile/workspace/mailbox"

fun manageMailbox(mailboxId: Int) = "${manageMailboxes()}/$mailboxId"
//region Personal Information Manager
private fun pim(): String {
return "$MAIL_API/api/pim"
}

fun updateMailboxPassword(mailboxId: Int): String {
return "$MAIL_API/api/securedProxy/cache/invalidation/profile/workspace/mailbox/$mailboxId/update_password"
fun addressBooks(): String {
return "${pim()}/addressbook"
}

fun requestMailboxPassword(mailboxHostingId: Int, mailboxName: String): String {
return "${apiMailbox(mailboxHostingId, mailboxName)}/ask_password"
fun contact(): String {
return "${pim()}/contact"
}

fun backups(mailboxHostingId: Int, mailboxName: String) = "${apiMailbox(mailboxHostingId, mailboxName)}/backups"
fun contacts(): String {
return "${contact()}/all?with=emails,details,others,contacted_times&filters=has_email"
}
//endregion

fun folders(mailboxUuid: String) = "$MAIL_API/api/mail/$mailboxUuid/folder"
//region Secured Proxy
private fun securedProxy(): String {
return "$MAIL_API/api/securedProxy"
}

private fun folder(mailboxUuid: String, folderId: String) = "${folders(mailboxUuid)}/$folderId"
fun updateMailboxPassword(mailboxId: Int): String {
return "${securedProxy()}/cache/invalidation/profile/workspace/mailbox/$mailboxId/update_password"
}

fun flushFolder(mailboxUuid: String, folderId: String) = "${folder(mailboxUuid, folderId)}/flush"
fun manageMailboxes(): String {
return "${securedProxy()}/profile/workspace/mailbox"
}

fun quotas(mailboxHostingId: Int, mailboxName: String): String =
"$MAIL_API/api/mailbox/quotas?mailbox=$mailboxName&product_id=$mailboxHostingId"
fun manageMailbox(mailboxId: Int): String {
return "${manageMailboxes()}/$mailboxId"
}
//endregion

private fun messages(mailboxUuid: String) = "$MAIL_API/api/mail/$mailboxUuid/message"
//region Mailbox
private fun mailbox(): String {
return "$MAIL_API/api/mailbox"
}

private fun message(mailboxUuid: String, folderId: String, shortUid: Int): String {
return "${folder(mailboxUuid, folderId)}/message/$shortUid"
fun mailboxes(): String {
return "${mailbox()}?with=aliases,external_mail_flag_enabled,unseen"
}

fun moveMessages(mailboxUuid: String) = "${messages(mailboxUuid)}/move"
fun permissions(linkId: Int, mailboxHostingId: Int): String {
return "${mailbox()}/permissions?user_mailbox_id=$linkId&product_id=$mailboxHostingId"
}

fun deleteMessages(mailboxUuid: String) = "${messages(mailboxUuid)}/delete"
fun quotas(mailboxHostingId: Int, mailboxName: String): String {
return "${mailbox()}/quotas?mailbox=$mailboxName&product_id=$mailboxHostingId"
}
//endregion

fun messagesSeen(mailboxUuid: String) = "${messages(mailboxUuid)}/seen"
//region Folder
private fun mailMailbox(mailboxUuid: String): String {
return "$MAIL_API/api/mail/$mailboxUuid"
}

fun messagesUnseen(mailboxUuid: String) = "${messages(mailboxUuid)}/unseen"
fun folders(mailboxUuid: String): String {
return "${mailMailbox(mailboxUuid)}/folder"
}

fun starMessages(mailboxUuid: String, star: Boolean): String = "${messages(mailboxUuid)}/${if (star) "star" else "unstar"}"
private fun folder(mailboxUuid: String, folderId: String): String {
return "${folders(mailboxUuid)}/$folderId"
}

fun draft(mailboxUuid: String) = "$MAIL_API/api/mail/$mailboxUuid/draft"
fun flushFolder(mailboxUuid: String, folderId: String): String {
return "${folder(mailboxUuid, folderId)}/flush"
}

fun draft(mailboxUuid: String, remoteDraftUuid: String) = "${draft(mailboxUuid)}/$remoteDraftUuid"
fun search(mailboxUuid: String, folderId: String, filters: String): String {
return "${folder(mailboxUuid, folderId)}/message?thread=on&offset=0&$filters"
}
//endregion

fun createAttachment(mailboxUuid: String) = "${draft(mailboxUuid)}/attachment"
//region Messages from Folder/Message
private fun message(mailboxUuid: String, folderId: String, shortUid: Int): String {
return "${folder(mailboxUuid, folderId)}/message/$shortUid"
}

fun downloadAttachments(mailboxUuid: String, folderId: String, shortUid: Int): String {
return "${message(mailboxUuid, folderId, shortUid)}/attachmentsArchive"
}

fun attachmentToForward(mailboxUuid: String) = "${draft(mailboxUuid)}/attachmentsToForward"

fun search(mailboxUuid: String, folderId: String, filters: String): String {
return "${folder(mailboxUuid, folderId)}/message?thread=on&offset=0&$filters"
fun blockUser(mailboxUuid: String, folderId: String, shortUid: Int): String {
return "${message(mailboxUuid, folderId, shortUid)}/blacklist"
}

fun reportPhishing(mailboxUuid: String, folderId: String, shortUid: Int): String {
return "${message(mailboxUuid, folderId, shortUid)}/report"
}
//endregion

fun blockUser(mailboxUuid: String, folderId: String, shortUid: Int): String {
return "${message(mailboxUuid, folderId, shortUid)}/blacklist"
//region Message from Folder/Mobile
private fun getMessages(mailboxUuid: String, folderId: String): String {
return "${folder(mailboxUuid, folderId)}/mobile"
}

fun getMessagesUidsDelta(mailboxUuid: String, folderId: String, cursor: String): String {
return "${getMessages(mailboxUuid, folderId)}/activities?signature=$cursor"
}

fun getMessagesUids(mailboxUuid: String, folderId: String, info: PaginationInfo?): String {
val endpoint = "${getMessages(mailboxUuid, folderId)}/messages-uids"
val messages = "?messages=${Utils.PAGE_SIZE}"
val endpoint = "${getMessages(mailboxUuid, folderId)}/messages-uids?messages=${Utils.PAGE_SIZE}"
val offset = info?.offsetUid?.let { "&uid_offset=$it" } ?: ""
val direction = info?.direction?.let { "&direction=$it" } ?: ""
return "${endpoint}${messages}${offset}${direction}"
}

fun getMessagesUidsDelta(mailboxUuid: String, folderId: String, cursor: String): String {
return "${getMessages(mailboxUuid, folderId)}/activities?signature=${cursor}"
return "${endpoint}${offset}${direction}"
}

fun getMessagesByUids(mailboxUuid: String, folderId: String, uids: List<Int>): String {
return "${getMessages(mailboxUuid, folderId)}/messages?uids=${uids.joinToString(",")}"
}
//endregion

private fun mailboxUuidParameter(currentMailboxUuid: String) = "?mailbox_uuid=$currentMailboxUuid"
//region Message from Mailbox
private fun messages(mailboxUuid: String): String {
return "${mailMailbox(mailboxUuid)}/message"
}

fun aiConversation(currentMailboxUuid: String): String {
return "${ai()}${mailboxUuidParameter(currentMailboxUuid)}"
fun deleteMessages(mailboxUuid: String): String {
return "${messages(mailboxUuid)}/delete"
}

fun aiShortcutWithContext(contextId: String, action: String, currentMailboxEmail: String): String {
return "${ai()}/mobile/$contextId/$action${mailboxUuidParameter(currentMailboxEmail)}"
fun moveMessages(mailboxUuid: String): String {
return "${messages(mailboxUuid)}/move"
}

fun aiShortcutNoContext(action: String, currentMailboxEmail: String): String {
return "${ai()}/mobile/$action${mailboxUuidParameter(currentMailboxEmail)}"
fun messagesSeen(mailboxUuid: String): String {
return "${messages(mailboxUuid)}/seen"
}

private fun getMessages(mailboxUuid: String, folderId: String): String {
return "$MAIL_API/api/mail/${mailboxUuid}/folder/${folderId}/mobile"
fun messagesUnseen(mailboxUuid: String): String {
return "${messages(mailboxUuid)}/unseen"
}

fun starMessages(mailboxUuid: String, star: Boolean): String {
return "${messages(mailboxUuid)}/${if (star) "star" else "unstar"}"
}
//endregion

//region Draft from Mailbox
fun draft(mailboxUuid: String): String {
return "${mailMailbox(mailboxUuid)}/draft"
}

fun draft(mailboxUuid: String, remoteDraftUuid: String): String {
return "${draft(mailboxUuid)}/$remoteDraftUuid"
}

fun getCredentialsPassword(): String = "$INFOMANIAK_API_V1/profile/password"
fun createAttachment(mailboxUuid: String): String {
return "${draft(mailboxUuid)}/attachment"
}

fun attachmentToForward(mailboxUuid: String): String {
return "${draft(mailboxUuid)}/attachmentsToForward"
}
//endregion

//region AI
private fun ai(): String {
return "$MAIL_API/api/ai"
}

fun aiConversation(mailboxUuid: String): String {
return "${ai()}${mailboxUuidParameter(mailboxUuid)}"
}

fun aiShortcutWithContext(contextId: String, action: String, mailboxUuid: String): String {
return "${ai()}/mobile/$contextId/${action}${mailboxUuidParameter(mailboxUuid)}"
}

fun aiShortcutNoContext(action: String, mailboxUuid: String): String {
return "${ai()}/mobile/${action}${mailboxUuidParameter(mailboxUuid)}"
}
//endregion

private fun mailboxUuidParameter(mailboxUuid: String): String {
return "?mailbox_uuid=$mailboxUuid"
}

fun featureFlag(featureName: String, mailboxUuid: String): String {
return "$MAIL_API/api/feature-flag/check/${featureName}${mailboxUuidParameter(mailboxUuid)}"
}

fun ping(): String {
return "$MAIL_API/api/ping-with-auth"
}

fun resource(resource: String): String {
return "${MAIL_API}${resource}"
}
}

0 comments on commit 9120441

Please sign in to comment.