Skip to content

remove unused degate and make other one public+rename #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 4, 2019
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
44 changes: 3 additions & 41 deletions Sources/AsyncHTTPClient/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,7 @@
import NIO
import NIOHTTP1

public class HandlingHTTPResponseDelegate<T>: HTTPClientResponseDelegate {
struct EmptyEndHandlerError: Error {}

public typealias Result = T

var handleHead: ((HTTPResponseHead) -> Void)?
var handleBody: ((ByteBuffer) -> Void)?
var handleError: ((Error) -> Void)?
var handleEnd: (() throws -> T)?

public func didReceiveHead(task: HTTPClient.Task<T>, _ head: HTTPResponseHead) -> EventLoopFuture<Void> {
if let handler = handleHead {
handler(head)
}
return task.eventLoop.makeSucceededFuture(())
}

public func didReceivePart(task: HTTPClient.Task<T>, _ buffer: ByteBuffer) -> EventLoopFuture<Void> {
if let handler = handleBody {
handler(buffer)
}
return task.eventLoop.makeSucceededFuture(())
}

public func didReceiveError(task: HTTPClient.Task<T>, _ error: Error) {
if let handler = handleError {
handler(error)
}
}

public func didFinishRequest(task: HTTPClient.Task<T>) throws -> T {
if let handler = handleEnd {
return try handler()
}
throw EmptyEndHandlerError()
}
}

final class CopyingDelegate: HTTPClientResponseDelegate {
public final class HTTPClientCopyingDelegate: HTTPClientResponseDelegate {
public typealias Response = Void

let chunkHandler: (ByteBuffer) -> EventLoopFuture<Void>
Expand All @@ -62,11 +24,11 @@ final class CopyingDelegate: HTTPClientResponseDelegate {
self.chunkHandler = chunkHandler
}

func didReceivePart(task: HTTPClient.Task<Void>, _ buffer: ByteBuffer) -> EventLoopFuture<Void> {
public func didReceivePart(task: HTTPClient.Task<Void>, _ buffer: ByteBuffer) -> EventLoopFuture<Void> {
return self.chunkHandler(buffer)
}

func didFinishRequest(task: HTTPClient.Task<Void>) throws {
public func didFinishRequest(task: HTTPClient.Task<Void>) throws {
return ()
}
}
4 changes: 2 additions & 2 deletions Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class HTTPClientInternalTests: XCTestCase {
var request = try Request(url: "http://localhost:\(httpBin.port)/events/10/1")
request.headers.add(name: "Accept", value: "text/event-stream")

let delegate = CopyingDelegate { part in
let delegate = HTTPClientCopyingDelegate { part in
writer.write(.byteBuffer(part))
}
return httpClient.execute(request: request, delegate: delegate).futureResult
Expand Down Expand Up @@ -122,7 +122,7 @@ class HTTPClientInternalTests: XCTestCase {
var request = try Request(url: "http://localhost:\(httpBin.port)/events/10/1")
request.headers.add(name: "Accept", value: "text/event-stream")

let delegate = CopyingDelegate { _ in
let delegate = HTTPClientCopyingDelegate { _ in
httpClient.eventLoopGroup.next().makeFailedFuture(HTTPClientError.invalidProxyResponse)
}
return httpClient.execute(request: request, delegate: delegate).futureResult
Expand Down