From 46d617a7d5ba334cca047b793fe156cae9ab60df Mon Sep 17 00:00:00 2001 From: Vladimir Espinola Date: Tue, 25 Jun 2024 21:34:37 -0400 Subject: [PATCH] added logger in api request client --- .swiftlint.yml | 3 ++- mobile-courier-app.xcodeproj/project.pbxproj | 12 +++++++++++ .../Data/Networking/APIRequestClient.swift | 12 +++++++++-- .../Networking/Extensions/Data+Data.swift | 20 +++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 mobile-courier-app/Data/Networking/Extensions/Data+Data.swift diff --git a/.swiftlint.yml b/.swiftlint.yml index 91afe4b..53e22c9 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -6,6 +6,7 @@ disabled_rules: - trailing_comma - opening_brace - todo + - non_optional_string_data_conversion identifier_name: allowed_symbols: "_" min_length: 1 @@ -15,4 +16,4 @@ nesting: error: 6 function_level: warning: 5 - error: 10 \ No newline at end of file + error: 10 diff --git a/mobile-courier-app.xcodeproj/project.pbxproj b/mobile-courier-app.xcodeproj/project.pbxproj index 8c92afa..a028a14 100644 --- a/mobile-courier-app.xcodeproj/project.pbxproj +++ b/mobile-courier-app.xcodeproj/project.pbxproj @@ -48,6 +48,7 @@ 1B6D75172C23BE5E0067BB56 /* GroupedPackageReadyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B6D75162C23BE5E0067BB56 /* GroupedPackageReadyView.swift */; }; 1B6D75192C23BFB30067BB56 /* RoundedIndicatorViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B6D75182C23BFB30067BB56 /* RoundedIndicatorViewModifier.swift */; }; 1B6D751C2C23DF910067BB56 /* ShipmentDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B6D751B2C23DF910067BB56 /* ShipmentDetailView.swift */; }; + 1B6F7C312C2BA621005F0476 /* Data+Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B6F7C302C2BA621005F0476 /* Data+Data.swift */; }; 1B821F192C1AA50600ED1795 /* CustomSessionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B821F182C1AA50600ED1795 /* CustomSessionDelegate.swift */; }; 1B85000E2C10033D006E96A0 /* ToastView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B85000D2C10033D006E96A0 /* ToastView.swift */; }; 1B8500102C1004D0006E96A0 /* ToastModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B85000F2C1004D0006E96A0 /* ToastModifier.swift */; }; @@ -154,6 +155,7 @@ 1B6D75162C23BE5E0067BB56 /* GroupedPackageReadyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupedPackageReadyView.swift; sourceTree = ""; }; 1B6D75182C23BFB30067BB56 /* RoundedIndicatorViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundedIndicatorViewModifier.swift; sourceTree = ""; }; 1B6D751B2C23DF910067BB56 /* ShipmentDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShipmentDetailView.swift; sourceTree = ""; }; + 1B6F7C302C2BA621005F0476 /* Data+Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Data.swift"; sourceTree = ""; }; 1B821F182C1AA50600ED1795 /* CustomSessionDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSessionDelegate.swift; sourceTree = ""; }; 1B85000D2C10033D006E96A0 /* ToastView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastView.swift; sourceTree = ""; }; 1B85000F2C1004D0006E96A0 /* ToastModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastModifier.swift; sourceTree = ""; }; @@ -359,6 +361,7 @@ 1B58EFAF2BE6E9D00066F447 /* Networking */ = { isa = PBXGroup; children = ( + 1B6F7C2F2C2BA601005F0476 /* Extensions */, 1BFD1EBE2C2A836F002E4232 /* MockFiles */, 1B9274CE2C1158E6008F4FA3 /* Endpoints */, 1B85E8E02BFC4D9F003040CC /* Models */, @@ -397,6 +400,14 @@ path = ShipmentDetail; sourceTree = ""; }; + 1B6F7C2F2C2BA601005F0476 /* Extensions */ = { + isa = PBXGroup; + children = ( + 1B6F7C302C2BA621005F0476 /* Data+Data.swift */, + ); + path = Extensions; + sourceTree = ""; + }; 1B8500132C100DE2006E96A0 /* Profile */ = { isa = PBXGroup; children = ( @@ -797,6 +808,7 @@ 1B6590E22C22856F00D46A27 /* PackagesForWithdrawalViewModel.swift in Sources */, 1B58EFA92BE6C0070066F447 /* BorderedViewModifier.swift in Sources */, 1BC0717C2C140CA9006EC08E /* AddressesRepositoryMock.swift in Sources */, + 1B6F7C312C2BA621005F0476 /* Data+Data.swift in Sources */, 1B821F192C1AA50600ED1795 /* CustomSessionDelegate.swift in Sources */, 1B9274D42C115EA4008F4FA3 /* AddressesEntity.swift in Sources */, 1BFD1EB32C2A7927002E4232 /* ShipmentStatus.swift in Sources */, diff --git a/mobile-courier-app/Data/Networking/APIRequestClient.swift b/mobile-courier-app/Data/Networking/APIRequestClient.swift index 35076dd..f969a2f 100644 --- a/mobile-courier-app/Data/Networking/APIRequestClient.swift +++ b/mobile-courier-app/Data/Networking/APIRequestClient.swift @@ -6,6 +6,7 @@ // import Foundation +import os enum APIErrorMessage: Error { case invalidRequest @@ -21,7 +22,7 @@ protocol APIRequestClientProtocol { final class APIRequestClient: NSObject, APIRequestClientProtocol { - static var instance: APIRequestClient = .init() + private lazy var logger: Logger = .init() private lazy var delegate: URLSessionDelegate? = CustomSessionDelegate() @@ -29,8 +30,12 @@ final class APIRequestClient: NSObject, APIRequestClientProtocol { #if DEBUG if let mockfile = endpoint.mockFile, let fileUrl = Bundle.main.url(forResource: mockfile, withExtension: "json") { decoder.keyDecodingStrategy = .convertFromSnakeCase - let decodedData = try decoder.decode(T.self, from: Data(contentsOf: fileUrl)) + let data = try Data(contentsOf: fileUrl, options: .uncached) + let decodedData = try decoder.decode(T.self, from: data) try? await Task.sleep(nanoseconds: 2 * 1_000_000_000) + + logger.debug("\(mockfile): \(data.prettyPrintedJSONString ?? "")") + return decodedData } #endif @@ -59,6 +64,9 @@ final class APIRequestClient: NSObject, APIRequestClientProtocol { decoder.keyDecodingStrategy = .convertFromSnakeCase let decodedData = try decoder.decode(T.self, from: data) + + logger.debug("\(response.url?.absoluteString ?? ""): \(data.prettyPrintedJSONString ?? "")") + return decodedData } catch { if (error as NSError).code == NSURLErrorTimedOut { diff --git a/mobile-courier-app/Data/Networking/Extensions/Data+Data.swift b/mobile-courier-app/Data/Networking/Extensions/Data+Data.swift new file mode 100644 index 0000000..635526c --- /dev/null +++ b/mobile-courier-app/Data/Networking/Extensions/Data+Data.swift @@ -0,0 +1,20 @@ +// +// Data+Data.swift +// mobile-courier-app +// +// Created by Vladimir Espinola on 2024-06-25. +// + +import Foundation + +extension Data { + var prettyPrintedJSONString: String? { + do { + let jsonObject = try JSONSerialization.jsonObject(with: self, options: []) + let prettyData = try JSONSerialization.data(withJSONObject: jsonObject, options: [.prettyPrinted]) + return String(data: prettyData, encoding: .utf8) + } catch { + return nil + } + } +}