forked from zetapush/zetapush-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
269 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// ClientHelperError.swift | ||
// | ||
// | ||
// Created by Anthony Guiguen on 22/07/2020. | ||
// | ||
|
||
import Foundation | ||
import CometDClient | ||
|
||
public typealias HandshakeError = CometDClient.HandshakeError | ||
|
||
public enum ClientHelperError: Error { | ||
case connectionFailed, connectionBroken, connectionClosed, handshakeFailed(reason: HandshakeError?), subscription | ||
} |
80 changes: 80 additions & 0 deletions
80
Sources/ZetaPushNetwork/Client/Server/ServerRemoteDataSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// ServerRemoteDataSource.swift | ||
// | ||
// | ||
// Created by Anthony Guiguen on 22/07/2020. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
public struct ServerConfiguration { | ||
var serverUrl: String | ||
let sandboxId: String | ||
var timeout: TimeInterval | ||
} | ||
|
||
class ServerRemoteDataSource { | ||
|
||
// MARK: Properties | ||
private let url: URL? | ||
private let session: URLSession! | ||
private var task: URLSessionDataTask? | ||
private weak var recorder: ZetapushNetworkRecorder? | ||
|
||
// MARK: Lifecycle | ||
init(configuration: ServerConfiguration, recorder: ZetapushNetworkRecorder?) { | ||
self.url = URL(string: configuration.serverUrl)?.appendingPathComponent(configuration.sandboxId) | ||
self.recorder = recorder | ||
|
||
let timeout = configuration.timeout | ||
let configuration = URLSessionConfiguration.default | ||
configuration.timeoutIntervalForRequest = timeout | ||
configuration.timeoutIntervalForResource = timeout * 3 | ||
|
||
self.session = URLSession(configuration: configuration) | ||
} | ||
|
||
// MARK: Methods | ||
func fetchServersURLs(callback: @escaping (Result<[String], Error>) -> Void) { | ||
guard let url = url, UIApplication.shared.canOpenURL(url) else { | ||
let error: ServerRemoteDataSourceError = .canNotOpenURL(url: self.url?.absoluteString ?? "") | ||
recorder?.record(error: error.toNSError()) | ||
callback(.failure(error)) | ||
return | ||
} | ||
task?.cancel() | ||
|
||
task = session.dataTask(with: url) { [weak self] data, response, error in | ||
defer { self?.task = nil } | ||
|
||
if let error = error { | ||
self?.recorder?.record(error: error as NSError) | ||
callback(.failure(error)) | ||
} else if let response = response as? HTTPURLResponse, response.statusCode != 200 { | ||
let error: ServerRemoteDataSourceError = .response(response: response) | ||
self?.recorder?.record(error: error.toNSError()) | ||
callback(.failure(error)) | ||
} else if let data = data { | ||
do { | ||
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] | ||
if let servers = json?["servers"] as? [String] { | ||
callback(.success(servers)) | ||
} else { | ||
let error: ServerRemoteDataSourceError = .serversNotFound | ||
self?.recorder?.record(error: error.toNSError()) | ||
callback(.failure(error)) | ||
} | ||
} catch let error { | ||
self?.recorder?.record(error: error as NSError) | ||
callback(.failure(error)) | ||
} | ||
} else { | ||
let error: ServerRemoteDataSourceError = .unknown | ||
self?.recorder?.record(error: error.toNSError()) | ||
callback(.failure(error)) | ||
} | ||
} | ||
task?.resume() | ||
} | ||
} |
Oops, something went wrong.