Protocols

The following protocols are available globally.

  • protocol for encoders generating a Response

    See more

    Declaration

    Swift

    public protocol HBResponseEncoder
  • protocol for decoder deserializing from a Request body

    See more

    Declaration

    Swift

    public protocol HBRequestDecoder
  • HBRouteHandler which uses Codable to initialize it

    An example

    struct CreateUser: HBRequestDecodable {
        let username: String
        let password: String
        func handle(request: HBRequest) -> EventLoopFuture<HTTPResponseStatus> {
            return addUserToDatabase(
                name: self.username,
                password: self.password
            ).map { _ in .ok }
    }
    application.router.put("user", use: CreateUser.self)
    
    See more

    Declaration

    Swift

    public protocol HBRequestDecodable : HBRouteHandler, Decodable
  • Protocol for encodable object that can generate a response. The router will encode the response using the encoder stored in HBApplication.encoder.

    See more

    Declaration

    Swift

    public protocol HBResponseEncodable : HBResponseGenerator, Encodable
  • Protocol for codable object that can generate a response

    Declaration

    Swift

    public protocol HBResponseCodable : HBResponseEncodable, Decodable
  • Protocol for extensible classes

    See more

    Declaration

    Swift

    public protocol HBExtensible
  • Applied to HBRequest before it is dealt with by the router. Middleware passes the processed request onto the next responder (either the next middleware or the router) by calling next.apply(to: request). If you want to shortcut the request you can return a response immediately

    Middleware is added to the application by calling app.middleware.add(MyMiddleware().

    Middleware allows you to process a request before it reaches your request handler and then process the response returned by that handler.

    func apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse> {
        let request = processRequest(request)
        return next.respond(to: request).map { response in
            return processResponse(response)
        }
    }
    

    Middleware also allows you to shortcut the whole process and not pass on the request to the handler

    func apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse> {
        if request.method == .OPTIONS {
            return request.success(HBResponse(status: .noContent))
        } else {
            return next.respond(to: request)
        }
    }
    
    See more

    Declaration

    Swift

    public protocol HBMiddleware
  • Object that can generate a Response.

    This is used by Router to convert handler return values into a HBResponse.

    See more

    Declaration

    Swift

    public protocol HBResponseGenerator
  • Object for handling requests.

    Instead of passing a closure to the router you can provide an object it should try and create before handling the request. This allows you to separate the extraction of data from the request and the processing of the request. For example

    struct UpdateReminder: HBRouteHandler {
        struct Request: Codable {
            let description: String
            let date: Date
        }
        let update: Request
        let id: String
    
        init(from request: HBRequest) throws {
            self.update = try request.decode(as: Request.self)
            self.id = try request.parameters.require("id")
        }
        func handle(request: HBRequest) -> EventLoopFuture<HTTPResponseStatus> {
            let reminder = Reminder(id: id, update: update)
            return reminder.update(on: request.db)
                .map { _ in .ok }
        }
    }
    
    See more

    Declaration

    Swift

    public protocol HBRouteHandler
  • Undocumented

    See more

    Declaration

    Swift

    public protocol HBRouterMethods
  • Directs Requests to handlers based on the request uri.

    Conforms to HBResponder so need to provide its own implementation of func apply(to request: Request) -> EventLoopFuture<Response>.

    HBRouter requires an implementation of the on(path:method:use) functions but because it also conforms to HBRouterMethods it is also possible to call the method specific functions get, put, head, post and patch. The route handler closures all return objects conforming to HBResponseGenerator. This allows us to support routes which return a multitude of types eg

    app.router.get("string") { _ -> String in
        return "string"
    }
    app.router.post("status") { _ -> HTTPResponseStatus in
        return .ok
    }
    app.router.data("data") { request -> ByteBuffer in
        return request.allocator.buffer(string: "buffer")
    }
    

    Routes can also return EventLoopFuture‘s. So you can support returning values from asynchronous processes.

    The default Router setup in HBApplication is the TrieRouter . This uses a trie to partition all the routes for faster access. It also supports wildcards and parameter extraction

    app.router.get("user/*", use: anyUser)
    app.router.get("user/:id", use: userWithId)
    

    Both of these match routes which start with “/user” and the next path segment being anything. The second version extracts the path segment out and adds it to HBRequest.parameters with the key “id”.

    See more

    Declaration

    Swift

    public protocol HBRouter : HBResponder, HBRouterMethods
  • Context that created HBRequest.

    See more

    Declaration

    Swift

    public protocol HBRequestContext
  • Protocol for object that produces a response given a request

    This is the core protocol for Hummingbird. It defines an object that can respond to a request.

    See more

    Declaration

    Swift

    public protocol HBResponder
  • Protocol for driver supporting persistent Key/Value pairs across requests

    See more

    Declaration

    Swift

    public protocol HBPersistDriver