Skip to content

Commit

Permalink
refactor: Remove redondant data when using api fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Feb 27, 2024
1 parent 12998af commit 4d72adb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 50 deletions.
6 changes: 3 additions & 3 deletions MailCore/API/MailApiFetcher/MailApiFetcher+AI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public extension MailApiFetcher {
.ai(mailbox: mailbox),
method: .post,
parameters: AIConversationRequest(messages: messages, output: output, engine: engine)
)).data
))
}

func aiShortcut(contextId: String, shortcut: AIShortcutAction, engine: AIEngine,
Expand All @@ -69,7 +69,7 @@ public extension MailApiFetcher {
.aiShortcut(contextId: contextId, shortcut: shortcut.apiName, mailbox: mailbox),
method: .patch,
parameters: AIShortcutRequest(engine: engine)
)).data
))
}

func aiShortcutAndRecreateConversation(
Expand All @@ -83,6 +83,6 @@ public extension MailApiFetcher {
.aiShortcut(shortcut: shortcut.apiName, mailbox: mailbox),
method: .post,
parameters: AIConversationRequest(messages: messages, output: output, engine: engine)
)).data
))
}
}
8 changes: 4 additions & 4 deletions MailCore/API/MailApiFetcher/MailApiFetcher+Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public extension MailApiFetcher {
return try await perform(request: authenticatedRequest(.resource(
resource,
queryItems: [URLQueryItem(name: "format", value: "render")]
))).data
)))
}

@discardableResult
Expand All @@ -40,7 +40,7 @@ public extension MailApiFetcher {
.replyToCalendarEvent(resource: resource),
method: .post,
parameters: replyRequest
)).data
))
}

@discardableResult
Expand All @@ -50,7 +50,7 @@ public extension MailApiFetcher {
.replyToCalendarEventAndUpdateCalendar(id: event.id),
method: .post,
parameters: replyRequest
)).data
))
}

@discardableResult
Expand All @@ -59,6 +59,6 @@ public extension MailApiFetcher {
throw MailError.resourceError
}

return try await perform(request: authenticatedRequest(.importICSEventToCalendar(resource: resource), method: .post)).data
return try await perform(request: authenticatedRequest(.importICSEventToCalendar(resource: resource), method: .post))
}
}
38 changes: 19 additions & 19 deletions MailCore/API/MailApiFetcher/MailApiFetcher+Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public extension MailApiFetcher {
// MARK: - API methods

func mailboxes() async throws -> [Mailbox] {
try await perform(request: authenticatedRequest(.mailboxes)).data
try await perform(request: authenticatedRequest(.mailboxes))
}

@discardableResult
Expand All @@ -35,7 +35,7 @@ public extension MailApiFetcher {
.addMailbox,
method: .post,
parameters: ["mail": mail, "password": password, "is_primary": false]
)).data
))
}

@discardableResult
Expand All @@ -44,31 +44,30 @@ public extension MailApiFetcher {
.updateMailboxPassword(mailboxId: mailbox.mailboxId),
method: .put,
parameters: ["password": password]
)).data
))
}

@discardableResult
func askMailboxPassword(mailbox: Mailbox) async throws -> Bool {
try await perform(request: authenticatedRequest(
.askMailboxPassword(hostingId: mailbox.hostingId, mailboxName: mailbox.mailbox),
method: .post
)).data
))
}

func detachMailbox(mailbox: Mailbox) async throws -> Bool {
try await perform(request: authenticatedRequest(.detachMailbox(mailboxId: mailbox.mailboxId), method: .delete)).data
try await perform(request: authenticatedRequest(.detachMailbox(mailboxId: mailbox.mailboxId), method: .delete))
}

func listBackups(mailbox: Mailbox) async throws -> BackupsList {
try await perform(request: authenticatedRequest(.backups(hostingId: mailbox.hostingId, mailboxName: mailbox.mailbox)))
.data
}

@discardableResult
func restoreBackup(mailbox: Mailbox, date: String) async throws -> Bool {
try await perform(request: authenticatedRequest(.backups(hostingId: mailbox.hostingId, mailboxName: mailbox.mailbox),
method: .put,
parameters: ["date": date])).data
parameters: ["date": date]))
}

func threads(mailbox: Mailbox, folderId: String, filter: Filter = .all,
Expand All @@ -79,11 +78,11 @@ public extension MailApiFetcher {
filter: filter == .all ? nil : filter.rawValue,
searchFilters: searchFilter,
isDraftFolder: isDraftFolder
))).data
)))
}

func threads(from resource: String, searchFilter: [URLQueryItem] = []) async throws -> ThreadResult {
try await perform(request: authenticatedRequest(.resource(resource, queryItems: searchFilter))).data
try await perform(request: authenticatedRequest(.resource(resource, queryItems: searchFilter)))
}

