Javier Cicchelli 43c156a2c3 [Setup] App architecture (#9)
This PR contains the work done to put in the app an MVVM-C architecture plus other small fixes in the `Library` libraries.

To give further details about the work done:
- [x] remove the `SceneDelegate` delegate;
- [x] implemented the `WindowRouter`, `PushNavigationRouter`, and `ModalNavigationRouter` routers in the `Core` library;
- [x] defined the `LocationsAddCoordination` and `LocationsListCoordination` protocols;
- [x] defined the `LocationsAddViewModeling` and `LocationsListViewModeling` protocols;
- [x] implemented the `LocationsListCoordinator` and `LocationsAddCoordinator` coordinators;
- [x] implemented the `LocationsAddViewController` view controller and `LocationsAddViewModel` view model;
- [x] implemented the `LocationsListViewController` view controller and `LocationsListViewModel` view model;
- [x] implemented the `BaseViewController` base view controller;
- [x] implemented the `persistence` and `remote` properties in the `DependencyService+Keys` extension.

Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Reviewed-on: rock-n-code/deep-linking-assignment#9
2023-04-11 22:14:40 +00:00

67 lines
1.6 KiB
Swift

//
// RemoteClient.swift
// Remote
//
// Created by Javier Cicchelli on 10/04/2023.
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
import APICore
import Foundation
struct RemoteClient {
// MARK: Properties
private let session: URLSession
private let decoder: JSONDecoder = .init()
private let makeURLRequest: MakeURLRequestUseCase = .init()
// MARK: Initialisers
init(configuration: URLSessionConfiguration = .default) {
self.session = .init(configuration: configuration)
}
}
// MARK: - Client
extension RemoteClient: Client {
// MARK: Functions
func request<Model: Decodable>(
endpoint: some Endpoint,
for model: Model.Type
) async throws -> Model {
let urlRequest = try makeURLRequest(endpoint: endpoint)
let (data, response) = try await session.data(for: urlRequest)
guard let httpResponse = response as? HTTPURLResponse else {
throw LocationsClientError.responseNotReturned
}
switch httpResponse.statusCode {
case 200:
return try decoder.decode(model, from: data)
case 400...499:
throw LocationsClientError.statusErrorClient
case 500...599:
throw LocationsClientError.statusErrorServer
default:
throw LocationsClientError.statusErrorUnexpected
}
}
}
// MARK: - Errors
public enum LocationsClientError: Error {
case responseNotReturned
case statusErrorClient
case statusErrorServer
case statusErrorUnexpected
}