Silpa provides a set of webservice APIs over json-rpc protocol. Silpa services can be used from any programming language which has an RPC implementation. The request and response formats of APIs are in json. It is recommended to read the json-rpc documentation if you are not familiar with that.
This page explains the available usage and sample usage from python application. Implementing this in other programming languages should not be difficult and should be a matter of changing the programming language syntax For further assistance, see the related links section of this page
For using the APIs you need the jsonrpc library of python. You can get this from here.
JSON-RPC wraps an object, allowing you to call methods on that object and get the return values. It also provides a way to get error responses.
The specification goes into the details (though in a vague sort of way). Here’s the basics:
All access goes through a POST to a single URL.
The POST contains a JSON body that looks like:
{"method": "methodName", "id": "arbitrary-something", "params": [arg1, arg2, ...]}
The id parameter is just a convenience for the client to keep track of which response goes with which request. This makes asynchronous calls (like an XMLHttpRequest) easier. We just send the exact same id back as we get, we never look at it.
The response is JSON. A successful response looks like:
{"result": the_result,
"error": null,
"id": "arbitrary-something"}
The error response looks like:
{"result": null, "error": {"name": "JSONRPCError", "code": (number 100-999), "message": "Some Error Occurred", "error": "whatever you want\n(a traceback?)"}, "id": "arbitrary-something"}
It doesn’t seem to indicate if an error response should have a 200 response or a 500 response. So as not to be completely stupid about HTTP, we choose a 500 resonse, as giving an error with a 200 response is irresponsible.
Silpa provides a service discovery APIs to get the list of available service APIs. The following code illustrates the usage:
# -*- coding: utf-8 -*- from jsonrpc import ServiceProxy silpaService = ServiceProxy("http://smc.org.in/silpa/JSONRPC") print silpaService.system.listMethods()
When the above code is executed you should get a response like this:
['modules.CharDetails.getdetails', 'modules.Dictionary.getdef', 'modules.Fortune.fortune_ml', 'modules.LangGuess.getScriptName', 'modules.LangGuess.guessLanguage', 'modules.Payyans.ASCII2Unicode', 'modules.Payyans.ASCII2Unicode', 'modules.Payyans.Unicode2ASCII', 'modules.Payyans.Unicode2ASCII', 'modules.Soundex.compare', 'modules.Soundex.soundex','modules.Syllabalizer.syllabalize', 'modules.Transliterator.transliterate']
As you can see the response is a json formatted list of available methods available in the server
print silpaService.modules.Dictionary.getdef("help","freedict-eng-hin")Response will be the string containing the definition of the given word
>>>print silpaService.modules.Spellchecker.check("speling") False >>>print silpaService.modules.Spellchecker.check("speling","en_US") False >>>print silpaService.modules.Spellchecker.suggest("speling") ["spelling","spieling","spewing"]The language parameter is optional. If not provided the language of the input word will be detected.
>>>print silpaService.modules.Soundex.soundex("കാര്ത്തിക്") കAPKKBF0 >>>print silpaService.modules.Soundex.compare("കാര്ത്തിക്","கார்திக்") 2
>>>print silpaService.modules.Fortune.fortune('chanakya') Do not inhabit a country where you are not respected,\n cannot earn your livelihood, have no friends, or cannot\n acquire knowledge.\n" >>>print silpaService.modules.Fortune.fortune('chanakya','charity') " The good habits of charity, learning and austerity\n practised during many past lives continue to be cultivated\n in this birth by virtue of the link (yoga) of this present\n life to the previous ones.\n"