Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
federicocappelli committed Dec 12, 2024
1 parent 50d4d64 commit 1292c9d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 94 deletions.
1 change: 1 addition & 0 deletions Sources/Common/KeychainType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import Foundation

/// A convenience enum to unify the logic for selecting the right keychain through the query attributes.
public enum KeychainType {
case dataProtection(_ accessGroup: AccessGroup)
/// Uses the system keychain.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ public actor NetworkProtectionEntitlementMonitor {
// MARK: - Start/Stop monitoring

public func start(entitlementCheck: @escaping () async -> Swift.Result<Bool, Error>, callback: @escaping (Result) async -> Void) {
Logger.networkProtectionEntitlement.log("Starting entitlement monitor")
Logger.networkProtectionEntitlement.log("⚫️ Starting entitlement monitor")

task = Task.periodic(interval: Self.monitoringInterval) {
let result = await entitlementCheck()
switch result {
case .success(let hasEntitlement):
if hasEntitlement {
Logger.networkProtectionEntitlement.log("Valid entitlement")
Logger.networkProtectionEntitlement.log("⚫️ Valid entitlement")
await callback(.validEntitlement)
} else {
Logger.networkProtectionEntitlement.log("Invalid entitlement")
Logger.networkProtectionEntitlement.log("⚫️ Invalid entitlement")
await callback(.invalidEntitlement)
}
case .failure(let error):
Logger.networkProtectionEntitlement.error("Error retrieving entitlement: \(error.localizedDescription, privacy: .public)")
Logger.networkProtectionEntitlement.error("⚫️ Error retrieving entitlement: \(error.localizedDescription, privacy: .public)")
await callback(.error(error))
}
}
}

public func stop() {
Logger.networkProtectionEntitlement.log("Stopping entitlement monitor")
Logger.networkProtectionEntitlement.log("⚫️ Stopping entitlement monitor")

task?.cancel() // Just making extra sure in case it's detached
task = nil
Expand Down
38 changes: 19 additions & 19 deletions Sources/NetworkProtection/Networking/NetworkProtectionClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,25 @@ public enum NetworkProtectionClientError: CustomNSError, NetworkProtectionErrorC
}
}

// public var errorDescription: String? {
// switch self {
// case .failedToFetchLocationList: return "Failed to fetch location list"
// case .failedToParseLocationListResponse: return "Failed to parse location list response"
// case .failedToFetchServerList: return "Failed to fetch server list"
// case .failedToParseServerListResponse: return "Failed to parse server list response"
// case .failedToEncodeRegisterKeyRequest: return "Failed to encode register key request"
// case .failedToFetchServerStatus(let error):
// return "Failed to fetch server status: \(error)"
// case .failedToParseServerStatusResponse(let error):
// return "Failed to parse server status response: \(error)"
// case .failedToFetchRegisteredServers(let error):
// return "Failed to fetch registered servers: \(error)"
// case .failedToParseRegisteredServersResponse(let error):
// return "Failed to parse registered servers response: \(error)"
// case .invalidAuthToken: return "Invalid auth token"
// case .accessDenied: return "Access denied"
// }
// }
public var errorDescription: String? {
switch self {
case .failedToFetchLocationList: return "Failed to fetch location list"
case .failedToParseLocationListResponse: return "Failed to parse location list response"
case .failedToFetchServerList: return "Failed to fetch server list"
case .failedToParseServerListResponse: return "Failed to parse server list response"
case .failedToEncodeRegisterKeyRequest: return "Failed to encode register key request"
case .failedToFetchServerStatus(let error):
return "Failed to fetch server status: \(error)"
case .failedToParseServerStatusResponse(let error):
return "Failed to parse server status response: \(error)"
case .failedToFetchRegisteredServers(let error):
return "Failed to fetch registered servers: \(error)"
case .failedToParseRegisteredServersResponse(let error):
return "Failed to parse registered servers response: \(error)"
case .invalidAuthToken: return "Invalid auth token"
case .accessDenied: return "Access denied"
}
}
}

struct RegisterKeyRequestBody: Encodable {
Expand Down
3 changes: 0 additions & 3 deletions Sources/Networking/OAuth/README.md

This file was deleted.

1 change: 0 additions & 1 deletion Sources/Networking/OAuth/SessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public final class SessionDelegate: NSObject, URLSessionTaskDelegate {

/// Disable automatic redirection, in our specific OAuth implementation we manage the redirection, not the user
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> URLRequest? {
// Logger.networking.debug("Stopping OAuth API redirection: \(response)")
return nil
}
}
1 change: 0 additions & 1 deletion Sources/Networking/v2/APIResponseV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public extension APIResponseV2 {
/// - Parameter decoder: A custom JSONDecoder, if not provided the default JSONDecoder() is used
/// - Returns: An instance of a Decodable model of the type inferred, throws an error if the body is empty or the decoding fails
func decodeBody<T: Decodable>(decoder: JSONDecoder = JSONDecoder()) throws -> T {
// decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .millisecondsSince1970

guard let data = self.data else {
Expand Down
63 changes: 0 additions & 63 deletions Sources/Subscription/API/SubscriptionRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,66 +81,3 @@ struct SubscriptionRequest {
return SubscriptionRequest(apiRequest: request)
}
}
/*
extension SubscriptionRequest {

// MARK: - Response body errors

enum Error: LocalizedError, Equatable {
case APIError(code: BodyErrorCode)
case missingResponseValue(String)

public var errorDescription: String? {
switch self {
case .APIError(let code):
"Subscription API responded with error \(code.rawValue) - \(code.description)"
case .missingResponseValue(let value):
"The API response is missing \(value)"
}
}

public static func == (lhs: SubscriptionRequest.Error, rhs: SubscriptionRequest.Error) -> Bool {
switch (lhs, rhs) {
case (.APIError(let lhsCode), .APIError(let rhsCode)):
return lhsCode == rhsCode
case (.missingResponseValue(let lhsValue), .missingResponseValue(let rhsValue)):
return lhsValue == rhsValue
default:
return lhs == rhs
}
}
}

struct BodyError: Decodable {
let error: BodyErrorCode
}

public enum BodyErrorCode: String, Decodable {
case noSubscriptionFound = "No subscription found"

public var description: String {
switch self {
case .noSubscriptionFound:
return self.rawValue
}
}
}

func extractBodyError(from response: APIResponseV2) -> BodyError? {
do {
let bodyError: SubscriptionRequest.BodyError = try response.decodeBody()
return bodyError
} catch {
return nil
}
}

func throwError(forResponse response: APIResponseV2) throws {
if let extractBodyError = extractBodyError(from: response) {
throw SubscriptionRequest.Error.APIError(code: extractBodyError.error)
} else {
throw SubscriptionRequest.Error.missingResponseValue("Body error")
}
}
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ public final class SubscriptionTokenKeychainStorageV2: TokenStoring {

public var tokenContainer: TokenContainer? {
get {
// Logger.subscriptionKeychain.debug("get TokenContainer")
guard let data = try? retrieveData(forField: .tokens) else {
Logger.subscriptionKeychain.debug("TokenContainer not found")
return nil
}
return CodableHelper.decode(jsonData: data)
}
set {
// Logger.subscriptionKeychain.debug("set TokenContainer")
do {
guard let newValue else {
Logger.subscriptionKeychain.debug("remove TokenContainer")
Expand Down

0 comments on commit 1292c9d

Please sign in to comment.