diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index f6648b8..70ef9b6 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -17,6 +17,10 @@ jobs: steps: - uses: actions/checkout@v3 + - uses: swift-actions/setup-swift@v1 + with: + swift-version: "5.6" + - uses: actions/cache@v3 id: cache with: diff --git a/Sources/YData/Client/InternalClient+Concurrency.swift b/Sources/YData/Client/InternalClient+Concurrency.swift index e463bf2..9c6482a 100644 --- a/Sources/YData/Client/InternalClient+Concurrency.swift +++ b/Sources/YData/Client/InternalClient+Concurrency.swift @@ -1,7 +1,7 @@ import Foundation import Vapor -public extension InternalClient { +public extension InternalClient { func send(_ request: Request) async throws -> Response { try await send(request).get() } diff --git a/Sources/YData/Client/InternalModel.swift b/Sources/YData/Client/InternalModel.swift index 35a9fa7..cadf6a5 100644 --- a/Sources/YData/Client/InternalModel.swift +++ b/Sources/YData/Client/InternalModel.swift @@ -1,3 +1,3 @@ import Foundation -public typealias InternalModel = Codable +public protocol InternalModel: Codable {} diff --git a/Sources/YData/Client/InternalRequest.swift b/Sources/YData/Client/InternalRequest.swift index 5595624..126419f 100644 --- a/Sources/YData/Client/InternalRequest.swift +++ b/Sources/YData/Client/InternalRequest.swift @@ -1,3 +1,4 @@ +import Foundation import Vapor public protocol InternalRequest { @@ -17,7 +18,7 @@ public extension Internal { public let query: [URLQueryItem]? public var body: ByteBuffer? - + public init(method: HTTPMethod, path: String? = nil, headers: HTTPHeaders? = nil, diff --git a/Sources/YData/Client/InternalResponse.swift b/Sources/YData/Client/InternalResponse.swift index 765cc84..7c8d8df 100644 --- a/Sources/YData/Client/InternalResponse.swift +++ b/Sources/YData/Client/InternalResponse.swift @@ -1,3 +1,4 @@ +import Foundation import Vapor public protocol InternalResponse { @@ -35,12 +36,12 @@ public extension Internal { public let status: HTTPResponseStatus public let message: String } - + struct SuccessResponse: InternalResponse { public var headers: HTTPHeaders public let status: HTTPResponseStatus public var body: ByteBuffer? - + public init(headers: HTTPHeaders, status: HTTPResponseStatus, body: ByteBuffer?) { self.headers = headers self.status = status @@ -57,23 +58,23 @@ public extension Internal.SuccessResponse { private struct _ContentContainer: ContentContainer { var body: ByteBuffer? var headers: HTTPHeaders - + var contentType: HTTPMediaType? { headers.contentType } - + func decode(_ decodable: D.Type, using decoder: ContentDecoder) throws -> D where D : Decodable { guard let body = self.body else { throw Abort(.lengthRequired) } return try decoder.decode(D.self, from: body, headers: self.headers) } - + mutating func encode(_ encodable: E, using encoder: ContentEncoder) throws where E : Encodable { var body = ByteBufferAllocator().buffer(capacity: 0) try encoder.encode(encodable, to: &body, headers: &self.headers) self.body = body } } - + var content: ContentContainer { get { return _ContentContainer(body: self.body, headers: self.headers) diff --git a/Sources/YData/Core/Error/FabricError.swift b/Sources/YData/Core/Error/FabricError.swift index 1bdb847..3016c8c 100644 --- a/Sources/YData/Core/Error/FabricError.swift +++ b/Sources/YData/Core/Error/FabricError.swift @@ -1,6 +1,6 @@ import Foundation -public protocol FabricError: Error, Codable, Equatable { +public protocol FabricError: Error, Codable { var context: [String: String]? { get } var description: String { get } var httpCode: Int { get } @@ -11,13 +11,3 @@ public protocol FabricError: Error, Codable, Equatable { public extension FabricError { var name: String { "\(Self.self)" } } - -public extension FabricError { - static func == (lhs: Self, rhs: Self) -> Bool { - lhs.context == rhs.context - && lhs.description == rhs.description - && lhs.httpCode == rhs.httpCode - && lhs.name == rhs.name - && lhs.returnValue == rhs.returnValue - } -} diff --git a/Sources/YData/Core/Error/GenericFabricError.swift b/Sources/YData/Core/Error/GenericFabricError.swift index 12675e8..e83ca8a 100644 --- a/Sources/YData/Core/Error/GenericFabricError.swift +++ b/Sources/YData/Core/Error/GenericFabricError.swift @@ -16,7 +16,7 @@ public struct GenericFabricError: FabricError { ) { self.context = context self.description = description - if let httpCode { + if let httpCode = httpCode { self.httpCode = httpCode } self.name = name diff --git a/Sources/YData/NIO/ByteBuffer.swift b/Sources/YData/NIO/ByteBuffer.swift index ec4504a..3e7322e 100644 --- a/Sources/YData/NIO/ByteBuffer.swift +++ b/Sources/YData/NIO/ByteBuffer.swift @@ -1,5 +1,5 @@ import NIO extension ByteBuffer { - var string: String? { getString(at: 0, length: readableBytes) } + public var string: String? { getString(at: 0, length: readableBytes) } } diff --git a/Sources/YData/NIO/ByteBufferInputStream+InputStream.swift b/Sources/YData/NIO/ByteBufferInputStream+InputStream.swift new file mode 100644 index 0000000..3214b09 --- /dev/null +++ b/Sources/YData/NIO/ByteBufferInputStream+InputStream.swift @@ -0,0 +1,46 @@ +import Foundation +import NIO + +public final class ByteBufferInputStream: InputStream { + private var byteBuffer: ByteBuffer + public override var streamStatus: Stream.Status { .open } + + init(_ byteBuffer: ByteBuffer) { + self.byteBuffer = byteBuffer + + super.init(data: Data()) + } + + public override func close() { + self.byteBuffer.clear() + } + + public override func read(_ buffer: UnsafeMutablePointer, maxLength len: Int) -> Int { + let length = min(len, byteBuffer.readableBytes) + + guard let bytes = byteBuffer.readBytes(length: length) else { + return 0 + } + + // Convert into a C pointer and assign it to the provided mutable pointer + let writtenBytes = bytes.withUnsafeBufferPointer { arrayPointer -> Int in + arrayPointer.baseAddress.flatMap { pointer -> Int in + buffer.assign(from: pointer, count: length) + return length + } ?? 0 + } + + return writtenBytes + } + + public override func getBuffer( + _ buffer: UnsafeMutablePointer?>, + length len: UnsafeMutablePointer + ) -> Bool { + return false + } + + public override var hasBytesAvailable: Bool { + return self.byteBuffer.readableBytes > 0 + } +}