mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
Set up skeleton code
This commit is contained in:
parent
9da6b6c31f
commit
58748471a4
2 changed files with 26 additions and 13 deletions
|
@ -154,7 +154,7 @@ public final class SnodeAPI : NSObject {
|
|||
let (promise, seal) = Promise<Snode>.pending()
|
||||
Threading.workQueue.async {
|
||||
attempt(maxRetryCount: 4, recoveringOn: Threading.workQueue) {
|
||||
HTTP.execute(.post, url, parameters: parameters, useSSLURLSession: true).map2 { json -> Snode in
|
||||
HTTP.execute(.post, url, parameters: parameters, useSeedNodeURLSession: true).map2 { json -> Snode in
|
||||
guard let intermediate = json["result"] as? JSON, let rawSnodes = intermediate["service_node_states"] as? [JSON] else { throw Error.snodePoolUpdatingFailed }
|
||||
let snodePool: Set<Snode> = Set(rawSnodes.compactMap { rawSnode in
|
||||
guard let address = rawSnode["public_ip"] as? String, let port = rawSnode["storage_port"] as? Int,
|
||||
|
|
|
@ -2,15 +2,28 @@ import Foundation
|
|||
import PromiseKit
|
||||
|
||||
public enum HTTP {
|
||||
private static let sslURLSession = URLSession(configuration: .ephemeral)
|
||||
private static let defaultURLSession = URLSession(configuration: .ephemeral, delegate: defaultURLSessionDelegate, delegateQueue: nil)
|
||||
private static let defaultURLSessionDelegate = DefaultURLSessionDelegateImplementation()
|
||||
private static let seedNodeURLSession = URLSession(configuration: .ephemeral, delegate: seedNodeURLSessionDelegate, delegateQueue: nil)
|
||||
private static let seedNodeURLSessionDelegate = SeedNodeURLSessionDelegateImplementation()
|
||||
private static let snodeURLSession = URLSession(configuration: .ephemeral, delegate: snodeURLSessionDelegate, delegateQueue: nil)
|
||||
private static let snodeURLSessionDelegate = SnodeURLSessionDelegateImplementation()
|
||||
|
||||
// MARK: Settings
|
||||
public static let timeout: TimeInterval = 10
|
||||
|
||||
// MARK: URL Session Delegate Implementation
|
||||
private final class DefaultURLSessionDelegateImplementation : NSObject, URLSessionDelegate {
|
||||
// MARK: Seed Node URL Session Delegate Implementation
|
||||
private final class SeedNodeURLSessionDelegateImplementation : NSObject, URLSessionDelegate {
|
||||
|
||||
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
|
||||
|
||||
// TODO: Implement
|
||||
|
||||
// Snode to snode communication uses self-signed certificates but clients can safely ignore this
|
||||
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Snode URL Session Delegate Implementation
|
||||
private final class SnodeURLSessionDelegateImplementation : NSObject, URLSessionDelegate {
|
||||
|
||||
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
|
||||
// Snode to snode communication uses self-signed certificates but clients can safely ignore this
|
||||
|
@ -42,32 +55,32 @@ public enum HTTP {
|
|||
}
|
||||
|
||||
// MARK: Main
|
||||
public static func execute(_ verb: Verb, _ url: String, timeout: TimeInterval = HTTP.timeout, useSSLURLSession: Bool = false) -> Promise<JSON> {
|
||||
return execute(verb, url, body: nil, timeout: timeout, useSSLURLSession: useSSLURLSession)
|
||||
public static func execute(_ verb: Verb, _ url: String, timeout: TimeInterval = HTTP.timeout, useSeedNodeURLSession: Bool = false) -> Promise<JSON> {
|
||||
return execute(verb, url, body: nil, timeout: timeout, useSeedNodeURLSession: useSeedNodeURLSession)
|
||||
}
|
||||
|
||||
public static func execute(_ verb: Verb, _ url: String, parameters: JSON?, timeout: TimeInterval = HTTP.timeout, useSSLURLSession: Bool = false) -> Promise<JSON> {
|
||||
public static func execute(_ verb: Verb, _ url: String, parameters: JSON?, timeout: TimeInterval = HTTP.timeout, useSeedNodeURLSession: Bool = false) -> Promise<JSON> {
|
||||
if let parameters = parameters {
|
||||
do {
|
||||
guard JSONSerialization.isValidJSONObject(parameters) else { return Promise(error: Error.invalidJSON) }
|
||||
let body = try JSONSerialization.data(withJSONObject: parameters, options: [ .fragmentsAllowed ])
|
||||
return execute(verb, url, body: body, timeout: timeout, useSSLURLSession: useSSLURLSession)
|
||||
return execute(verb, url, body: body, timeout: timeout, useSeedNodeURLSession: useSeedNodeURLSession)
|
||||
} catch (let error) {
|
||||
return Promise(error: error)
|
||||
}
|
||||
} else {
|
||||
return execute(verb, url, body: nil, timeout: timeout, useSSLURLSession: useSSLURLSession)
|
||||
return execute(verb, url, body: nil, timeout: timeout, useSeedNodeURLSession: useSeedNodeURLSession)
|
||||
}
|
||||
}
|
||||
|
||||
public static func execute(_ verb: Verb, _ url: String, body: Data?, timeout: TimeInterval = HTTP.timeout, useSSLURLSession: Bool = false) -> Promise<JSON> {
|
||||
public static func execute(_ verb: Verb, _ url: String, body: Data?, timeout: TimeInterval = HTTP.timeout, useSeedNodeURLSession: Bool = false) -> Promise<JSON> {
|
||||
var request = URLRequest(url: URL(string: url)!)
|
||||
request.httpMethod = verb.rawValue
|
||||
request.httpBody = body
|
||||
request.timeoutInterval = timeout
|
||||
request.allHTTPHeaderFields?.removeValue(forKey: "User-Agent")
|
||||
let (promise, seal) = Promise<JSON>.pending()
|
||||
let urlSession = useSSLURLSession ? sslURLSession : defaultURLSession
|
||||
let urlSession = useSeedNodeURLSession ? seedNodeURLSession : snodeURLSession
|
||||
let task = urlSession.dataTask(with: request) { data, response, error in
|
||||
guard let data = data, let response = response as? HTTPURLResponse else {
|
||||
if let error = error {
|
||||
|
|
Loading…
Reference in a new issue