session-ios/SessionMessagingKit/Open Groups/Models/SOGSBatchRequest.swift

75 lines
2.1 KiB
Swift
Raw Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
Work on the PromiseKit refactor # Conflicts: # Session.xcodeproj/project.pbxproj # Session/Conversations/ConversationVC+Interaction.swift # Session/Home/Message Requests/MessageRequestsViewModel.swift # Session/Notifications/AppNotifications.swift # Session/Notifications/PushRegistrationManager.swift # Session/Notifications/SyncPushTokensJob.swift # Session/Notifications/UserNotificationsAdaptee.swift # Session/Settings/BlockedContactsViewModel.swift # Session/Settings/NukeDataModal.swift # Session/Settings/SettingsViewModel.swift # Session/Utilities/BackgroundPoller.swift # SessionMessagingKit/Database/Models/ClosedGroup.swift # SessionMessagingKit/File Server/FileServerAPI.swift # SessionMessagingKit/Open Groups/OpenGroupAPI.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ClosedGroups.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+UnsendRequests.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift # SessionMessagingKit/Sending & Receiving/MessageSender+Convenience.swift # SessionMessagingKit/Sending & Receiving/MessageSender.swift # SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Pollers/ClosedGroupPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/Poller.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionSnodeKit/Networking/SnodeAPI.swift # SessionSnodeKit/OnionRequestAPI.swift # SessionUtilitiesKit/Networking/HTTP.swift
2022-11-27 22:32:32 +01:00
import Combine
import SessionUtilitiesKit
public extension OpenGroupAPI {
internal struct BatchRequest: Encodable {
let requests: [Child]
init(requests: [ErasedPreparedSendData]) {
self.requests = requests.map { Child(request: $0) }
}
// MARK: - Encodable
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(requests)
}
// MARK: - BatchRequest.Child
struct Child: Encodable {
enum CodingKeys: String, CodingKey {
case method
case path
case headers
case json
case b64
case bytes
}
let request: ErasedPreparedSendData
func encode(to encoder: Encoder) throws {
try request.encodeForBatchRequest(to: encoder)
}
}
}
struct BatchResponse: Decodable {
let info: ResponseInfoType
let data: [Endpoint: Decodable]
public subscript(position: Endpoint) -> Decodable? {
get { return data[position] }
}
public var count: Int { data.count }
public var keys: Dictionary<Endpoint, Decodable>.Keys { data.keys }
public var values: Dictionary<Endpoint, Decodable>.Values { data.values }
// MARK: - Initialization
internal init(
info: ResponseInfoType,
data: [Endpoint: Decodable]
) {
self.info = info
self.data = data
}
public init(from decoder: Decoder) throws {
#if DEBUG
preconditionFailure("The `OpenGroupAPI.BatchResponse` type cannot be decoded directly, this is simply here to allow for `PreparedSendData<OpenGroupAPI.BatchResponse>` support")
#else
2023-06-26 10:07:54 +02:00
info = HTTP.ResponseInfo(code: 0, headers: [:])
data = [:]
#endif
}
}
}