Skip to content

Commit

Permalink
Update APIConstant and RequestManager
Browse files Browse the repository at this point in the history
  • Loading branch information
yusasarisoy committed Jul 22, 2023
1 parent c19f5b5 commit 5ea7493
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Sources/SwiftChain/Data/API/Constant/DomainProtocol.swift
Original file line number Diff line number Diff line change
@@ -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 }
}
8 changes: 4 additions & 4 deletions Sources/SwiftChain/Data/API/Network/APIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ 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

private let urlSession: URLSession

// MARK: - Initialization

init(urlSession: URLSession = URLSession.shared) {
public init(urlSession: URLSession = URLSession.shared) {
self.urlSession = urlSession
}
}

// 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
Expand Down
6 changes: 1 addition & 5 deletions Sources/SwiftChain/Data/API/Network/RequestManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ public protocol RequestManagerProtocol {

public final class RequestManager {

// MARK: - Singleton Instance

public static let shared = RequestManager()

// MARK: - Properties

let apiManager: APIManagerProtocol
let parser: DataParserProtocol

// MARK: - Initialization

init(
public init(
apiManager: APIManagerProtocol = APIManager(),
parser: DataParserProtocol = DataParser()
) {
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftChain/Data/API/Parser/DataParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ import Foundation

// MARK: - DataParserProtocol

protocol DataParserProtocol {
public protocol DataParserProtocol {
func parse<Element: Decodable>(data: Data) throws -> Element
}

// MARK: - DataParser

final class DataParser {
public final class DataParser {

// MARK: - Properties

private var jsonDecoder: JSONDecoder

// MARK: - Initialization

init(jsonDecoder: JSONDecoder = JSONDecoder()) {
public init(jsonDecoder: JSONDecoder = JSONDecoder()) {
self.jsonDecoder = jsonDecoder
}
}

// MARK: - DataParserProtocol

extension DataParser: DataParserProtocol {
func parse<Element: Decodable>(data: Data) throws -> Element {
public func parse<Element: Decodable>(data: Data) throws -> Element {
try jsonDecoder.decode(
Element.self,
from: data
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 5ea7493

Please sign in to comment.