Skip to content

Commit

Permalink
Merge pull request #12 from alperen23230/feature/iOS-15-upgrade
Browse files Browse the repository at this point in the history
Upgrade iOS version to 15
  • Loading branch information
alperen23230 authored Jul 13, 2024
2 parents 0043739 + 1134c00 commit 18c0986
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 97 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- uses: swift-actions/setup-swift@v1
- uses: swift-actions/setup-swift@v2
with:
swift-version: "5.10.0"
- name: Build
run: swift build -v
- name: Run tests
Expand Down
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// swift-tools-version: 5.5
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "CleanNetwork",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6)
.iOS(.v15),
.macOS(.v12),
.tvOS(.v15),
.watchOS(.v8)
],
products: [
.library(
Expand Down
4 changes: 1 addition & 3 deletions Sources/CleanNetwork/Core/CLEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public extension CLEndpoint {
components.queryItems = queryItems

guard let url = components.url else {
preconditionFailure(
"Invalid URL components: \(components)"
)
preconditionFailure("Invalid URL components: \(components)")
}

return url
Expand Down
6 changes: 5 additions & 1 deletion Sources/CleanNetwork/Core/CLNetworkConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public class CLNetworkConfig: NetworkConfig {
public var decoder = JSONDecoder()
public var encoder = JSONEncoder()
public var urlSession = URLSession.shared
public var loggerEnabled = true
public var loggerEnabled = true {
didSet {
CLNetworkLogger.loggerEnabled = loggerEnabled
}
}
public var sharedHeaders: [String: String] = [:]

public init() {}
Expand Down
18 changes: 14 additions & 4 deletions Sources/CleanNetwork/Core/CLNetworkLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import Foundation

struct CLNetworkLogger {
static var loggerEnabled = true

static func log(request: URLRequest) {
guard loggerEnabled else { return }

print("\n - - - - - - - - - - OUTGOING - - - - - - - - - - \n")
defer { print("\n - - - - - - - - - - END - - - - - - - - - - \n") }

Expand Down Expand Up @@ -38,7 +42,9 @@ struct CLNetworkLogger {
print(output)
}

static func log(data: Data?, response: HTTPURLResponse?, error: Error?) {
static func log(data: Data?, response: HTTPURLResponse?) {
guard loggerEnabled else { return }

print("\n - - - - - - - - - - INCOMMING - - - - - - - - - - \n")
defer { print("\n - - - - - - - - - - END - - - - - - - - - - \n") }

Expand Down Expand Up @@ -68,13 +74,17 @@ struct CLNetworkLogger {
if let body = data {
output += "\n\(String(data: body, encoding: .utf8) ?? "")\n"
}
if error != nil {
output += "\nError: \(error!.localizedDescription)\n"
}
print(output)
}

static func logError(with error: Error) {
guard loggerEnabled else { return }
print("\nError: \(error.localizedDescription)\n")
}

static func logDecodingError(with error: DecodingError) {
guard loggerEnabled else { return }

print("\n\nDecoding Error\n")
switch error {
case .typeMismatch(let type, let context):
Expand Down
112 changes: 30 additions & 82 deletions Sources/CleanNetwork/Core/CLNetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation

public struct CLNetworkService: NetworkService {

public static var shared = CLNetworkService()

public var config: NetworkConfig = CLNetworkConfig()
Expand All @@ -17,106 +16,55 @@ public struct CLNetworkService: NetworkService {
private init() {}

public func fetch<T: CLNetworkBodyRequest>(_ request: T) async throws -> T.ResponseType {
var urlRequest = URLRequest(url: request.endpoint.url)
let allHeaders = config.sharedHeaders.merging(request.headers) { (_, new) in new }
urlRequest.allHTTPHeaderFields = allHeaders
urlRequest.httpMethod = request.method.rawValue
let requestBody = try config.encoder.encode(request.requestBody)
urlRequest.httpBody = requestBody
let urlRequest = try request.build(encoder: config.encoder, with: config.sharedHeaders)
return try await fetch(urlRequest: urlRequest)
}

public func fetch<T: CLNetworkDecodableRequest>(_ request: T) async throws -> T.ResponseType {
var urlRequest = URLRequest(url: request.endpoint.url)
let allHeaders = config.sharedHeaders.merging(request.headers) { (_, new) in new }
urlRequest.allHTTPHeaderFields = allHeaders
urlRequest.httpMethod = request.method.rawValue
return try await fetch(urlRequest: urlRequest)
return try await fetch(urlRequest: request.build(with: config.sharedHeaders))
}

public func fetch<T: CLNetworkRequest>(_ request: T) async throws -> Data {
var urlRequest = URLRequest(url: request.endpoint.url)
let allHeaders = config.sharedHeaders.merging(request.headers) { (_, new) in new }
urlRequest.allHTTPHeaderFields = allHeaders
urlRequest.httpMethod = request.method.rawValue
return try await fetchData(urlRequest: urlRequest)
return try await fetch(urlRequest: request.build(with: config.sharedHeaders))
}

func fetch<T: Decodable>(urlRequest: URLRequest) async throws -> T {
if config.loggerEnabled {
CLNetworkLogger.log(request: urlRequest)
}
let data = try await fetch(urlRequest: urlRequest)

let data: T = try await withCheckedThrowingContinuation { continuation in
self.config.urlSession.dataTask(with: urlRequest) { (data, response, error) in
if config.loggerEnabled, let urlResponse = response as? HTTPURLResponse {
CLNetworkLogger.log(data: data, response: urlResponse, error: error)
}

if let error = error {
continuation.resume(throwing: error)
} else {
guard let data = data else {
continuation.resume(throwing: CLError.errorMessage(.dataIsNil))
return
}
do {
guard let urlResponse = response as? HTTPURLResponse,
successRange.contains(urlResponse.statusCode) else {
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
continuation.resume(throwing: CLError.apiError(data, statusCode))
} else {
continuation.resume(throwing: CLError.errorMessage(.statusCodeIsNotValid))
}
return
}
let decodedData = try self.config.decoder.decode(T.self, from: data)
continuation.resume(returning: decodedData)
} catch {
if config.loggerEnabled, let error = error as? DecodingError {
CLNetworkLogger.logDecodingError(with: error)
}
continuation.resume(throwing: error)
}
}
do {
return try config.decoder.decode(T.self, from: data)
} catch {
if config.loggerEnabled, let error = error as? DecodingError {
CLNetworkLogger.logDecodingError(with: error)
}
.resume()

throw error
}
return data
}

func fetchData(urlRequest: URLRequest) async throws -> Data {
if config.loggerEnabled {
CLNetworkLogger.log(request: urlRequest)
}
func fetch(urlRequest: URLRequest) async throws -> Data {
CLNetworkLogger.log(request: urlRequest)

let data: Data = try await withCheckedThrowingContinuation { continuation in
self.config.urlSession.dataTask(with: urlRequest) { (data, response, error) in
if config.loggerEnabled, let urlResponse = response as? HTTPURLResponse {
CLNetworkLogger.log(data: data, response: urlResponse, error: error)
}

if let error = error {
continuation.resume(throwing: error)
do {
let (data, response) = try await config.urlSession.data(for: urlRequest)

if let urlResponse = response as? HTTPURLResponse {
CLNetworkLogger.log(data: data, response: urlResponse)
}

guard let urlResponse = response as? HTTPURLResponse,
successRange.contains(urlResponse.statusCode) else {
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
throw CLError.apiError(data, statusCode)
} else {
guard let data = data else {
continuation.resume(throwing: CLError.errorMessage(.dataIsNil))
return
}
guard let urlResponse = response as? HTTPURLResponse,
successRange.contains(urlResponse.statusCode) else {
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
continuation.resume(throwing: CLError.apiError(data, statusCode))
} else {
continuation.resume(throwing: CLError.errorMessage(.statusCodeIsNotValid))
}
return
}
continuation.resume(returning: data)
throw CLError.errorMessage(.statusCodeIsNotValid)
}
}
.resume()

return data
} catch {
CLNetworkLogger.logError(with: error)
throw error
}
return data
}
}
13 changes: 13 additions & 0 deletions Sources/CleanNetwork/Protocols/CLNetworkBodyRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@
// Created by Alperen Ünal on 3.07.2022.
//

import Foundation

public protocol CLNetworkBodyRequest: CLNetworkDecodableRequest {
associatedtype RequestBodyType: Encodable

var requestBody: RequestBodyType { get }

func build(encoder: JSONEncoder, with sharedHeaders: [String: String]) throws -> URLRequest
}

public extension CLNetworkBodyRequest {
var method: CLHTTPMethod { .post }
}

public extension CLNetworkBodyRequest {
func build(encoder: JSONEncoder, with sharedHeaders: [String: String]) throws -> URLRequest {
var urlRequest = build(with: sharedHeaders)
urlRequest.httpBody = try encoder.encode(requestBody)

return urlRequest
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
// Created by Alperen Ünal on 28.05.2022.
//

import Foundation

public protocol CLNetworkDecodableRequest: CLNetworkRequest {
associatedtype ResponseType: Decodable

var endpoint: CLEndpoint { get }
var method: CLHTTPMethod { get }
var headers: [String: String] { get }

func build(with sharedHeaders: [String: String]) -> URLRequest
}
15 changes: 15 additions & 0 deletions Sources/CleanNetwork/Protocols/CLNetworkRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@
// Created by Alperen Ünal on 7.08.2022.
//

import Foundation

public protocol CLNetworkRequest {
var endpoint: CLEndpoint { get }
var method: CLHTTPMethod { get }
var headers: [String: String] { get }

func build(with sharedHeaders: [String: String]) -> URLRequest
}

public extension CLNetworkRequest {
var method: CLHTTPMethod { .get }
var headers: [String: String] { .init() }
}

public extension CLNetworkDecodableRequest {
func build(with sharedHeaders: [String: String]) -> URLRequest {
var urlRequest = URLRequest(url: endpoint.url)
let allHeaders = sharedHeaders.merging(headers) { (_, new) in new }
urlRequest.allHTTPHeaderFields = allHeaders
urlRequest.httpMethod = method.rawValue

return urlRequest
}
}
1 change: 0 additions & 1 deletion Sources/CleanNetwork/Protocols/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ protocol NetworkService {
func fetch<T: CLNetworkDecodableRequest>(_ request: T) async throws -> T.ResponseType
func fetch<T: CLNetworkRequest>(_ request: T) async throws -> Data
func fetch<T: Decodable>(urlRequest: URLRequest) async throws -> T
func fetchData(urlRequest: URLRequest) async throws -> Data
}

0 comments on commit 18c0986

Please sign in to comment.