From 5ea74935ef9383e325c51da64336dbd721865bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yu=C5=9Fa=C2=A0?= Date: Sat, 22 Jul 2023 23:21:37 +0200 Subject: [PATCH] Update APIConstant and RequestManager --- .../Data/API/Constant/DomainProtocol.swift | 4 ++-- .../Data/API/Network/APIManager.swift | 8 ++++---- .../Data/API/Network/RequestManager.swift | 6 +----- .../SwiftChain/Data/API/Parser/DataParser.swift | 8 ++++---- .../Request/ExampleRequest/APIConstant.swift | 17 +++-------------- 5 files changed, 14 insertions(+), 29 deletions(-) diff --git a/Sources/SwiftChain/Data/API/Constant/DomainProtocol.swift b/Sources/SwiftChain/Data/API/Constant/DomainProtocol.swift index 472aa91..9aded8d 100644 --- a/Sources/SwiftChain/Data/API/Constant/DomainProtocol.swift +++ b/Sources/SwiftChain/Data/API/Constant/DomainProtocol.swift @@ -1,4 +1,4 @@ public protocol DomainProtocol { - static var domain: String { get } - static var apiKey: String { get } + static var domain: String { get set } + static var apiKey: String { get set } } diff --git a/Sources/SwiftChain/Data/API/Network/APIManager.swift b/Sources/SwiftChain/Data/API/Network/APIManager.swift index 9d13904..bb01155 100644 --- a/Sources/SwiftChain/Data/API/Network/APIManager.swift +++ b/Sources/SwiftChain/Data/API/Network/APIManager.swift @@ -2,13 +2,13 @@ import Foundation // MARK: - APIManagerProtocol -protocol APIManagerProtocol { +public protocol APIManagerProtocol { func makeRequest(from request: RequestProtocol) async throws -> Data } // MARK: - APIManager -final class APIManager { +public final class APIManager { // MARK: - Properties @@ -16,7 +16,7 @@ final class APIManager { // MARK: - Initialization - init(urlSession: URLSession = URLSession.shared) { + public init(urlSession: URLSession = URLSession.shared) { self.urlSession = urlSession } } @@ -24,7 +24,7 @@ final class APIManager { // MARK: - APIManagerProtocol extension APIManager: APIManagerProtocol { - func makeRequest(from request: RequestProtocol) async throws -> Data { + public func makeRequest(from request: RequestProtocol) async throws -> Data { let (data, response) = try await urlSession.data(for: request.createURLRequest()) guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 diff --git a/Sources/SwiftChain/Data/API/Network/RequestManager.swift b/Sources/SwiftChain/Data/API/Network/RequestManager.swift index cf50c1e..b372856 100644 --- a/Sources/SwiftChain/Data/API/Network/RequestManager.swift +++ b/Sources/SwiftChain/Data/API/Network/RequestManager.swift @@ -8,10 +8,6 @@ public protocol RequestManagerProtocol { public final class RequestManager { - // MARK: - Singleton Instance - - public static let shared = RequestManager() - // MARK: - Properties let apiManager: APIManagerProtocol @@ -19,7 +15,7 @@ public final class RequestManager { // MARK: - Initialization - init( + public init( apiManager: APIManagerProtocol = APIManager(), parser: DataParserProtocol = DataParser() ) { diff --git a/Sources/SwiftChain/Data/API/Parser/DataParser.swift b/Sources/SwiftChain/Data/API/Parser/DataParser.swift index e68ab0f..43249ce 100644 --- a/Sources/SwiftChain/Data/API/Parser/DataParser.swift +++ b/Sources/SwiftChain/Data/API/Parser/DataParser.swift @@ -2,13 +2,13 @@ import Foundation // MARK: - DataParserProtocol -protocol DataParserProtocol { +public protocol DataParserProtocol { func parse(data: Data) throws -> Element } // MARK: - DataParser -final class DataParser { +public final class DataParser { // MARK: - Properties @@ -16,7 +16,7 @@ final class DataParser { // MARK: - Initialization - init(jsonDecoder: JSONDecoder = JSONDecoder()) { + public init(jsonDecoder: JSONDecoder = JSONDecoder()) { self.jsonDecoder = jsonDecoder } } @@ -24,7 +24,7 @@ final class DataParser { // MARK: - DataParserProtocol extension DataParser: DataParserProtocol { - func parse(data: Data) throws -> Element { + public func parse(data: Data) throws -> Element { try jsonDecoder.decode( Element.self, from: data diff --git a/Sources/SwiftChain/Data/API/Request/ExampleRequest/APIConstant.swift b/Sources/SwiftChain/Data/API/Request/ExampleRequest/APIConstant.swift index 51b3748..1a62345 100644 --- a/Sources/SwiftChain/Data/API/Request/ExampleRequest/APIConstant.swift +++ b/Sources/SwiftChain/Data/API/Request/ExampleRequest/APIConstant.swift @@ -1,17 +1,6 @@ // MARK: - APIConstant -enum APIConstant { - -} - -// MARK: - DomainProtocol - -extension APIConstant: DomainProtocol { - static var domain: String { - "YOUR_DOMAIN" - } - - static var apiKey: String { - "YOUR_API_KEY" - } +public enum APIConstant: DomainProtocol { + public static var domain: String = "YOUR_DOMAIN" + public static var apiKey: String = "YOUR_API_KEY" }