JulienDelplanque / SimpleRESTHandler
About SimpleRESTHandler
SimpleRESTHandler
This project contain the class SimpleRESTHandler that give methods needed to build a proper REST handler. This class implement the method a "clever" handleRequest method that distribute requests to correct methods according to pragmas set inside them.
Load it
Gofer new url: 'http://smalltalkhub.com/mc/JulienDelplanque/SimpleRESTHandler/main'; package: 'ConfigurationOfSimpleRESTHandler'; load.
ConfigurationOfSimpleRESTHandler loadDevelopment.
How to use it?
You have to override the class SimpleRESTHandler and implement at least the 2 following messages:
- SimpleRESTHandler>>#handleRootRequest:
- SimpleRESTHandler>>#versionFromVersionString:
See these methods docstrings for details.
How to add resources?
If you want to add messages that will be taken in account by the handler while resolving request, you must set the two following pragmas:
- restAPIversion: X where X is an integer describing the version of the api.
- restAPIname: #name where name is the name of the resource path from root as a String.
See ExampleRESTHandler and ExampleRESTCalculator for examples of use.
About the API version
Important note: if you override this object to create a handler, requests to your API MUST have a path that start with the a string that represent the version of the API to use.
For example: /v1/myresource
The format of this string will be defined in SimpleRESTHandler>>#versionFromVersionString:
An example working out of the box:
server := ZnSingleThreadedServer new port: 8080; delegate: ExampleRESTCalculator new.
server start.
That's all! Now you can do request to http:localhost:8080.
For example:
ZnClient new get: 'http://127.0.0.1:8080/v1/addition?a=41&b=1'.
ZnClient new get: 'http://127.0.0.1:8080/v1/sqrt?a=4'.
ZnClient new get: 'http://127.0.0.1:8080/v1/sum?a=5&b=6&c=6&d=5'.