func download(message: Message) async throws -> URL {
Expand All @@ -96,24 +95,24 @@ public extension MailApiFetcher {
}

func quotas(mailbox: Mailbox) async throws -> Quotas {
try await perform(request: authenticatedRequest(.quotas(mailbox: mailbox.mailbox, productId: mailbox.hostingId))).data
try await perform(request: authenticatedRequest(.quotas(mailbox: mailbox.mailbox, productId: mailbox.hostingId)))
}

@discardableResult
func undoAction(resource: String) async throws -> Bool {
try await perform(request: authenticatedRequest(.resource(resource), method: .post)).data
try await perform(request: authenticatedRequest(.resource(resource), method: .post))
}

func star(mailbox: Mailbox, messages: [Message]) async throws -> MessageActionResult {
try await perform(request: authenticatedRequest(.star(uuid: mailbox.uuid),
method: .post,
parameters: ["uids": messages.map(\.uid)])).data
parameters: ["uids": messages.map(\.uid)]))
}

func unstar(mailbox: Mailbox, messages: [Message]) async throws -> MessageActionResult {
try await perform(request: authenticatedRequest(.unstar(uuid: mailbox.uuid),
method: .post,
parameters: ["uids": messages.map(\.uid)])).data
parameters: ["uids": messages.map(\.uid)]))
}

func downloadAttachments(message: Message) async throws -> URL {
Expand All @@ -130,17 +129,17 @@ public extension MailApiFetcher {

@discardableResult
func blockSender(message: Message) async throws -> NullableResponse {
try await perform(request: authenticatedRequest(.blockSender(messageResource: message.resource), method: .post)).data
try await perform(request: authenticatedRequest(.blockSender(messageResource: message.resource), method: .post))
}

func reportPhishing(message: Message) async throws -> Bool {
try await perform(request: authenticatedRequest(.report(messageResource: message.resource),
method: .post,
parameters: ["type": "phishing"])).data
parameters: ["type": "phishing"]))
}

func create(mailbox: Mailbox, folder: NewFolder) async throws -> Folder {
try await perform(request: authenticatedRequest(.folders(uuid: mailbox.uuid), method: .post, parameters: folder)).data
try await perform(request: authenticatedRequest(.folders(uuid: mailbox.uuid), method: .post, parameters: folder))
}

func createAttachment(
Expand All @@ -164,12 +163,13 @@ public extension MailApiFetcher {
}
}

return try await perform(request: uploadRequest).data
return try await perform(request: uploadRequest)
}

func attachmentsToForward(mailbox: Mailbox, message: Message) async throws -> AttachmentsToForwardResult {
let attachmentsToForward = AttachmentsToForward(toForwardUids: [message.uid], mode: AttachmentDisposition.inline.rawValue)
return try await perform(request: authenticatedRequest(.attachmentToForward(uuid: mailbox.uuid), method: .post,
parameters: attachmentsToForward)).data
return try await perform(request: authenticatedRequest(.attachmentToForward(uuid: mailbox.uuid),
method: .post,
parameters: attachmentsToForward))
}
}
43 changes: 20 additions & 23 deletions MailCore/API/MailApiFetcher/MailApiFetcher+Extended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,30 @@ import InfomaniakLogin
/// implementing `MailApiExtendedFetchable`
public extension MailApiFetcher {
func permissions(mailbox: Mailbox) async throws -> MailboxPermissions {
try await perform(request: authenticatedRequest(.permissions(mailbox: mailbox))).data
try await perform(request: authenticatedRequest(.permissions(mailbox: mailbox)))
}

func featureFlag(_ mailboxUUID: String) async throws -> [FeatureFlag] {
try await perform(request: authenticatedRequest(.featureFlag(mailboxUUID)))
.data
}

func contacts() async throws -> [InfomaniakContact] {
try await perform(request: authenticatedRequest(.contacts)).data
try await perform(request: authenticatedRequest(.contacts))
}

func addressBooks() async throws -> AddressBookResult {
try await perform(request: authenticatedRequest(.addressBooks)).data
try await perform(request: authenticatedRequest(.addressBooks))
}

func addContact(_ recipient: Recipient, to addressBook: AddressBook) async throws -> Int {
try await perform(request: authenticatedSession.request(Endpoint.addContact.url,
method: .post,
parameters: NewContact(from: recipient, addressBook: addressBook),
encoder: JSONParameterEncoder.default)).data
encoder: JSONParameterEncoder.default))
}

func signatures(mailbox: Mailbox) async throws -> SignatureResponse {
try await perform(request: authenticatedRequest(.signatures(hostingId: mailbox.hostingId, mailboxName: mailbox.mailbox)))
.data
}

@discardableResult
Expand All @@ -63,43 +61,43 @@ public extension MailApiFetcher {
),
method: .patch,
parameters: signature
)).data
))
}

func folders(mailbox: Mailbox) async throws -> [Folder] {
try await perform(request: authenticatedRequest(.folders(uuid: mailbox.uuid))).data
try await perform(request: authenticatedRequest(.folders(uuid: mailbox.uuid)))
}

