Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logger in api request client #26

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ disabled_rules:
- trailing_comma
- opening_brace
- todo
- non_optional_string_data_conversion
identifier_name:
allowed_symbols: "_"
min_length: 1
Expand All @@ -15,4 +16,4 @@ nesting:
error: 6
function_level:
warning: 5
error: 10
error: 10
12 changes: 12 additions & 0 deletions mobile-courier-app.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -154,6 +155,7 @@
1B6D75162C23BE5E0067BB56 /* GroupedPackageReadyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupedPackageReadyView.swift; sourceTree = "<group>"; };
1B6D75182C23BFB30067BB56 /* RoundedIndicatorViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundedIndicatorViewModifier.swift; sourceTree = "<group>"; };
1B6D751B2C23DF910067BB56 /* ShipmentDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShipmentDetailView.swift; sourceTree = "<group>"; };
1B6F7C302C2BA621005F0476 /* Data+Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Data.swift"; sourceTree = "<group>"; };
1B821F182C1AA50600ED1795 /* CustomSessionDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSessionDelegate.swift; sourceTree = "<group>"; };
1B85000D2C10033D006E96A0 /* ToastView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastView.swift; sourceTree = "<group>"; };
1B85000F2C1004D0006E96A0 /* ToastModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastModifier.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -359,6 +361,7 @@
1B58EFAF2BE6E9D00066F447 /* Networking */ = {
isa = PBXGroup;
children = (
1B6F7C2F2C2BA601005F0476 /* Extensions */,
1BFD1EBE2C2A836F002E4232 /* MockFiles */,
1B9274CE2C1158E6008F4FA3 /* Endpoints */,
1B85E8E02BFC4D9F003040CC /* Models */,
Expand Down Expand Up @@ -397,6 +400,14 @@
path = ShipmentDetail;
sourceTree = "<group>";
};
1B6F7C2F2C2BA601005F0476 /* Extensions */ = {
isa = PBXGroup;
children = (
1B6F7C302C2BA621005F0476 /* Data+Data.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
1B8500132C100DE2006E96A0 /* Profile */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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 */,
Expand Down
12 changes: 10 additions & 2 deletions mobile-courier-app/Data/Networking/APIRequestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import os

enum APIErrorMessage: Error {
case invalidRequest
Expand All @@ -21,16 +22,20 @@ protocol APIRequestClientProtocol {

final class APIRequestClient: NSObject, APIRequestClientProtocol {

static var instance: APIRequestClient = .init()
private lazy var logger: Logger = .init()

private lazy var delegate: URLSessionDelegate? = CustomSessionDelegate()

func performRequest<T: Decodable>(endpoint: Endpoint, decoder: JSONDecoder = JSONDecoder()) async throws -> T {
#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
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions mobile-courier-app/Data/Networking/Extensions/Data+Data.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Loading