Skip to content

Commit

Permalink
fix(http-client): internal response backward compatibility (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoaguimaraes authored Apr 26, 2022
1 parent c4cf167 commit 31a0070
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Sources/YData/Client/InternalResponse+Vapor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public extension EventLoopFuture where Value: InternalResponse {
flatMapThrowing { try callback($0.content) }
}

func mapContent<NewValue>(_ callback: @escaping (ContentContainer) throws -> (NewValue))
-> EventLoopFuture<Value> where NewValue: Content { flatMapThrowing { try $0.map(callback) } }

func mapToContent<R>() -> EventLoopFuture<R> where R: Decodable {
flatMapThrowing { response -> R in try response.content.decode(R.self) }
}
Expand All @@ -26,6 +29,11 @@ public extension EventLoopFuture where Value: InternalResponse {
-> EventLoopFuture<NewValue> where NewValue: Content, E: Error { flatMapResult { callback($0.content) } }
}

extension EventLoop {
public func tryFlatSubmit<NewValue: InternalResponse>(_ callback: @escaping () throws -> EventLoopFuture<NewValue>)
-> EventLoopFuture<NewValue> { self.submit(callback).flatMap { $0 } }
}

extension ClientResponse: InternalResponse {
public init(headers: HTTPHeaders, status: HTTPResponseStatus, body: ByteBuffer?) {
self.init(status: status, headers: headers, body: body)
Expand Down Expand Up @@ -68,3 +76,11 @@ public extension ClientResponse {
}
}
}

extension Internal.SuccessResponse: ResponseEncodable {
public func encodeResponse(for request: Request) -> EventLoopFuture<Vapor.Response> {
let response = Vapor.Response(status: status, headers: headers)
response.body ?= body.flatMap(Vapor.Response.Body.init)
return request.eventLoop.makeSucceededFuture(response)
}
}
21 changes: 20 additions & 1 deletion Sources/YData/Client/InternalResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@ public protocol InternalResponse {
var status: HTTPResponseStatus { get }
var body: ByteBuffer? { get set }

var content: ContentContainer { get }
var content: ContentContainer { get set }

init(headers: HTTPHeaders, status: HTTPResponseStatus, body: ByteBuffer?)
}

public extension InternalResponse {
@inlinable
func map<NewValue>(_ callback: (ContentContainer) throws -> (NewValue)) throws -> Self where NewValue: Content {
let newValue = try callback(content)

var newResponse = Self.init(headers: headers, status: status, body: nil)
try newResponse.content.encode(newValue)

return newResponse
}

@inlinable
func map(_ callback: (ContentContainer) throws -> (Void)) throws -> Self {
let _ = try callback(content)

return Self.init(headers: headers, status: status, body: nil)
}
}

public extension Internal {
struct ErrorResponse: Error {
public let headers: HTTPHeaders
Expand Down

0 comments on commit 31a0070

Please sign in to comment.