func flushFolder(mailbox: Mailbox, folderId: String) async throws -> Bool {
try await perform(request: authenticatedRequest(.flushFolder(mailboxUuid: mailbox.uuid, folderId: folderId),
method: .post)).data
method: .post))
}

@discardableResult
func markAsSeen(mailbox: Mailbox, messages: [Message]) async throws -> MessageActionResult {
try await perform(request: authenticatedRequest(.messageSeen(uuid: mailbox.uuid),
method: .post,
parameters: ["uids": messages.map(\.uid)])).data
parameters: ["uids": messages.map(\.uid)]))
}

@discardableResult
func markAsUnseen(mailbox: Mailbox, messages: [Message]) async throws -> MessageActionResult {
try await perform(request: authenticatedRequest(.messageUnseen(uuid: mailbox.uuid),
method: .post,
parameters: ["uids": messages.map(\.uid)])).data
parameters: ["uids": messages.map(\.uid)]))
}

func move(mailbox: Mailbox, messages: [Message], destinationId: String) async throws -> UndoResponse {
try await perform(request: authenticatedRequest(.moveMessages(uuid: mailbox.uuid),
method: .post,
parameters: ["uids": messages.map(\.uid), "to": destinationId])).data
parameters: ["uids": messages.map(\.uid), "to": destinationId]))
}

@discardableResult
func delete(mailbox: Mailbox, messages: [Message]) async throws -> Empty {
try await perform(request: authenticatedRequest(.deleteMessages(uuid: mailbox.uuid),
method: .post,
parameters: ["uids": messages.map(\.uid)])).data
parameters: ["uids": messages.map(\.uid)]))
}

func attachment(attachment: Attachment) async throws -> Data {
Expand All @@ -119,23 +117,23 @@ public extension MailApiFetcher {
mailboxUuid: mailboxUuid,
folderId: folderId,
paginationInfo: paginationInfo
))).data
)))
}

func messagesByUids(mailboxUuid: String, folderId: String, messageUids: [String]) async throws -> MessageByUidsResult {
try await perform(request: authenticatedRequest(.messagesByUids(
mailboxUuid: mailboxUuid,
folderId: folderId,
messagesUids: messageUids
))).data
)))
}

func messagesDelta(mailboxUUid: String, folderId: String, signature: String) async throws -> MessageDeltaResult {
try await perform(request: authenticatedRequest(.messagesDelta(
mailboxUuid: mailboxUUid,
folderId: folderId,
signature: signature
))).data
)))
}

func message(message: Message) async throws -> Message {
Expand All @@ -144,46 +142,45 @@ public extension MailApiFetcher {
queryItems: [
URLQueryItem(name: "prefered_format", value: "html")
]
))).data
)))
}

func draft(mailbox: Mailbox, draftUuid: String) async throws -> Draft {
try await perform(request: authenticatedRequest(.draft(uuid: mailbox.uuid, draftUuid: draftUuid))).data
try await perform(request: authenticatedRequest(.draft(uuid: mailbox.uuid, draftUuid: draftUuid)))
}

func draft(from message: Message) async throws -> Draft {
guard let resource = message.draftResource else {
throw MailError.resourceError
}
return try await perform(request: authenticatedRequest(.resource(resource))).data
return try await perform(request: authenticatedRequest(.resource(resource)))
}

func send(mailbox: Mailbox, draft: Draft) async throws -> SendResponse {
try await perform(request: authenticatedRequest(
draft.remoteUUID.isEmpty ? .draft(uuid: mailbox.uuid) : .draft(uuid: mailbox.uuid, draftUuid: draft.remoteUUID),
method: draft.remoteUUID.isEmpty ? .post : .put,
parameters: draft
)).data
))
}

func save(mailbox: Mailbox, draft: Draft) async throws -> DraftResponse {
try await perform(request: authenticatedRequest(
draft.remoteUUID.isEmpty ? .draft(uuid: mailbox.uuid) : .draft(uuid: mailbox.uuid, draftUuid: draft.remoteUUID),
method: draft.remoteUUID.isEmpty ? .post : .put,
parameters: draft
)).data
))
}

@discardableResult
func deleteDraft(mailbox: Mailbox, draftId: String) async throws -> Empty? {
// TODO: Remove try? when bug will be fixed from API
return try? await perform(request: authenticatedRequest(.draft(uuid: mailbox.uuid, draftUuid: draftId), method: .delete))
.data
}

@discardableResult
func deleteDraft(draftResource: String) async throws -> Empty? {
// TODO: Remove try? when bug will be fixed from API
return try? await perform(request: authenticatedRequest(.resource(draftResource), method: .delete)).data
return try? await perform(request: authenticatedRequest(.resource(draftResource), method: .delete))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public extension MailApiFetcher {
.applicationPassword,
method: .post,
parameters: ["name": "CalDAV/CardDAV - \(Constants.appVersion()) - \(Date().ISO8601Format())"]
)).data
))
}
}

0 comments on commit 4d72adb

Please sign in to comment.