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.7 date: 2019/05/29 21:57:03
user: cg
file: HTTPSimplePluggableJSONRestService.st directory: goodies/webServer
module: stx stc-classLibrary: webServer
Author:
cg

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:]


Related information:



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.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Fri, 16 Apr 2021 11:22:45 GMT