Skip to content

Commit

Permalink
Add minimum deployment (#270)
Browse files Browse the repository at this point in the history
* Add minimum deployment

* Change platforms sorting

* Remove comma

* Improve failure
  • Loading branch information
3lvis authored Jun 23, 2023
1 parent 14ad2f9 commit adba8ec
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 75 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import PackageDescription

let package = Package(
name: "Networking",
platforms: [
.iOS(.v15), .macOS(.v12), .tvOS(.v15), .watchOS(.v8)
],
products: [
.library(
name: "Networking",
Expand All @@ -22,4 +25,4 @@ let package = Package(
resources: [.process("Resources")]
)
]
)
)
12 changes: 6 additions & 6 deletions Sources/Networking/Networking+HTTPRequests.swift
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Networking/Networking+Private.swift
Original file line number Diff line number Diff line change
@@ -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? {
Expand Down
53 changes: 28 additions & 25 deletions Sources/Networking/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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()
Expand All @@ -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)
}
}
}
}
8 changes: 4 additions & 4 deletions Tests/NetworkingTests/FakeRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}
}
Expand Down
44 changes: 22 additions & 22 deletions Tests/NetworkingTests/GETTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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"])
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/NetworkingTests/NetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/NetworkingTests/PATCHTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/NetworkingTests/ResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
20 changes: 10 additions & 10 deletions Tests/NetworkingTests/ResultTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class ResultTests: XCTestCase {
case .array(_, _), .none:
XCTFail()
}
case .failure:
XCTFail()
case let .failure(response):
XCTFail(response.error.localizedDescription)
}
}

Expand All @@ -46,8 +46,8 @@ class ResultTests: XCTestCase {
case .dictionary(_, _), .none:
XCTFail()
}
case .failure:
XCTFail()
case let .failure(response):
XCTFail(response.error.localizedDescription)
}
}

Expand All @@ -68,8 +68,8 @@ class ResultTests: XCTestCase {
case .array(_, _), .none:
XCTFail()
}
case .failure:
XCTFail()
case let .failure(response):
XCTFail(response.error.localizedDescription)
}
}

Expand All @@ -90,8 +90,8 @@ class ResultTests: XCTestCase {
case .dictionary(_, _), .none:
XCTFail()
}
case .failure:
XCTFail()
case let .failure(response):
XCTFail(response.error.localizedDescription)
}
}

Expand All @@ -109,8 +109,8 @@ class ResultTests: XCTestCase {
case .none:
break
}
case .failure:
XCTFail()
case let .failure(response):
XCTFail(response.error.localizedDescription)
}
}

Expand Down

0 comments on commit adba8ec

Please sign in to comment.