2022-11-28 07:46:08 +01:00
// 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
2022-11-27 22:32:32 +01:00
import Combine
2022-11-28 07:46:08 +01:00
import SessionUtilitiesKit
2023-06-26 10:03:40 +02:00
public extension OpenGroupAPI {
internal struct BatchRequest : Encodable {
2022-11-28 07:46:08 +01:00
let requests : [ Child ]
2023-06-26 10:03:40 +02:00
init ( requests : [ ErasedPreparedSendData ] ) {
self . requests = requests . map { Child ( request : $0 ) }
2022-11-28 07:46:08 +01:00
}
2023-06-26 10:03:40 +02:00
// MARK: - E n c o d a b l e
2022-11-28 07:46:08 +01:00
func encode ( to encoder : Encoder ) throws {
var container = encoder . singleValueContainer ( )
2023-06-26 10:03:40 +02:00
2022-11-28 07:46:08 +01:00
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
}
2023-06-26 10:03:40 +02:00
let request : ErasedPreparedSendData
2022-11-28 07:46:08 +01:00
func encode ( to encoder : Encoder ) throws {
2023-06-26 10:03:40 +02:00
try request . encodeForBatchRequest ( to : encoder )
2022-11-28 07:46:08 +01:00
}
}
}
2023-06-26 10:03:40 +02:00
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
2023-06-26 10:07:54 +02:00
info = HTTP . ResponseInfo ( code : 0 , headers : [ : ] )
2023-06-26 10:03:40 +02:00
data = [ : ]
#endif
}
2022-11-28 07:46:08 +01:00
}
}