Sunday, May 19, 2024
HomeIOS DevelopmentHummingbird routing and requests - The.Swift.Dev.

Hummingbird routing and requests – The.Swift.Dev.


Routing on the server facet means the server goes to ship a response primarily based on the URL path that the shopper known as when firing up the HTTP request. In fact the server can verify further parameters and headers to construct the ultimate response, however after we speak about routing usually, we often seek advice from the trail parts. Hummingbird makes use of a trie-based router, which is a quick and environment friendly method of trying up routes. It is fairly easy to reply to HTTP request utilizing the built-in router, you possibly can merely add your primary route handlers like this:

 
router.on("foo", technique: .HEAD) { _ -> HTTPResponseStatus in .okay }
router.on("foo", technique: .GET) { _ -> HTTPResponseStatus in .okay }
router.on("foo", technique: .POST) { _ -> HTTPResponseStatus in .okay }
router.on("foo", technique: .PUT) { _ -> HTTPResponseStatus in .okay }
router.on("foo", technique: .PATCH) { _ -> HTTPResponseStatus in .okay }
router.on("foo", technique: .DELETE) { _ -> HTTPResponseStatus in .okay }


router.head("foo") { _ -> HTTPResponseStatus in .okay }
router.get("foo") { _ -> HTTPResponseStatus in .okay }
router.put("foo") { _ -> HTTPResponseStatus in .okay }
router.publish("foo") { _ -> HTTPResponseStatus in .okay }
router.patch("foo") { _ -> HTTPResponseStatus in .okay }
router.delete("foo") { _ -> HTTPResponseStatus in .okay }

In Hummingbird additionally it is doable to register use a operate as an alternative of a block. Handler features could be async and throwing too, so you possibly can mark the blocks with these key phrases or use asynchronous Swift features when registering route handlers. In case you do not present the primary parameter, the trail as a string, the route handler goes to be connected to the bottom group. 👍

You may also prefix a path part with a colon, it will flip that part right into a dynamic route parameter. The parameter goes to be named after the trail part, by merely dropping the colon prefix. You may entry parameters inside your route handler by way of the req.parameters property. It is usually doable to register a number of parts utilizing a / character.

public extension HBApplication {
    
    func configure() throws {

        router.get { _ async throws in "Whats up, world!" }

        router.get("hiya/:identify") { req throws in
            guard let identify = req.parameters.get("identify") else {
                throw HBHTTPError(
                    .badRequest,
                    message: "Invalid identify parameter."
                )
            }
            return "Whats up, (identify)!"
        }

        let group = router.group("todos")
        group.get(use: record)
        group.publish(use: create)
        
        let idGroup = group.group(":todoId")
        idGroup.head(use: verify)
        idGroup.get(use: fetch)
        idGroup.put(use: replace)
        idGroup.patch(use: patch)
        idGroup.delete(use: delete)

        
        router.group("todos")
            .get(use: record)
            .publish(use: create)
            .group(":todoId")
                .head(use: verify)
                .get(use: fetch)
                .put(use: replace)
                .patch(use: patch)
                .delete(use: delete)

    }

    func record(_ req: HBRequest) async throws -> HTTPResponseStatus { .okay }
    func verify(_ req: HBRequest) async throws -> HTTPResponseStatus { .okay }
    func fetch(_ req: HBRequest) async throws -> HTTPResponseStatus { .okay }
    func create(_ req: HBRequest) async throws -> HTTPResponseStatus { .okay }
    func replace(_ req: HBRequest) async throws -> HTTPResponseStatus { .okay }
    func patch(_ req: HBRequest) async throws -> HTTPResponseStatus { .okay }
    func delete(_ req: HBRequest) async throws -> HTTPResponseStatus { .okay }
}

It’s doable to make use of a wildcard character () when detecting path parts and the recursive model (*) to catch every thing. Additionally you should utilize the ${identify} syntax to catch a named request parameter even with a prefix or suffix, however you possibly can’t insert this in the course of a path part. (e.g. “prefix-${identify}.jpg” will not work, however “${identify}.jpg” is simply nice) 💡

import Hummingbird
import HummingbirdFoundation

extension HBApplication {

