Skip to content

Commit

Permalink
Merge pull request #15 from swift-server/jo/hb2-beta
Browse files Browse the repository at this point in the history
Remove the 'HB' prefix
  • Loading branch information
Joannis committed Mar 14, 2024
2 parents 8007a5f + 14bdd5a commit 9686d8a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Expand Up @@ -13,7 +13,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-alpha.2"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.1"),
],
targets: [
.target(
Expand All @@ -27,7 +27,7 @@ let package = Package(
name: "OpenAPIHummingbirdTests",
dependencies: [
"OpenAPIHummingbird",
.product(name: "HummingbirdXCT", package: "hummingbird"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -4,7 +4,7 @@ Hummingbird transport for [OpenAPI generator](https://github.com/apple/swift-ope

```swift
// Create your router.
let router = HBRouter()
let router = Router()

// Create an instance of your handler type that conforms the generated protocol
// defining your service API.
Expand All @@ -15,7 +15,7 @@ let api = MyServiceAPIImpl()
try api.registerHandlers(on: router)

// Create the application and run as you would normally.
let app = HBApplication(router: router)
let app = Application(router: router)
try await app.runService()
```

Expand Down
18 changes: 9 additions & 9 deletions Sources/OpenAPIHummingbird/OpenAPITransport.swift
Expand Up @@ -18,7 +18,7 @@ import Hummingbird
import NIOHTTP1
import OpenAPIRuntime

extension HBRouter: ServerTransport {
extension Router: ServerTransport {
/// Registers an HTTP operation handler at the provided path and method.
/// - Parameters:
/// - handler: A handler to be invoked when an HTTP request is received.
Expand All @@ -40,14 +40,14 @@ extension HBRouter: ServerTransport {
let (openAPIRequest, openAPIRequestBody) = try request.makeOpenAPIRequest(context: context)
let openAPIRequestMetadata = context.makeOpenAPIRequestMetadata()
let (openAPIResponse, openAPIResponseBody) = try await handler(openAPIRequest, openAPIRequestBody, openAPIRequestMetadata)
return HBResponse(openAPIResponse, body: openAPIResponseBody)
return Response(openAPIResponse, body: openAPIResponseBody)
}
}
}

extension HBRequest {
/// Construct ``OpenAPIRuntime.Request`` from Hummingbird ``HBRequest``
func makeOpenAPIRequest<Context: HBBaseRequestContext>(context: Context) throws -> (HTTPRequest, HTTPBody?) {
extension Request {
/// Construct ``OpenAPIRuntime.Request`` from Hummingbird ``Request``
func makeOpenAPIRequest<Context: BaseRequestContext>(context: Context) throws -> (HTTPRequest, HTTPBody?) {
let request = self.head
// extract length from content-length header
let length = if let contentLengthHeader = self.headers[.contentLength], let contentLength = Int(contentLengthHeader) {
Expand All @@ -64,8 +64,8 @@ extension HBRequest {
}
}

extension HBBaseRequestContext {
/// Construct ``OpenAPIRuntime.ServerRequestMetadata`` from Hummingbird ``HBRequest``
extension BaseRequestContext {
/// Construct ``OpenAPIRuntime.ServerRequestMetadata`` from Hummingbird ``Request``
func makeOpenAPIRequestMetadata() -> ServerRequestMetadata {
let keyAndValues = self.parameters.map { (key: String($0.0), value: $0.1) }
let openAPIParameters = [String: Substring](keyAndValues) { first, _ in first }
Expand All @@ -75,9 +75,9 @@ extension HBBaseRequestContext {
}
}

extension HBResponse {
extension Response {
init(_ response: HTTPResponse, body: HTTPBody?) {
let responseBody: HBResponseBody
let responseBody: ResponseBody
if let body = body {
let bufferSequence = body.map { ByteBuffer(bytes: $0)}
if case .known(let length) = body.length {
Expand Down
28 changes: 14 additions & 14 deletions Tests/OpenAPIHummingbirdTests/OpenAPITransportTests.swift
Expand Up @@ -15,7 +15,7 @@
import HTTPTypes
import Hummingbird
import HummingbirdCore
import HummingbirdXCT
import HummingbirdTesting
import NIOCore
import NIOHTTP1
import OpenAPIRuntime
Expand All @@ -31,9 +31,9 @@ extension HTTPField.Name {

final class HBOpenAPITransportTests: XCTestCase {
func test_requestConversion() async throws {
let router = HBRouter()
let router = Router()

router.post("/hello/:name") { hbRequest, context -> HBResponse in
router.post("/hello/:name") { hbRequest, context -> Response in
// Hijack the request handler to test the request-conversion functions.
let expectedRequest = HTTPRequest(
method: .post,
Expand All @@ -60,15 +60,15 @@ final class HBOpenAPITransportTests: XCTestCase {
XCTAssertEqual(collectedBody, [UInt8]("👋".utf8))
XCTAssertEqual(context.makeOpenAPIRequestMetadata(), expectedRequestMetadata)

// Use the response-conversion to create the HBRequest for returning.
// Use the response-conversion to create the Request for returning.
let response = HTTPResponse(status: .created, headerFields: [.xMumble: "mumble"])
return HBResponse(response, body: .init([UInt8]("👋".utf8)))
return Response(response, body: .init([UInt8]("👋".utf8)))
}

let app = HBApplication(responder: router.buildResponder())
let app = Application(responder: router.buildResponder())

try await app.test(.live) { client in
try await client.XCTExecute(
try await client.execute(
uri: "/hello/Maria?greeting=Howdy",
method: .post,
headers: [
Expand All @@ -86,11 +86,11 @@ final class HBOpenAPITransportTests: XCTestCase {
}

func test_largeBody() async throws {
let router = HBRouter()
let router = Router()
let bytes = (0..<1_000_000).map { _ in UInt8.random(in: 0...255)}
let byteBuffer = ByteBuffer(bytes: bytes)

router.post("/hello/:name") { hbRequest, context -> HBResponse in
router.post("/hello/:name") { hbRequest, context -> Response in
// Hijack the request handler to test the request-conversion functions.
let expectedRequest = HTTPRequest(
method: .post,
Expand All @@ -109,12 +109,12 @@ final class HBOpenAPITransportTests: XCTestCase {
XCTAssertEqual(request, expectedRequest)
XCTAssertEqual(context.makeOpenAPIRequestMetadata(), expectedRequestMetadata)

// Use the response-conversion to create the HBRequest for returning.
// Use the response-conversion to create the Request for returning.
let response = HTTPResponse(status: .ok)
return HBResponse(response, body: body)
return Response(response, body: body)
}

let app = HBApplication(
let app = Application(
router: router,
server: .http1(
additionalChannelHandlers: [
Expand All @@ -124,12 +124,12 @@ final class HBOpenAPITransportTests: XCTestCase {
)

try await app.test(.live) { client in
try await client.XCTExecute(
try await client.execute(
uri: "/hello/Maria?greeting=Howdy",
method: .post,
body: byteBuffer
) { hbResponse in
// Check the HBResponse (created from the Response) is what meets expectations.
// Check the Response (created from the Response) is what meets expectations.
XCTAssertEqual(hbResponse.status, .ok)
XCTAssertEqual(byteBuffer, hbResponse.body)
}
Expand Down

0 comments on commit 9686d8a

Please sign in to comment.