// C o p y r i g h t © 2 0 2 2 R a n g e p r o o f P t y L t d . A l l r i g h t s r e s e r v e d .
import Foundation
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: - E n c o d a b l e
func encode ( to encoder : Encoder ) throws {
var container = encoder . singleValueContainer ( )
try container . encode ( requests )
}
// MARK: - B a t c h R e q u e s t . C h i l d
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: - I n i t i a l i z a t i o n
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
info = HTTP . ResponseInfo ( code : 0 , headers : [ : ] )
data = [ : ]
#endif
}
}
}