Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Sources/YData/Client/InternalClient+Concurrency.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Vapor

public extension InternalClient {
public extension InternalClient {
func send<Request: InternalRequest, Response: InternalResponse>(_ request: Request) async throws -> Response {
try await send(request).get()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/YData/Client/InternalModel.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Foundation

public typealias InternalModel = Codable
public protocol InternalModel: Codable {}
3 changes: 2 additions & 1 deletion Sources/YData/Client/InternalRequest.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import Vapor

public protocol InternalRequest {
Expand All @@ -17,7 +18,7 @@ public extension Internal {
public let query: [URLQueryItem]?

public var body: ByteBuffer?

public init<C: Content>(method: HTTPMethod,
path: String? = nil,
headers: HTTPHeaders? = nil,
Expand Down
13 changes: 7 additions & 6 deletions Sources/YData/Client/InternalResponse.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import Vapor

public protocol InternalResponse {
Expand Down Expand Up @@ -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
Expand All @@ -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<D>(_ 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<E>(_ 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)
Expand Down
12 changes: 1 addition & 11 deletions Sources/YData/Core/Error/FabricError.swift
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion Sources/YData/Core/Error/GenericFabricError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/YData/NIO/ByteBuffer.swift
Original file line number Diff line number Diff line change
@@ -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) }
}
46 changes: 46 additions & 0 deletions Sources/YData/NIO/ByteBufferInputStream+InputStream.swift
Original file line number Diff line number Diff line change
@@ -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<UInt8>, 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<UnsafeMutablePointer<UInt8>?>,
length len: UnsafeMutablePointer<Int>
) -> Bool {
return false
}

public override var hasBytesAvailable: Bool {
return self.byteBuffer.readableBytes > 0
}
}