Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosCaceres86 committed Nov 6, 2024
1 parent 6e0fa0f commit e6b4d75
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 183 deletions.
23 changes: 13 additions & 10 deletions Sources/NetNetNet/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
// Created by Carlos Cáceres González on 5/11/24.
//

public struct Endpoint {
let path: String
let encoding: Encoding
let method: HTTPMethods
let withAuth: Bool
let retries: Int
let queryItems: [String : String]?
let headers: [String : String]?
import Foundation


public struct Endpoint {
public var path: String
public var encoding: BodyEncoding
public var method: HTTPMethods
public var withAuth: Bool
public var retries: Int
public var queryItems: [URLQueryItem]?
public var headers: [String : String]?

public init(path: String,
encoding: Encoding = .json,
encoding: BodyEncoding = .json,
method: HTTPMethods = .get,
withAuth: Bool = false,
retries: Int = 0,
queryItems: [String : String]? = nil,
queryItems: [URLQueryItem]? = nil,
headers: [String : String]? = nil) {
self.path = path
self.encoding = encoding
Expand Down
31 changes: 31 additions & 0 deletions Sources/NetNetNet/NetConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// NetNetNet.swift
// NetNetNet
//
// Created by Carlos Cáceres González on 20/10/24.
//

import Foundation


public struct NetConfig {
// Basic configuration
var scheme: String
var host: String
// OAuth Configuration
var clientId: String
var clientSecret: String
var oauthEndpoint: String

public init(scheme: String = "https",
host: String,
clientId: String = "",
clientSecret: String = "",
oauthEndpoint: String = "") {
self.scheme = scheme
self.host = host
self.clientId = clientId
self.clientSecret = clientSecret
self.oauthEndpoint = oauthEndpoint
}
}
78 changes: 0 additions & 78 deletions Sources/NetNetNet/NetNetNet.swift

This file was deleted.

47 changes: 9 additions & 38 deletions Sources/NetNetNet/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,15 @@ protocol RequestProtocol {
@available(iOS 15.0, *)
public class Request: RequestProtocol {
let urlRequest: URLRequest?
let netConfig: NetConfig

public init(endpoint: Endpoint) {
self.urlRequest = URLFactory().create(endpoint: endpoint)
public init(endpoint: Endpoint, netConfig: NetConfig) {
self.netConfig = netConfig
self.urlRequest = URLFactory(netConfig: netConfig).create(endpoint: endpoint)
}

/* Public Methods */
// MARK: Public Methods
public func makeCall<R: Codable>() async throws -> R {
var data: Data

if NetNetNet.shared.isCacheEnabled, let cachedData = try fetchDataFromCache() {
data = cachedData
} else {
data = try await fetchDataFromRemote()
}

guard let decodedResponse = try? JSONDecoder().decode(R.self, from: data) else {
throw NetErrors.runtimeError("Error decoding response")
}

return decodedResponse
}

/* Private Methods */
private func fetchDataFromRemote() async throws -> Data {
guard let request = self.urlRequest else {
throw NetErrors.runtimeError("Bad URL")
}
Expand All @@ -52,24 +37,10 @@ public class Request: RequestProtocol {
throw NetErrors.runtimeError("Error response code: \(httpResponse.statusCode)")
}

if NetNetNet.shared.isCacheEnabled {
let cachedResponse = CachedURLResponse(response: response, data: data)
URLCache.shared.storeCachedResponse(cachedResponse, for: request)
}

return data
}

private func fetchDataFromCache() throws -> Data? {
guard let request = self.urlRequest else {
throw NetErrors.runtimeError("Bad URL")
}

guard let cachedResponse = URLCache.shared.cachedResponse(for: request) else {
return nil
guard let decodedResponse = try? JSONDecoder().decode(R.self, from: data) else {
throw NetErrors.runtimeError("Error decoding response")
}

return cachedResponse.data
return decodedResponse
}

}
2 changes: 1 addition & 1 deletion Sources/NetNetNet/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Carlos Cáceres González on 5/11/24.
//

public enum Encoding {
public enum BodyEncoding {
case json, url
}

Expand Down
17 changes: 12 additions & 5 deletions Sources/NetNetNet/URLFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@

import Foundation

public protocol URLFactoryProtocol {
protocol URLFactoryProtocol {
func create(endpoint: Endpoint) -> URLRequest?
}

struct URLFactory: URLFactoryProtocol {
private let netConfig: NetConfig

init(netConfig: NetConfig) {
self.netConfig = netConfig
}

func create(endpoint: Endpoint) -> URLRequest? {
var components = URLComponents()

components.scheme = NetNetNet.shared.apiConfig?.scheme
components.host = NetNetNet.shared.apiConfig?.host
components.scheme = netConfig.scheme
components.host = netConfig.host
components.path = endpoint.path
components.queryItems = endpoint.queryItems?.map { URLQueryItem(name: $0.key, value: $0.value) }
components.queryItems = endpoint.queryItems

guard let url = components.url else {
return nil
}

var request = URLRequest(url: url)
var request = URLRequest(url: url,
cachePolicy: .returnCacheDataElseLoad)

request.httpMethod = endpoint.method.rawValue
endpoint.headers?.forEach({ request.addValue($1, forHTTPHeaderField: $0) })
Expand Down
65 changes: 65 additions & 0 deletions Tests/NetNetNetTests/Builders/EndpointBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// MocksEndpoint.swift
// NetNetNet
//
// Created by Carlos Cáceres González on 6/11/24.
//

import Foundation
@testable import NetNetNet


final class EndpointBuilder {
private var path: String = ""
private var encoding: BodyEncoding = .json
private var method: HTTPMethods = .get
private var withAuth: Bool = false
private var retries: Int = 0
private var queryItems: [URLQueryItem]?
private var headers: [String : String]?

func path(_ param: String) -> Self {
path = param
return self
}

func encoding(_ param: BodyEncoding) -> Self {
encoding = param
return self
}

func method(_ param: HTTPMethods) -> Self {
method = param
return self
}

func withAuth(_ param: Bool) -> Self {
withAuth = param
return self
}

func retries(_ param: Int) -> Self {
retries = param
return self
}

func queryItems(_ param: [URLQueryItem]?) -> Self {
queryItems = param
return self
}

func headers(_ param: [String : String]?) -> Self {
headers = param
return self
}

func build() -> Endpoint {
.init(path: path,
encoding: encoding,
method: method,
withAuth: withAuth,
retries: retries,
queryItems: queryItems,
headers: headers)
}
}
50 changes: 50 additions & 0 deletions Tests/NetNetNetTests/Builders/NetConfigBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// NetConfigBuilder.swift
// NetNetNet
//
// Created by Carlos Cáceres González on 6/11/24.
//

@testable import NetNetNet

final class NetConfigBuilder {
private var scheme: String = "https"
private var host: String = "somehost.com"
private var clientId: String = ""
private var clientSecret: String = ""
private var oauthEndpoint: String = ""

func scheme(_ param: String) -> Self {
scheme = param
return self
}

func host(_ param: String) -> Self {
host = param
return self
}

func clientId(_ param: String) -> Self {
clientId = param
return self
}

func clientSecret(_ param: String) -> Self {
clientSecret = param
return self
}

func oauthEndpoint(_ param: String) -> Self {
oauthEndpoint = param
return self
}

func build() -> NetConfig {
.init(scheme: scheme,
host: host,
clientId: clientId,
clientSecret: clientSecret,
oauthEndpoint: oauthEndpoint)
}

}
15 changes: 0 additions & 15 deletions Tests/NetNetNetTests/Mocks/MocksURLFactory.swift

This file was deleted.

Loading

0 comments on commit e6b4d75

Please sign in to comment.