    func configure(_ args: AppArguments) throws {

        router.get("foo-${identify}", use: catchPrefix)
        router.get("${identify}.jpg", use: catchSuffix)
        
        router.get("*", use: catchOne)
        router.get("*/*", use: catchTwo)

        router.get("**", use: catchAll)
        
    }
    
    
    func catchOne(_ req: HBRequest) async throws -> String {
        "one"
    }

    
    func catchTwo(_ req: HBRequest) async throws -> String {
        "two"
    }
    
    
    func catchAll(_ req: HBRequest) async throws -> String {
        "all: " + req.parameters.getCatchAll().joined(separator: ", ")
    }
    
    
    func catchPrefix(_ req: HBRequest) async throws -> String {
        "prefix: " + (req.parameters.get("identify") ?? "n/a")
    }
    
    
    func catchSuffix(_ req: HBRequest) async throws -> String {
        "suffix: " + (req.parameters.get("identify") ?? "n/a")
    }
}

It is usually doable to edit the auto-generated response for those who specify the .editResponse possibility.

router.get("foo", choices: .editResponse) { req -> String in
    req.response.standing = .okay
    req.response.headers.replaceOrAdd(
        identify: "Content material-Sort", 
        worth: "software/json"
    )
    return #"{"foo": "bar"}"#
}

Hummingbird assist for physique streaming is superb, you possibly can stream a HTTP request physique by utilizing the .streamBody possibility. The physique stream has a sequence property, which you should utilize to iterate by way of the incoming ByteBuffer chunks when dealing with the request. 🔄

func configure() throws { 
    router.publish("foo", choices: .streamBody) { req async throws -> String in
        guard
            let rawLength = req.headers["Content-Length"].first,
            let size = Int(rawLength),
            let stream = req.physique.stream
        else {
            throw HBHTTPError(
                .badRequest,
                message: "Lacking or invalid physique stream."
            )
        }
        var depend: Int = 0
        for strive await chunk in stream.sequence {
            depend += chunk.readableBytes
        }
        return String("(size) / (depend)")
    }
}


let app = HBApplication(
    configuration: .init(
        handle: .hostname(hostname, port: port),
        serverName: "Hummingbird",
        maxUploadSize: 1 * 1024 * 1024 * 1024 
    )
)

As you possibly can see you possibly can simply entry all of the incoming headers through the req.headers container, you need to word that this technique will return header values in a case-insensitive method. If you wish to stream bigger recordsdata, you additionally must set a customized maxUploadSize utilizing the configuration object when initializing the HBApplication occasion.

curl -X POST http://localhost:8080/foo 
    -H "Content material-Size: 3" 
    --data-raw 'foo'

curl -X POST http://localhost:8080/foo 
    -H "content-Size: 5242880" 
    -T ~/check

You may check out streaming with a easy cURL script, be happy to experiment with these.

One other factor I would like to point out you is the way to entry question parameters and different properties utilizing the request object. Right here is an all-in-one instance, which you should utilize as a cheatsheet… 😉


router.get("bar") { req async throws -> String in
            
    struct Foo: Codable {
        var a: String
    }

    print(req.technique)
    print(req.headers)
    print(req.headers["accept"])
    print(req.uri.queryParameters.get("q") ?? "n/a")
    print(req.uri.queryParameters.get("key", as: Int.self) ?? 0)

    if let buffer = req.physique.buffer {
        let foo = strive? JSONDecoder().decode(Foo.self, from: buffer)
        print(foo ?? "n/a")
    }
    return "Whats up, world!"
}

Anyway, there’s one further tremendous cool characteristic in Hummingbird that I would like to point out you. It’s doable to outline a route handler, this manner you possibly can encapsulate every thing right into a single object. There’s an async model of the route handler protocol, for those who do not want async, you possibly can merely drop the key phrase each from the protocol identify & the strategy. I really like this strategy loads. 😍

struct MyRouteHandler: HBAsyncRouteHandler {

    struct Enter: Decodable {
        let foo: String
    }

    struct Output: HBResponseEncodable {
        let id: String
        let foo: String
    }
    
    let enter: Enter

    init(from request: HBRequest) throws {
        self.enter = strive request.decode(as: Enter.self)
    }

    func deal with(request: HBRequest) async throws -> Output {
        .init(
            id: "id-1",
            foo: enter.foo
        )
    }
}

The request.decode technique makes use of the built-in decoder, which it’s a must to explicitly set for the applying, since we will talk utilizing JSON knowledge, we will use the JSON encoder / decoder from Basis to robotically remodel the info.

With a purpose to make use of the customized route handler, you possibly can merely register the article sort.

import Hummingbird
import HummingbirdFoundation

public extension HBApplication {

    func configure() throws {
        
        encoder = JSONEncoder()
        decoder = JSONDecoder()
                
        
        router.publish("foo", use: MyRouteHandler.self)
    }
}

You may learn extra about how the encoding and decoding works in Hummingbird, however perhaps that subject deserves its personal weblog publish. When you’ve got questions or solutions, be happy to contact me. 🙈

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments