diff --git a/Sources/Request/Request/Request.swift b/Sources/Request/Request/Request.swift index 867b41a..a33dcd5 100644 --- a/Sources/Request/Request/Request.swift +++ b/Sources/Request/Request/Request.swift @@ -43,7 +43,7 @@ public typealias Request = AnyRequest public struct AnyRequest where ResponseType: Decodable { public let combineIdentifier = CombineIdentifier() - private var rootParam: RequestParam + internal var rootParam: RequestParam internal var onData: ((Data) -> Void)? internal var onString: ((String) -> Void)? @@ -97,6 +97,13 @@ public struct AnyRequest where ResponseType: Decodable { modify { $0.onStatusCode = callback } } + /// Modifies self to contain the procided Auth struct in its headers + public mutating func withAuthorization(_ authorization: Auth) -> Self { + self.rootParam = CombinedParams(children: [Header.Authorization(authorization), + rootParam]) + return self + } + /// Performs the `Request`, and calls the `onData`, `onString`, `onJson`, and `onError` callbacks when appropriate. public func call() { buildPublisher() @@ -142,6 +149,12 @@ public struct AnyRequest where ResponseType: Decodable { } } +extension AnyRequest: Identifiable { + public var id: String { + buildSession().request.url!.absoluteString + } +} + extension AnyRequest: Equatable { public static func == (lhs: AnyRequest, rhs: AnyRequest) -> Bool { let lhsSession = lhs.buildSession() @@ -150,3 +163,48 @@ extension AnyRequest: Equatable { } } +extension AnyRequest { + public func prettyJson() -> String { + + let session = self.buildSession() + let request = session.request + let method = request.httpMethod ?? "WTF" + let url = request.url?.absoluteString ?? "" + let headers = request.allHTTPHeaderFields + let body = request.httpBody ?? Data() + let jh = Json(headers ?? [:]).stringified ?? "No Headers" + + return """ + Beginning of Request. + ---------------------------------- + Endpoint: \(method.uppercased()) \(url) + __________________________________ + Headers: \(jh) + __________________________________ + Body: \(body.prettyJSON()) + ___________________________________ + End Of Request. + """ + + } +} + +extension Data { + func toString() -> String { + return String(data:self, encoding: .utf8) ?? "" + } + func prettyJSON() -> String { + do { + let json = try JSONSerialization.jsonObject(with: self, options: []) + let data = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) + guard let jsonString = String(data: data, encoding: .utf8) else { + print("Inavlid data") + return "" + } + return jsonString + } catch { + print("Data+prettyJSON | Error: \(error.localizedDescription)") + } + return "" + } +} diff --git a/Sources/Request/Request/RequestParams/CombinedParams.swift b/Sources/Request/Request/RequestParams/CombinedParams.swift index 0e0ad40..039f344 100644 --- a/Sources/Request/Request/RequestParams/CombinedParams.swift +++ b/Sources/Request/Request/RequestParams/CombinedParams.swift @@ -7,14 +7,14 @@ import Foundation -internal struct CombinedParams: RequestParam, SessionParam { - fileprivate let children: [RequestParam] +public struct CombinedParams: RequestParam, SessionParam { + public let children: [RequestParam] init(children: [RequestParam]) { self.children = children } - func buildParam(_ request: inout URLRequest) { + public func buildParam(_ request: inout URLRequest) { children .sorted { a, _ in (a is Url) } .filter { !($0 is SessionParam) || $0 is CombinedParams } @@ -23,7 +23,7 @@ internal struct CombinedParams: RequestParam, SessionParam { } } - func buildConfiguration(_ configuration: URLSessionConfiguration) { + public func buildConfiguration(_ configuration: URLSessionConfiguration) { children .compactMap { $0 as? SessionParam } .forEach {