« Previous  |  Next »

Seaside-REST APIs for RedditSt20

13 January 2018

I've implemented RESTful APIs for RedditSt20 using Seaside-REST. The API endpoints are accessible using Curl:

% curl -X GET -H "Accept: text/json" http://127.0.0.1:8080/redditst20-api/latestLinks
<JSON output in one long line>

The same from Zinc, using NeoJSON to pretty-print:

NeoJSONWriter toStringPretty: 
  (NeoJSONObject fromString: 
    (ZnEasy get: 'http://127.0.0.1:8080/redditst20-api/latestLinks') contents)

Output:

[
    {
        "points" : 0,
        "url" : "http://postgresql.org",
        "created" : "2018-01-13T20:03:56.405254+08:00",
        "title" : "PostgreSQL",
        "id" : 9
    },
    {
        "points" : 0,
        "url" : "http://sqlite.org",
        "created" : "2018-01-13T20:03:56.404526+08:00",
        "title" : "SQLite",
        "id" : 10
    },
    {
        "points" : 0,
        "url" : "http://pharo.org",
        "created" : "2018-01-13T20:03:56.403656+08:00",
        "title" : "Pharo",
        "id" : 11
    }
]

Voting is implemented using POST:

% curl -X POST \
    -H "Accept: text/json" \
    -H "Content-Type: text/json" \
    http://127.0.0.1:8080/redditst20-api/11/voteUp  
{"Updated":"true"}
%                                                                           

Let's check out the highest ranking links:

NeoJSONWriter toStringPretty: 
  (NeoJSONObject fromString: 
    (ZnEasy get: 'http://127.0.0.1:8080/redditst20-api/highestRankingLinks') contents)

Here's the output:

[
    {
        "points" : 1,
        "url" : "http://pharo.org",
        "created" : "2018-01-13T20:03:56.403656+08:00",
        "title" : "Pharo",
        "id" : 11
    },
    {
        "points" : 0,
        "url" : "http://postgresql.org",
        "created" : "2018-01-13T20:03:56.405254+08:00",
        "title" : "PostgreSQL",
        "id" : 9
    },
    {
        "points" : 0,
        "url" : "http://sqlite.org",
        "created" : "2018-01-13T20:03:56.404526+08:00",
        "title" : "SQLite",
        "id" : 10
    }
]

To create a new link, use PUT:

% curl -X PUT \
    -H "Accept: text/json" \
    -H "Content-Type: text/json" \
    -d '{"title":"Glorp", "url":"http://glorp.org"}' \
    http://127.0.0.1:8080/redditst20-api/putLink   
{"Created":true}
%                      

And here it is:

[
    {
        "points" : 0,
        "url" : "http://glorp.org",
        "created" : "2018-01-13T20:15:15.569197+08:00",
        "title" : "Glorp",
        "id" : 12
    },
        ... 
]

Code is on STH. I plan to move it to GH anytime now.

Blog comments powered by Disqus