Skip to content

Commit

Permalink
naming
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawright11 committed Jun 12, 2024
1 parent eae1f85 commit c563600
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 46 deletions.
14 changes: 7 additions & 7 deletions Papyrus/Sources/Extensions/URLSession+Papyrus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ extension Provider {
// MARK: `HTTPService` Conformance

extension URLSession: HTTPService {
public func build(method: String, url: URL, headers: [String: String], body: Data?) -> Request {
public func build(method: String, url: URL, headers: [String: String], body: Data?) -> PapyrusRequest {
var request = URLRequest(url: url)
request.httpMethod = method
request.httpBody = body
request.allHTTPHeaderFields = headers
return request
}

public func request(_ req: Request) async -> Response {
public func request(_ req: PapyrusRequest) async -> PapyrusResponse {
#if os(Linux) // Linux doesn't have access to async URLSession APIs
await withCheckedContinuation { continuation in
let urlRequest = req.urlRequest
Expand All @@ -46,16 +46,16 @@ extension URLSession: HTTPService {

// MARK: `Response` Conformance

extension Response {
extension PapyrusResponse {
public var urlRequest: URLRequest { (self as! _Response).urlRequest }
public var urlResponse: URLResponse? { (self as! _Response).urlResponse }
}

private struct _Response: Response {
private struct _Response: PapyrusResponse {
let urlRequest: URLRequest
let urlResponse: URLResponse?

var request: Request? { urlRequest }
var request: PapyrusRequest? { urlRequest }
let error: Error?
let body: Data?
let headers: [String: String]?
Expand Down Expand Up @@ -85,13 +85,13 @@ private struct _Response: Response {

// MARK: `Request` Conformance

extension Request {
extension PapyrusRequest {
public var urlRequest: URLRequest {
(self as! URLRequest)
}
}

extension URLRequest: Request {
extension URLRequest: PapyrusRequest {
public var body: Data? {
get { httpBody }
set { httpBody = newValue }
Expand Down
4 changes: 2 additions & 2 deletions Papyrus/Sources/HTTPService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Foundation
/// A type that can perform arbitrary HTTP requests.
public protocol HTTPService {
/// Build a `Request` from the given components.
func build(method: String, url: URL, headers: [String: String], body: Data?) -> Request
func build(method: String, url: URL, headers: [String: String], body: Data?) -> PapyrusRequest

/// Concurrency based API
func request(_ req: Request) async -> Response
func request(_ req: PapyrusRequest) async -> PapyrusResponse
}
4 changes: 2 additions & 2 deletions Papyrus/Sources/Interceptors/CurlInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct CurlLogger {
}

extension CurlLogger: Interceptor {
public func intercept(req: Request, next: Next) async throws -> Response {
public func intercept(req: PapyrusRequest, next: Next) async throws -> PapyrusResponse {
if condition == .always {
logHandler(req.curl(sortedHeaders: true))
}
Expand All @@ -40,7 +40,7 @@ extension CurlLogger: Interceptor {
}
}

public extension Request {
public extension PapyrusRequest {
/// Create a cURL command from this instance
///
/// - Parameter sortedHeaders: sort headers in output
Expand Down
6 changes: 3 additions & 3 deletions Papyrus/Sources/PapyrusError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ public struct PapyrusError: Error, CustomDebugStringConvertible {
/// What went wrong.
public let message: String
/// Error related request.
public let request: Request?
public let request: PapyrusRequest?
/// Error related response.
public let response: Response?
public let response: PapyrusResponse?

/// Create an error with the specified message.
///
/// - Parameter message: What went wrong.
/// - Parameter request: Error related request.
/// - Parameter response: Error related response.
public init(_ message: String, _ request: Request? = nil, _ response: Response? = nil) {
public init(_ message: String, _ request: PapyrusRequest? = nil, _ response: PapyrusResponse? = nil) {
self.message = message
self.request = request
self.response = response
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public protocol Request {
public protocol PapyrusRequest {
var url: URL? { get set }
var method: String { get set }
var headers: [String: String] { get set }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Foundation

public protocol Response {
var request: Request? { get }
public protocol PapyrusResponse {
var request: PapyrusRequest? { get }
var body: Data? { get }
var headers: [String: String]? { get }
var statusCode: Int? { get }
var error: Error? { get }
}

extension Response {
extension PapyrusResponse {
/// Validates the status code of a Response, as well as any transport errors that may have occurred.
@discardableResult
public func validate() throws -> Self {
Expand Down Expand Up @@ -50,20 +50,20 @@ extension Response {
}
}

extension Response where Self == ErrorResponse {
public static func error(_ error: Error) -> Response {
extension PapyrusResponse where Self == ErrorResponse {
public static func error(_ error: Error) -> PapyrusResponse {
ErrorResponse(error)
}
}

public struct ErrorResponse: Response {
public struct ErrorResponse: PapyrusResponse {
let _error: Error?

public init(_ error: Error) {
self._error = error
}

public var request: Request? { nil }
public var request: PapyrusRequest? { nil }
public var body: Data? { nil }
public var headers: [String : String]? { nil }
public var statusCode: Int? { nil }
Expand Down
16 changes: 8 additions & 8 deletions Papyrus/Sources/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public final class Provider {
}

@discardableResult
public func intercept(action: @escaping (Request, (Request) async throws -> Response) async throws -> Response) -> Self {
public func intercept(action: @escaping (PapyrusRequest, (PapyrusRequest) async throws -> PapyrusResponse) async throws -> PapyrusResponse) -> Self {
struct AnonymousInterceptor: Interceptor {
let action: (Request, Interceptor.Next) async throws -> Response
let action: (PapyrusRequest, Interceptor.Next) async throws -> PapyrusResponse

func intercept(req: Request, next: Interceptor.Next) async throws -> Response {
func intercept(req: PapyrusRequest, next: Interceptor.Next) async throws -> PapyrusResponse {
try await action(req, next)
}
}
Expand All @@ -46,9 +46,9 @@ public final class Provider {
}

@discardableResult
public func request(_ builder: inout RequestBuilder) async throws -> Response {
public func request(_ builder: inout RequestBuilder) async throws -> PapyrusResponse {
let request = try createRequest(&builder)
var next: (Request) async throws -> Response = http.request
var next: (PapyrusRequest) async throws -> PapyrusResponse = http.request
for interceptor in interceptors.reversed() {
let _next = next
next = { try await interceptor.intercept(req: $0, next: _next) }
Expand All @@ -57,7 +57,7 @@ public final class Provider {
return try await next(request)
}

private func createRequest(_ builder: inout RequestBuilder) throws -> Request {
private func createRequest(_ builder: inout RequestBuilder) throws -> PapyrusRequest {
for modifier in modifiers {
try modifier.modify(req: &builder)
}
Expand All @@ -69,8 +69,8 @@ public final class Provider {
}

public protocol Interceptor {
typealias Next = (Request) async throws -> Response
func intercept(req: Request, next: Next) async throws -> Response
typealias Next = (PapyrusRequest) async throws -> PapyrusResponse
func intercept(req: PapyrusRequest, next: Next) async throws -> PapyrusResponse
}

public protocol RequestModifier {
Expand Down
13 changes: 6 additions & 7 deletions Papyrus/Tests/APITests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import XCTest
import Papyrus
@testable import Papyrus

final class APITests: XCTestCase {
Expand Down Expand Up @@ -121,28 +120,28 @@ fileprivate class _HTTPServiceMock: HTTPService {
_responseType = responseType
}

func build(method: String, url: URL, headers: [String : String], body: Data?) -> Request {
func build(method: String, url: URL, headers: [String : String], body: Data?) -> PapyrusRequest {
_Request(method: "", headers: [:])
}

func request(_ req: Papyrus.Request) async -> Papyrus.Response {
func request(_ req: PapyrusRequest) async -> PapyrusResponse {
_Response(body: _responseType.value?.data(using: .utf8), statusCode: 200)
}

func request(_ req: Papyrus.Request, completionHandler: @escaping (Papyrus.Response) -> Void) {
func request(_ req: PapyrusRequest, completionHandler: @escaping (PapyrusResponse) -> Void) {
completionHandler(_Response(body: "".data(using: .utf8)))
}
}

fileprivate struct _Request: Request {
fileprivate struct _Request: PapyrusRequest {
var url: URL?
var method: String
var headers: [String : String]
var body: Data?
}

fileprivate struct _Response: Response {
var request: Papyrus.Request?
fileprivate struct _Response: PapyrusResponse {
var request: PapyrusRequest?
var body: Data?
var headers: [String : String]?
var statusCode: Int?
Expand Down
6 changes: 3 additions & 3 deletions Papyrus/Tests/CurlTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ final class CurlTests: XCTestCase {
}
}

private struct TestResponse: Response {
var request: Request? = nil
private struct TestResponse: PapyrusResponse {
var request: PapyrusRequest? = nil
var body: Data? = nil
var headers: [String : String]? = nil
var statusCode: Int? = nil
var error: Error? = nil
}

private struct TestRequest: Request {
private struct TestRequest: PapyrusRequest {
var method: String
var url: URL?
var headers: [String : String]
Expand Down
6 changes: 3 additions & 3 deletions Papyrus/Tests/ProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ final class ProviderTests: XCTestCase {
}

private struct TestHTTPService: HTTPService {
func build(method: String, url: URL, headers: [String : String], body: Data?) -> Request {
func build(method: String, url: URL, headers: [String : String], body: Data?) -> PapyrusRequest {
fatalError()
}

func request(_ req: Request) async -> Response {
func request(_ req: PapyrusRequest) async -> PapyrusResponse {
fatalError()
}

func request(_ req: Request, completionHandler: @escaping (Response) -> Void) {
func request(_ req: PapyrusRequest, completionHandler: @escaping (PapyrusResponse) -> Void) {

}
}
4 changes: 2 additions & 2 deletions Papyrus/Tests/ResponseDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ fileprivate struct _Person: Decodable {
let name: String
}

fileprivate class _Response : Response {
var request: Papyrus.Request?
fileprivate class _Response : PapyrusResponse {
var request: PapyrusRequest?
var body: Data?
var headers: [String : String]?
var statusCode: Int?
Expand Down
2 changes: 1 addition & 1 deletion Papyrus/Tests/ResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class ResponseTests: XCTestCase {
case test
}

let res: Response = .error(TestError.test)
let res: PapyrusResponse = .error(TestError.test)
XCTAssertThrowsError(try res.validate())
}
}

0 comments on commit c563600

Please sign in to comment.