Skip to content

Commit

Permalink
Updated With Publisher Kit Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Raghav Ahuja committed Dec 29, 2019
1 parent 23a8d21 commit d61d332
Show file tree
Hide file tree
Showing 60 changed files with 439 additions and 2,566 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "PublisherKit",
"repositoryURL": "https://github.com/ragzy15/PublisherKit",
"state": {
"branch": null,
"revision": "ad211780f0f532a473e3d5d1cef2a2644024d7c4",
"version": "1.0.0"
}
}
]
},
"version": 1
}
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let package = Package(
targets: ["NetworkKit"]),
],
dependencies: [
.package(url: "https://github.com/ragzy15/PublisherKit", from: .init(1, 0, 0))
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
Expand All @@ -26,7 +27,7 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "NetworkKit",
dependencies: []),
dependencies: ["PublisherKit"]),
.testTarget(
name: "NetworkKitTests",
dependencies: ["NetworkKit"]),
Expand Down
16 changes: 8 additions & 8 deletions Sources/NetworkKit/Create Request/Create Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct CreateRequest {

public let request: URLRequest

public init?(with connection: ConnectionRepresentable, query urlQuery: URLQuery?) {
public init?(with connection: ConnectionRepresentable, query urlQuery: Set<URLQueryItem>, body: Data?, headers: HTTPHeaderParameters) {

var components = URLComponents()
components.scheme = connection.scheme.rawValue
Expand All @@ -26,15 +26,15 @@ public struct CreateRequest {
components.host = (subURL.isEmpty ? subURL : subURL + ".") + connection.host.host
components.path = endPoint + connection.path

var queryItems: [URLQueryItem] = []
queryItems.addURLQuery(query: urlQuery)
var queryItems = Set<URLQueryItem>()
queryItems.addURLQuery(query: connection.defaultQuery)
queryItems.addURLQuery(query: connection.host.defaultUrlQuery)
queryItems = queryItems.union(urlQuery)

let method = connection.method

if !queryItems.isEmpty {
components.queryItems = queryItems
components.queryItems = Array(queryItems)
}

guard let url = components.url else {
Expand All @@ -47,15 +47,15 @@ public struct CreateRequest {
let defaultHeaderFields = connection.host.defaultHeaders
let connectionHeaderFields = connection.httpHeaders

let headerFields = defaultHeaderFields.merging(connectionHeaderFields) { (_, new) in new }
var headerFields = defaultHeaderFields.merging(connectionHeaderFields) { (_, new) in new }
headerFields.merge(headers) { (_, new) in new }

if !headerFields.isEmpty {
urlRequest.allHTTPHeaderFields = headerFields
}

urlRequest.httpBody = body
request = urlRequest

#if DEBUG
DebugPrint.logAPIRequest(request: request, apiName: connection.name)
#endif
}
}
42 changes: 0 additions & 42 deletions Sources/NetworkKit/Debug Loging/Debug Logging.swift

This file was deleted.

226 changes: 226 additions & 0 deletions Sources/NetworkKit/Debug Loging/NKLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
//
// NKLogger.swift
// NetworkKit
//
// Created by Raghav Ahuja on 18/10/19.
// Copyright © 2019 Raghav Ahuja. All rights reserved.
//

import Foundation

final public class NKLogger {

/// Allows Logs to be Printed in Debug Console.
/// Default value is `true`
public var isLoggingEnabled: Bool = true

public static let `default` = NKLogger()

/**
Creates a `NKLogger`.
*/
public init() { }

/**
Writes the textual representations of the given items into the standard output.
- parameter items: Zero or more items to print..
- parameter separator: A string to print between each item. The default is a single space (" ").
- parameter terminator: The string to print after all items have been printed. The default is a newline ("\n").
*/
func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return }
Swift.print(items, separator: separator, terminator: terminator)
#endif
}

/**
Writes the textual representations of the given items most suitable for debugging into the standard output.
- parameter items: Zero or more items to print.
- parameter separator: A string to print between each item. The default is a single space (" ").
- parameter terminator: The string to print after all items have been printed. The default is a newline ("\n").
*/
func debugPrint(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return }
Swift.debugPrint(items, separator: separator, terminator: terminator)
#endif
}

/**
Handles APIRequest logging sent by the `NetworkKit`.
- parameter request: URLRequest
- parameter apiName: API name.
*/
func logAPIRequest(request: URLRequest?, apiName: String?) {
#if DEBUG
guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return }

Swift.print(
"""
------------------------------------------------------------
API Call Request for:
Name: \(apiName ?? "nil")
\(request?.debugDescription ?? "")
"""
)
#endif
}

/**
Print JSON sent by the `NetworkKit`.
- parameter data: Input Type to be printed
- parameter apiName: API name.
*/
func printJSON<Input>(data: Input, apiName: String) {
#if DEBUG
guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return }
guard let data = data as? Data else {
return
}

do {
let object = try JSONSerialization.jsonObject(with: data, options: [])
let newData = try JSONSerialization.data(withJSONObject: object, options: .prettyPrinted)

// Swift.print(
// """
// ------------------------------------------------------------
// Printing JSON for:
// API Name: \(apiName)
// JSON:
//
// """)
Swift.print("""
------------------------------------------------------------
JSON:
""")
Swift.print(String(data: newData, encoding: .utf8) ?? "nil")
Swift.print("------------------------------------------------------------")

} catch {

}
#endif
}

