[Cuis-dev] success with WebClient package

Mark Volkmann r.mark.volkmann at gmail.com
Mon Jun 17 14:20:03 PDT 2024


I finally have a set of HTTP CRUD operations working using the WebClient
package!
The following is a method that handles requests for all URLs with the path
"/dog".
Regardless of the HTTP method, all requests pass through this.
This code works perfectly, but I'm questioning whether it is idiomatic
Smalltalk code.
What would you do differently in this code?

handleDog: aRequest
    "handle an HTTP request based on its method"
    | id parts |

    aRequest method = 'GET' ifTrue: [
        | accept |

        accept := aRequest headerAt: 'Accept'.
        (accept includesSubString: 'application/json') ifTrue: [
            self getDogsAsJson: aRequest.
            ^nil
        ].

        (accept includesSubString: 'text/html') ifTrue: [
            self getDogsAsHtml: aRequest.
            ^nil
        ]
    ].

    aRequest method = 'POST' ifTrue: [
        self createDog: aRequest.
        ^nil
    ].

    "Get the path parameter value."
    "TODO: Is this really the best way to do it?"
    parts := aRequest url prefixAndSuffix: $/.
    id := parts last asNumber.

    aRequest method = 'DELETE' ifTrue: [
        self deleteDog: aRequest id: id.
        ^nil
    ].

    aRequest method = 'PUT' ifTrue: [
        self updateDog: aRequest id: id.
        ^nil
    ]

-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240617/9a1026e1/attachment.htm>


More information about the Cuis-dev mailing list