[Cuis-dev] success with WebClient package
Andres Valloud
ten at smallinteger.com
Mon Jun 17 14:27:56 PDT 2024
Why don't all branches of GET return nil?
Do you need to return nil, or is just returning self enough? How about
returning e.g. ^self getDogsAsJson: aRequest? That reduces lines of
code quite a bit.
How many methods are there? If there are many more than 4, a dictionary
might be in order. Or another take:
handleDog: aRequest
aRequest doDogCrudWith: self
and let the request ask the questions about method, header etc to self,
rather than the questions being asked to aRequest here. Push the
responsibility closer to the actual objects involved.
On 6/17/24 2:20 PM, Mark Volkmann via Cuis-dev wrote:
> 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
> ].
>
> (acceptincludesSubString: '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.
>
More information about the Cuis-dev
mailing list