Skip to content

Commit

Permalink
Merge pull request #24 from vespinola/23-add-mock-files
Browse files Browse the repository at this point in the history
23 add mock files
  • Loading branch information
vespinola authored Jun 25, 2024
2 parents 3f709b9 + d325fb8 commit 65005b3
Show file tree
Hide file tree
Showing 41 changed files with 2,386 additions and 407 deletions.
99 changes: 89 additions & 10 deletions mobile-courier-app.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion mobile-courier-app/App/AppDIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class AppDIContainer: DIContainerProtocol {
register(AddressRespository(), for: AddressRepositoryProtocol.self)
register(PackagesRepository(), for: PackagesRepositoryProtocol.self)

//Helpers
// Helpers
register(UserDefaultsStorage(), for: Storage.self)

// ViewModels
Expand All @@ -38,6 +38,10 @@ final class AppDIContainer: DIContainerProtocol {
PackagesForWithdrawalViewModel(packagesRepository: resolve(PackagesRepositoryProtocol.self)),
for: PackagesForWithdrawalViewModel.self
)
register(
SettingsViewModel(authRepository: resolve(AuthRepositoryProtocol.self)),
for: SettingsViewModel.self
)
}

func register<T>(_ service: T, for type: T.Type) {
Expand Down
2 changes: 1 addition & 1 deletion mobile-courier-app/App/MobileCourierApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftUI

@main
struct MobileCourierApp: App {
@AppStorage("isDarkMode") var isDarkMode: Bool = true
@AppStorage("isDarkMode") var isDarkMode: Bool = false

@StateObject var coordinator = Coordinator(diContainer: AppDIContainer())
@StateObject var appData = AppData.shared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct AddressRespository: AddressRepositoryProtocol {
decoder: JSONDecoder()
)

AppData.shared.setUsername(model.enviosAereos.cliente.clienteNombre)
AppData.shared.setUsername(model.airShipments.client.clientFirstName)

return model.asEntity()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ struct AuthRepository: AuthRepositoryProtocol {

AppData.shared.updateToken(model.accessToken)
}

func performLogout() {
AppData.shared.updateToken(nil)
}
}
14 changes: 9 additions & 5 deletions mobile-courier-app/Data/Networking/APIRequestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ final class APIRequestClient: NSObject, APIRequestClientProtocol {
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))
try? await Task.sleep(nanoseconds: 2 * 1_000_000_000)
return decodedData
}
#endif

guard let urlRequest = try? endpoint.asRequest() else {
throw APIErrorMessage.invalidRequest
}

do {
//
// #if DEBUG
// endpoint.mockFile =
// #endif

let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 10
configuration.timeoutIntervalForResource = 10
Expand Down
24 changes: 0 additions & 24 deletions mobile-courier-app/Data/Networking/CustomSessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@
import Foundation

final class CustomSessionDelegate: NSObject, URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
do {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: [.prettyPrinted])

if let jsonString = String(data: jsonData, encoding: .utf8) {
let urlString = dataTask.currentRequest?.url?.absoluteString ?? "Unknown URL"
print("Data received from '\(urlString)' ➡️ \n\(jsonString)")
}
} catch {
print("Failed to parse JSON: \(error.localizedDescription)")
}
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
print("Error: \(error.localizedDescription)")
} else {
print("Request completed successfully.")
}
}
}

