How to Call an API

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf How to Call an API

Viewing 4 reply threads
  • Author
    Replies
    • #85013
      Levy Lazarre
      Participant

        Layale,

        It all depends on the API. It seems like you are making a call to a REST API, so you have many options.

        If you are on Cloverleaf 6.2 and are licensed for the Web Services Adapter (CAA-WS), you can use the GUI since JSON support has been added to 6.2.

        Otherwise, you can use a UPOC thread and write some code. You could use CURL to make the API call. Another alternative would be to use the ‘rest’ package to make the API call then the ‘json’ package to convert the json reply to a Tcl dictionary. Then use dict commands to parse the dictionary.

        Code:


        package require rest
        package require json
         ….
        ::json::json2dict   the_json_reply

        If you know Java, you have even more libraries to get and parse the JSON.

        I hope this helps.

      • #85014
        Layale Zakhour
        Participant

          Thank you for your reply Levy. I created a python script that calls the URL and parses the result. How can I pass input from a database table to the python script in Cloverleaf? and how can I write the output to the database?

        • #85015
          David Barr
          Participant

            The normal way to call a Python script from Cloverleaf would be from a Tcl TPS proc. You can use an inbound database protocol thread to get your input data in a message, then your TPS proc can call “msgget” to get the data into a variable. You can pass that to your Python script either through command line arguments using the exec command (http://wiki.tcl.tk/exec) or through an IPC pipe (http://wiki.tcl.tk/14024). You can put the data back in your message with a “msgset” command, then route the message to an outbound database protocol thread to write the result back to the database. Another alternative is to have Cloverleaf call the Python script, and the script can query the database directly to get the data.

            Is there a particular reason you want to do this from Cloverleaf instead of in a script outside of Cloverleaf?

          • #85016
            Layale Zakhour
            Participant

              Thank you for the info. Well what’s the best way to call a REST web service and a SOAP web service in Cloverleaf? Im using version 6.2. how do we do that using the CAA-WS component?

            • #85017
              Levy Lazarre
              Participant

                If you look in this forum, under the Sticky: BOX REPO, there is a BOX named ‘ws_samples’ that you can download and import into your system. These are good examples on how to use CAA-WS.

                If you don’t want to leverage CAA-WS, you can always use a UPOC thread and write a TPS using one of several packages that are available to you: TclCurl, rest, http…

                By the way, with Cloverleaf 6.2, support has been added for Python. You can write your TPS directly in Python. I haven’t had a chance to explore this new feature yet. A BOX has been provided with the install (‘JavascriptAndPython’) that you can use for learning.

            Viewing 4 reply threads
            • The forum ‘Cloverleaf’ is closed to new topics and replies.