/**
Handles errors sent by the `NetworkKit`.
- parameter error: Error occurred.
- parameter file: Source file name.
- parameter line: Source line number.
- parameter function: Source function name.
*/
@inline(__always)
func log(error: Error, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
#if DEBUG
guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return }

Swift.print("⚠️ [NetworkKit: Error] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(error as NSError)\n")
#endif
}

/**
Handles assertions made throughout the `NetworkKit`.
- parameter condition: Assertion condition.
- parameter message: Assertion failure message.
- parameter file: Source file name.
- parameter line: Source line number.
- parameter function: Source function name.
*/
@inline(__always)
func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {

#if DEBUG
let condition = condition()

if condition { return }

let message = message()

Swift.print("❗ [NetworkKit: Assertion Failure] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n")
Swift.assert(condition, message, file: file, line: line)
#endif
}

/**
Handles assertion failures made throughout the `NetworkKit`.
- parameter message: Assertion failure message.
- parameter file: Source file name.
- parameter line: Source line number.
- parameter function: Source function name.
*/
@inlinable public func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
let message = message()
Swift.print("❗ [NetworkKit: Assertion Failure] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n")
Swift.assertionFailure(message, file: file, line: line)
}

/**
Handles precondition failures made throughout the `NetworkKit`.
- parameter message: Assertion failure message.
- parameter file: Source file name.
- parameter line: Source line number.
- parameter function: Source function name.
*/
@inlinable public func preconditionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line, function: StaticString = #function) -> Never {
let message = message()
Swift.print("❗ [NetworkKit: Assertion Failure] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n")
Swift.preconditionFailure(message, file: file, line: line)
}

/**
Handles preconditions made throughout the `NetworkKit`.
- parameter condition: Precondition to be satisfied.
- parameter message: Precondition failure message.
- parameter file: Source file name.
- parameter line: Source line number.
- parameter function: Source function name.
*/
@inline(__always)
func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {

#if DEBUG
let condition = condition()

if condition { return }

let message = message()

Swift.print("❗ [NetworkKit: Precondition Failure] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n")
Swift.preconditionFailure(message, file: file, line: line)
#endif
}

/**
Handles fatal errors made throughout the `NetworkKit`.
- Important: Implementers should guarantee that this function doesn't return, either by calling another `Never` function such as `fatalError()` or `abort()`, or by raising an exception.
- parameter message: Fatal error message.
- parameter file: Source file name.
- parameter line: Source line number.
- parameter function: Source function name.
*/
@inline(__always)
func fatalError(_ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) -> Never {

#if DEBUG
let message = message()
Swift.print("❗ [NetworkKit: Fatal Error] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n")
Swift.fatalError(message, file: file, line: line)
#endif
}
}
29 changes: 0 additions & 29 deletions Sources/NetworkKit/Errors/Business Error.swift

This file was deleted.

Loading

0 comments on commit d61d332

Please sign in to comment.