diff --git a/Package.swift b/Package.swift index fea46ad..d543c3e 100644 --- a/Package.swift +++ b/Package.swift @@ -5,6 +5,9 @@ import PackageDescription let package = Package( name: "Networking", + platforms: [ + .iOS(.v15), .macOS(.v12), .tvOS(.v15), .watchOS(.v8) + ], products: [ .library( name: "Networking", @@ -22,4 +25,4 @@ let package = Package( resources: [.process("Resources")] ) ] -) +) \ No newline at end of file diff --git a/Sources/Networking/Networking+HTTPRequests.swift b/Sources/Networking/Networking+HTTPRequests.swift index 40ccb71..1df2f0b 100644 --- a/Sources/Networking/Networking+HTTPRequests.swift +++ b/Sources/Networking/Networking+HTTPRequests.swift @@ -1,6 +1,6 @@ import Foundation -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public extension Networking { /// GET request to the specified path. /// @@ -42,7 +42,7 @@ public extension Networking { } } -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public extension Networking { /// PATCH request to the specified path, using the provided parameters. @@ -84,7 +84,7 @@ public extension Networking { } } -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public extension Networking { /// PUT request to the specified path, using the provided parameters. @@ -126,7 +126,7 @@ public extension Networking { } } -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public extension Networking { /// POST request to the specified path, using the provided parameters. @@ -178,7 +178,7 @@ public extension Networking { } } -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public extension Networking { /// DELETE request to the specified path, using the provided parameters. @@ -220,7 +220,7 @@ public extension Networking { } } -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public extension Networking { /// Retrieves an image from the cache or from the filesystem. diff --git a/Sources/Networking/Networking+Private.swift b/Sources/Networking/Networking+Private.swift index aa43d24..91cd8ad 100644 --- a/Sources/Networking/Networking+Private.swift +++ b/Sources/Networking/Networking+Private.swift @@ -1,6 +1,6 @@ import Foundation -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) extension Networking { func objectFromCache(for path: String, cacheName: String?, cachingLevel: CachingLevel, responseType: ResponseType) throws -> Any? { diff --git a/Sources/Networking/Networking.swift b/Sources/Networking/Networking.swift index 7a05e3d..8b01560 100644 --- a/Sources/Networking/Networking.swift +++ b/Sources/Networking/Networking.swift @@ -238,10 +238,36 @@ open class Networking { return (baseURL, relativePath) } + /// Removes the stored credentials and cached data. + public func reset() throws { + cache.removeAllObjects() + fakeRequests.removeAll() + token = nil + headerFields = nil + authorizationHeaderKey = "Authorization" + authorizationHeaderValue = nil + + try Networking.deleteCachedFiles() + } + + /// Deletes the downloaded/cached files. + public static func deleteCachedFiles() throws { + let directory = FileManager.SearchPathDirectory.cachesDirectory + if let cachesURL = FileManager.default.urls(for: directory, in: .userDomainMask).first { + let folderURL = cachesURL.appendingPathComponent(URL(string: Networking.domain)!.absoluteString) + if FileManager.default.exists(at: folderURL) { + _ = try FileManager.default.remove(at: folderURL) + } + } + } +} + +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) +public extension Networking { /// Cancels the request that matches the requestID. /// /// - Parameter requestID: The ID of the request to be cancelled. - public func cancel(_ requestID: String) async { + func cancel(_ requestID: String) async { let (dataTasks, uploadTasks, downloadTasks) = await session.tasks var tasks = [URLSessionTask]() tasks.append(contentsOf: dataTasks as [URLSessionTask]) @@ -257,7 +283,7 @@ open class Networking { } /// Cancels all the current requests. - public func cancelAllRequests() async { + func cancelAllRequests() async { let (dataTasks, uploadTasks, downloadTasks) = await session.tasks for sessionTask in dataTasks { sessionTask.cancel() @@ -269,27 +295,4 @@ open class Networking { sessionTask.cancel() } } - - /// Removes the stored credentials and cached data. - public func reset() throws { - cache.removeAllObjects() - fakeRequests.removeAll() - token = nil - headerFields = nil - authorizationHeaderKey = "Authorization" - authorizationHeaderValue = nil - - try Networking.deleteCachedFiles() - } - - /// Deletes the downloaded/cached files. - public static func deleteCachedFiles() throws { - let directory = FileManager.SearchPathDirectory.cachesDirectory - if let cachesURL = FileManager.default.urls(for: directory, in: .userDomainMask).first { - let folderURL = cachesURL.appendingPathComponent(URL(string: Networking.domain)!.absoluteString) - if FileManager.default.exists(at: folderURL) { - _ = try FileManager.default.remove(at: folderURL) - } - } - } } diff --git a/Tests/NetworkingTests/FakeRequestTests.swift b/Tests/NetworkingTests/FakeRequestTests.swift index 006bc98..1d2ceff 100644 --- a/Tests/NetworkingTests/FakeRequestTests.swift +++ b/Tests/NetworkingTests/FakeRequestTests.swift @@ -334,8 +334,8 @@ extension FakeRequestTests { let json = response.arrayBody let value = json[0]["name"] as? String XCTAssertEqual(value, "Elvis") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -365,8 +365,8 @@ extension FakeRequestTests { let entry = json[0] let value = entry["title"] as? String XCTAssertEqual(value, "Entry 1") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } } diff --git a/Tests/NetworkingTests/GETTests.swift b/Tests/NetworkingTests/GETTests.swift index fb1f915..7e0705c 100644 --- a/Tests/NetworkingTests/GETTests.swift +++ b/Tests/NetworkingTests/GETTests.swift @@ -18,8 +18,8 @@ class GETTests: XCTestCase { guard let headers = json["headers"] as? [String: String] else { XCTFail(); return } let contentType = headers["Content-Type"] XCTAssertNil(contentType) - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -36,8 +36,8 @@ class GETTests: XCTestCase { guard let headers = json["headers"] as? [String: String] else { XCTFail(); return } let contentType = headers["Content-Type"] XCTAssertNil(contentType) - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -54,8 +54,8 @@ class GETTests: XCTestCase { guard let connection = headers["Connection"] as? String else { XCTFail(); return } XCTAssertEqual(connection, "keep-alive") XCTAssertEqual(headers["Content-Type"] as? String, "application/json") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -91,8 +91,8 @@ class GETTests: XCTestCase { switch result200 { case let .success(response): XCTAssertEqual(response.statusCode, 200) - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } var statusCode = 300 @@ -123,8 +123,8 @@ class GETTests: XCTestCase { case let .success(response): let json = response.dictionaryBody XCTAssertEqual(json["url"] as? String, "http://httpbin.org/get?count=25") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -135,8 +135,8 @@ class GETTests: XCTestCase { case let .success(response): let json = response.dictionaryBody XCTAssertEqual(json["url"] as? String, "http://httpbin.org/get?accountId=123&userId=5") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -147,8 +147,8 @@ class GETTests: XCTestCase { case let .success(response): let json = response.dictionaryBody XCTAssertEqual(json["url"] as? String, "http://httpbin.org/get?name=Elvis Nuñez") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -161,8 +161,8 @@ class GETTests: XCTestCase { case let .success(response): let json = response.dictionaryBody XCTAssertEqual(json["key"] as? String, "value1") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } networking.fakeGET("/get", response: ["key": "value2"]) @@ -172,8 +172,8 @@ class GETTests: XCTestCase { case let .success(response): let json = response.dictionaryBody XCTAssertEqual(json["key"] as? String, "value2") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -196,8 +196,8 @@ class GETTests: XCTestCase { case let .success(response): let json = response.dictionaryBody XCTAssertEqual(json["key"] as? String, "value2") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -220,8 +220,8 @@ class GETTests: XCTestCase { case let .success(response): let json = response.dictionaryBody XCTAssertEqual(json["key"] as? String, "value2") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } diff --git a/Tests/NetworkingTests/NetworkingTests.swift b/Tests/NetworkingTests/NetworkingTests.swift index be13588..d1bb0aa 100644 --- a/Tests/NetworkingTests/NetworkingTests.swift +++ b/Tests/NetworkingTests/NetworkingTests.swift @@ -16,8 +16,8 @@ class NetworkingTests: XCTestCase { let authenticated = json["authenticated"] as? Bool XCTAssertEqual(user, "user") XCTAssertEqual(authenticated, true) - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } diff --git a/Tests/NetworkingTests/PATCHTests.swift b/Tests/NetworkingTests/PATCHTests.swift index b597f08..68f510f 100644 --- a/Tests/NetworkingTests/PATCHTests.swift +++ b/Tests/NetworkingTests/PATCHTests.swift @@ -17,8 +17,8 @@ class PATCHTests: XCTestCase { guard let headers = json["headers"] as? [String: String] else { XCTFail(); return } XCTAssertEqual(headers["Content-Type"], "application/json") - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } diff --git a/Tests/NetworkingTests/ResponseTests.swift b/Tests/NetworkingTests/ResponseTests.swift index 9e90bd6..9c0f0c2 100644 --- a/Tests/NetworkingTests/ResponseTests.swift +++ b/Tests/NetworkingTests/ResponseTests.swift @@ -12,8 +12,8 @@ class ResponseTest: XCTestCase { switch result { case let .success(response): XCTAssertEqual(try response.data.toStringStringDictionary().debugDescription, expectedBody.debugDescription) - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } } diff --git a/Tests/NetworkingTests/ResultTests.swift b/Tests/NetworkingTests/ResultTests.swift index 7fffbe2..5dc2d3f 100644 --- a/Tests/NetworkingTests/ResultTests.swift +++ b/Tests/NetworkingTests/ResultTests.swift @@ -25,8 +25,8 @@ class ResultTests: XCTestCase { case .array(_, _), .none: XCTFail() } - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -46,8 +46,8 @@ class ResultTests: XCTestCase { case .dictionary(_, _), .none: XCTFail() } - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -68,8 +68,8 @@ class ResultTests: XCTestCase { case .array(_, _), .none: XCTFail() } - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -90,8 +90,8 @@ class ResultTests: XCTestCase { case .dictionary(_, _), .none: XCTFail() } - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } } @@ -109,8 +109,8 @@ class ResultTests: XCTestCase { case .none: break } - case .failure: - XCTFail() + case let .failure(response): + XCTFail(response.error.localizedDescription) } }