Skip to content

Commit 219a178

Browse files
Improvements
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent f17b596 commit 219a178

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

Sources/NextcloudKit/NKCommon.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,40 @@ public struct NKCommon: Sendable {
438438
return completion(filesChunk)
439439
}
440440

441+
// MARK: - Server Error GroupDefaults
442+
443+
public func appendServerErrorAccount(_ account: String, errorCode: Int) {
444+
guard let groupDefaults = UserDefaults(suiteName: groupIdentifier) else {
445+
return
446+
}
447+
448+
/// Unavailable
449+
if errorCode == 503 {
450+
var array = groupDefaults.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnavailable) as? [String] ?? []
451+
452+
if !array.contains(account) {
453+
array.append(account)
454+
groupDefaults.set(array, forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnavailable)
455+
}
456+
/// Unauthorized
457+
} else if errorCode == 401 {
458+
var array = groupDefaults.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnauthorized) as? [String] ?? []
459+
460+
if !array.contains(account) {
461+
array.append(account)
462+
groupDefaults.set(array, forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnauthorized)
463+
}
464+
/// ToS
465+
} else if errorCode == 403 {
466+
var array = groupDefaults.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsToS) as? [String] ?? []
467+
468+
if !array.contains(account) {
469+
array.append(account)
470+
groupDefaults.set(array, forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsToS)
471+
}
472+
}
473+
}
474+
441475
// MARK: - Common
442476

443477
public func getSession(account: String) -> NKSession? {

Sources/NextcloudKit/NKInterceptor.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ final class NKInterceptor: RequestInterceptor, Sendable {
1313
}
1414

1515
func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, Error>) -> Void) {
16-
//
17-
// Detect if exists in the groupDefaults Unauthorized array the account
18-
//
1916
if let url: String = urlRequest.url?.absoluteString,
2017
self.nkCommonInstance.levelLog > 0 {
2118
debugPrint("[DEBUG] Interceptor request url: " + url)

Sources/NextcloudKit/NKMonitor.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ final class NKMonitor: EventMonitor, Sendable {
2828
func request<Value>(_ request: DataRequest, didParseResponse response: AFDataResponse<Value>) {
2929
nkCommonInstance.delegate?.request(request, didParseResponse: response)
3030

31+
//
32+
// Server Error GroupDefaults
33+
//
34+
if let statusCode = response.response?.statusCode,
35+
let headerCheckInterceptor = request.request?.allHTTPHeaderFields?[self.nkCommonInstance.headerCheckInterceptor],
36+
headerCheckInterceptor.lowercased() == "true",
37+
let account = request.request?.allHTTPHeaderFields?[self.nkCommonInstance.headerAccount] as? String {
38+
self.nkCommonInstance.appendServerErrorAccount(account, errorCode: statusCode)
39+
}
40+
3141
//
3242
// LOG
3343
//

0 commit comments

Comments
 (0)