extension CustomSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let serverTrust = challenge.protectionSpace.serverTrust else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ enum AddressEndpoints {

extension AddressEndpoints: Endpoint {
var mockFile: String? {
""
"Address"
}

var requestType: RequestType {
.get
}

var path: String {
"/frontliner-middleware/api/direccion"
"/direccion"
}

var body: [AnyHashable: Any]? {
Expand Down
10 changes: 4 additions & 6 deletions mobile-courier-app/Data/Networking/Endpoints/AuthEndpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ enum AuthEndpoints {

extension AuthEndpoints: Endpoint {

#if DEBUG
var mockFile: String? {
"login-response"
"Login"
}
#endif

var requestType: RequestType {
switch self {
case .login(email: _, password: _):
case .login:
return .post
default:
return .get
Expand All @@ -31,8 +29,8 @@ extension AuthEndpoints: Endpoint {

var path: String {
switch self {
case .login(email: _, password: _):
return "/frontliner-middleware/api/ingresar"
case .login:
return "/ingresar"
default:
return ""
}
Expand Down
4 changes: 1 addition & 3 deletions mobile-courier-app/Data/Networking/Endpoints/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ protocol Endpoint {
var authToken: String? { get }
var port: Int { get }

#if DEBUG
var mockFile: String? { get }
#endif
}

extension Endpoint {
var scheme: String { "https" }
var host: String { "frontliner.com.py" }
var host: String { "justacourier.com.py" }
var port: Int { 8449 }
var headers: [String: String] {
var internalHeaders = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ enum PackageEndpoints {

extension PackageEndpoints: Endpoint {
var mockFile: String? {
""
switch self {
case .withdrawn:
"WithdrawnPackages"
case .forWithdrawl:
"PackagesForWithdrawl"
}
}

var requestType: RequestType {
Expand All @@ -24,9 +29,9 @@ extension PackageEndpoints: Endpoint {
var path: String {
switch self {
case .withdrawn:
"/frontliner-middleware/api/paquetesRetirados"
"/paquetesRetirados"
case .forWithdrawl:
"/frontliner-middleware/api/paquetesPendientes"
"/paquetesPendientes"
}
}

Expand Down
68 changes: 68 additions & 0 deletions mobile-courier-app/Data/Networking/MockFiles/Address.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"enviosAereos": {
"ciudad": "New York, NY 10001",
"cliente": {
"class": "com.example.Clientes",
"id": 12345,
"apellidoautorizado": "SMITH",
"autorizaemail": 1,
"ciautorizado": "1234567",
"ciudad": 1,
"clienteapellido": "DOE",
"clientecelular": "1234567890",
"clienteci": "12345678",
"clientedireccion": "123 Main St",
"clientenombre": "JOHN",
"clienteobservacion": "",
"clientetelefono": "0987654321",
"dianac": 1,
"estado": 1,
"estante": "A1",
"mesnac": 1,
"nombreautorizado": "JANE",
"pagoAnualidad": null,
"password": "passwordhash",
"ruc": "9876543",
"tarifa": 25.0,
"username": "[email protected]"
},
"direccion": "789 Broadway",
"empresa": "EXAMPLE USA",
"pais": "United States",
"telefono": "(555) 123-4567",
"viaMaritima": false
},
"viaMaritima": {
"ciudad": "Los Angeles, CA 90001",
"cliente": {
"class": "com.example.Clientes",
"id": 12345,
"apellidoautorizado": "SMITH",
"autorizaemail": 1,
"ciautorizado": "1234567",
"ciudad": 1,
"clienteapellido": "DOE",
"clientecelular": "1234567890",
"clienteci": "12345678",
"clientedireccion": "123 Main St",
"clientenombre": "JOHN",
"clienteobservacion": "",
"clientetelefono": "0987654321",
"dianac": 1,
"estado": 1,
"estante": "A1",
"mesnac": 1,
"nombreautorizado": "JANE",
"pagoAnualidad": null,
"password": "passwordhash",
"ruc": "9876543",
"tarifa": 25.0,
"username": "[email protected]"
},
"direccion": "123 Ocean Blvd",
"empresa": "EXAMPLE USA",
"pais": "United States",
"telefono": "(555) 987-6543",
"viaMaritima": true
}
}
4 changes: 4 additions & 0 deletions mobile-courier-app/Data/Networking/MockFiles/Login.json
Original file line number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tIiwiZXhwIjoxNzI1MzM4ODE5LCJpYXQiOjE3MTkyOTA4MTksInJvbGVzIjpbIlJPTEVfTk9fUk9MRVMiXX0.Us_2_7asxxzrPv6D6LqKwVctdgD397XpyB4rfHC0pEK",
"username": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"paquetes": [
{
"paquetedescripcion": "TOY",
"paquetetracking": "CNUSUP00000773583",
"estado": "B",
"cotizacion": 7585.0,
"embarqueestado": "ASUNCION",
"embarquefecha": "2024-06-11T04:00:00Z",
"paquetepeso": 0.2,
"embarquemedio": "Aereo",
"embarquecodigo": 2179,
"id": 1606577,
"tarifapreciocli": 22.5,
"paqueteprecio": 22.5
},
{
"paquetedescripcion": "ACC",
"paquetetracking": "420331869361289677032421065418",
"estado": "A",
"cotizacion": 7585.0,
"embarqueestado": "TRANSITO",
"embarquefecha": "2024-06-18T04:00:00Z",
"paquetepeso": 0.12,
"embarquemedio": "Aereo",
"embarquecodigo": 2182,
"id": 1610658,
"tarifapreciocli": 22.5,
"paqueteprecio": 22.5
}
]
}
Loading

0 comments on commit 65005b3

Please sign in to comment.