mirror of https://github.com/oxen-io/session-ios
Removed some unneeded imports
# Conflicts: # SessionMessagingKit/Jobs/Types/AttachmentUploadJob.swift # SessionMessagingKit/Jobs/Types/MessageSendJob.swift # SessionShareExtension/SAEScreenLockViewController.swift # SessionSnodeKit/Models/SnodeBatchRequest.swiftpull/856/head
parent
8ac6b25ced
commit
8b37002d89
@ -0,0 +1,63 @@
|
|||||||
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import SessionUtilitiesKit
|
||||||
|
|
||||||
|
internal extension SnodeAPI {
|
||||||
|
struct BatchRequest: Encodable {
|
||||||
|
let requests: [Child]
|
||||||
|
|
||||||
|
init(requests: [Info]) {
|
||||||
|
self.requests = requests.map { $0.child }
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - BatchRequest.Info
|
||||||
|
|
||||||
|
struct Info {
|
||||||
|
public let responseType: Codable.Type
|
||||||
|
fileprivate let child: Child
|
||||||
|
|
||||||
|
public init<T: Encodable, R: Codable>(request: SnodeRequest<T>, responseType: R.Type) {
|
||||||
|
self.child = Child(request: request)
|
||||||
|
self.responseType = HTTP.BatchSubResponse<R>.self
|
||||||
|
}
|
||||||
|
|
||||||
|
public init<T: Encodable>(request: SnodeRequest<T>) {
|
||||||
|
self.init(
|
||||||
|
request: request,
|
||||||
|
responseType: NoResponse.self
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - BatchRequest.Child
|
||||||
|
|
||||||
|
struct Child: Encodable {
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case method
|
||||||
|
case params
|
||||||
|
}
|
||||||
|
|
||||||
|
let endpoint: SnodeAPI.Endpoint
|
||||||
|
|
||||||
|
/// The `jsonBodyEncoder` is used to avoid having to make `BatchSubRequest` a generic type (haven't found
|
||||||
|
/// a good way to keep `BatchSubRequest` encodable using protocols unfortunately so need this work around)
|
||||||
|
private let jsonBodyEncoder: ((inout KeyedEncodingContainer<CodingKeys>, CodingKeys) throws -> ())?
|
||||||
|
|
||||||
|
init<T: Encodable>(request: SnodeRequest<T>) {
|
||||||
|
self.endpoint = request.endpoint
|
||||||
|
|
||||||
|
self.jsonBodyEncoder = { [body = request.body] container, key in
|
||||||
|
try container.encode(body, forKey: key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func encode(to encoder: Encoder) throws {
|
||||||
|
var container: KeyedEncodingContainer<CodingKeys> = encoder.container(keyedBy: CodingKeys.self)
|
||||||
|
|
||||||
|
try container.encode(endpoint.rawValue, forKey: .method)
|
||||||
|
try jsonBodyEncoder?(&container, .params)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue