eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'HTTPSimplePluggableJSONRestService':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: HTTPSimplePluggableJSONRestService


Inheritance:

   Object
   |
   +--HTTPService
      |
      +--HTTPRestService
         |
         +--HTTPSimplePluggableJSONRestService

Package:
stx:goodies/webServer
Category:
Net-Communication-Rest
Version:
rev: 1.10 date: 2024/02/09 11:25:25
user: stefan
file: HTTPSimplePluggableJSONRestService.st directory: goodies/webServer
module: stx stc-classLibrary: webServer

Description:


A pluggable JSON based Rest services.
This answers with a block's return value, which can be configured dynamically.
This is especially useful for ad-hoc and mock Rest services (for example, in expecco).

Also provides a common 'protocolInfo' entry, which allows querying the supported entries.

[start Server with:]
    HTTPServer startServerOnPort:9876

[configure service entries and start with:]

    (HTTPSimplePluggableJSONRestService new)
        entryName:'operation1' getOrPut:'GET' parameterType:#String returnType:#String actionBlock:[:request :arg | 'Hello World'];
        entryName:'operation2' getOrPut:'GET' parameterType:#Dictionary returnType:#Dictionary actionBlock:[:request :arg | Dictionary withKeys:#('x' 'y' 'z') andValues:#(10 20 'Hello World') ];
        entryName:'operation3' getOrPut:'PUT' parameterType:#Integer returnType:#Integer actionBlock:[:request :arg | arg + 1 ];
        linkName:'/rest';
        registerServiceOn:(HTTPServer runningServerOnPort:9876)

[request:]
    (HTTPServer runningServerOnPort:9876) debugErrors:true.

    |data|
    data := HTTPInterface getFile:'localHost:9876/rest/protocolInfo'.
    data := JSONReader fromJSON:data.
    Transcript showCR:data

    |response data|
    response := HTTPInterface put:'localHost:9876/rest/operation3' with:'123' contentType:'text/json'.
    data := JSONReader fromJSON:response data.
    Transcript showCR:data

    |data|
    data := HTTPInterface getFile:'localHost:9876/rest/operation1'.
    data := JSONReader fromJSON:data.
    Transcript showCR:data


[instance variables:]

[class variables:]

copyright

COPYRIGHT (c) 2015 by eXept Software AG All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

Instance protocol:

call entries
o  protocolInfo

initialization
o  initialize

request handling
o  process: aRequest
handles requests of the form:
<baseUrl>/relPath
The registered block is called with the request and the json decoded argument (or nil).
The returned object is then encoded to json and returned.

setup
o  entryName: entryName getOrPut: getOrPut parameterType: parameterType returnType: returnType actionBlock: aBlock


Private classes:

    EntryInfo


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Sat, 21 Dec 2024 16:25:06 GMT