Arc Forumnew | comments | leaders | submitlogin
An example exchanging data via webservices in arc
4 points by thaddeus 5529 days ago | 2 comments
With the absence of documentation I thought it was worth sharing a rudementary example of exchanging data from one webserver to another using json, http-get and a few arc extensions. I believe those new to arc might appreciate examples.

  Requirements: 
  * pg's arc v3.1 
  * aw's json parser/combinator -> http://awwx.ws/combinator/17
  * stefano's? http-get/utils -> http://github.com/nex3/arc/tree/arc2.master/lib/http-get/
This example also uses an extra intra-symbol that extends arcs special syntax and can be found here-> http://www.blackstag.com/intra-symbol.arc. I am a big fan of arcs intra-symbols for table lookups. A while back I added a '$' symbol to convert a 'string' to a 'symbol' for lookup -> http://arclanguage.org/item?id=10324. In this example I also added the '%' symbol to handle keys of type 'string' as created by the json combinator function 'fromjson'. You can skip this by using "." instead of "%" as long as you assign the names as strings, as to be resolved, prior to using the intra-symbol.

On Webserver # 1: A nested table and a webservice that outputs the table in json format. Example found here: http://www.blackstag.com/great-authors.

  (= author (obj 1 (obj name (obj first "Edgar" last "Poe"))
   	             2 (obj name (obj first "Farley" last "Mowat")) 
	             3 (obj name (obj first "Margaret" last "Atwood"))))

  (defop great-authors req 
    (pr:tojson author))

On Webserver # 2: A service accepting arguments 'first' and 'last' for names. It pulls data from Webserver # 1 then checks to see if the arguments exist. http://www.yourwebserver.com/isgreatauthor?first=Margaret&last=Atwood (can use blackstag.com to see result).

  (defop isgreatauthor req
    (with (fname (arg req "first")
           lname (arg req "last") 
           resultant "not found")
      (each (k v) (fromjson (cadr (get-request (str->url "http://www.blackstag.com/great-authors"))))
        (if (and (is fname v%name%first)(is lname v%name%last))
            (= resultant "found")))
    (prn (string fname " " lname " was " resultant " in the great-author-db!"))))
thaddeus


1 point by thaddeus 5528 days ago | link

As a note the http-utils str->url has a reference to a function '1+' that probably exists in some other library? if you're using anarki. If not you may need to:

  (def 1+ (n)
    (+ n 1))

-----

1 point by palsecam 5528 days ago | link

See 'inc in arc.arc (there is also '++).

-----