Arc Forumnew | comments | leaders | submitlogin
3 points by CatDancer 6077 days ago | link | parent

For a public API, JSON is a nice choice (http://json.org/), for which (ta da!) I just happen to have a reader/writer for: http://catdancer.github.com/json.html

For a nice example of a JSON web API, check out FriendFeed: http://code.google.com/p/friendfeed-api/wiki/ApiDocumentatio...

To handle an API message, you use a regular defop... what makes it a "web" API is that it uses the same HTTP protocol that a browser uses to fetch a web page or to post a form. In fact, you can often use a browser to try out a web API. For example, point your browser to http://friendfeed.com/api/feed/public, and you'll see a JSON message containing the most recent 30 public entries published to FriendFeed.

Here's an example of a very simple API written in Arc. For debugging, I have an op called "log" which will print a message in the window I'm running Arc in. So calling "http://myserver.com/log?message=hello" will print "hello" in my server window:

  (defop log req
    (eprn (arg req "message"))
    (pr "OK"))
My eprn function simply prints out to stderr, since anything printed to stdout is returned to the client.

  (def eprn args
    (w/stdout (stderr)
      (apply prn args)))
The (pr "OK") prints "OK" to stdout, which is returned to the client, so if you pointed your browser at http://myserver.com/log?message=hello you'd see "OK" displayed in your browser window.

To return JSON data like FriendFeed, you can either do it by hand:

  (defop inventory req
    (pr "{\"apples\":5,\"bananas\":18}"))
or use my JSON library:

  (defop inventory req
    (pr:tojson (obj apples 5 bananas 18)))
To have your client control your server, write a defop that takes actions depending on the arguments passed to it:

  (defop shutdown req
    (if (is (arg req "password") "xyzzy") (quit)))


2 points by thaddeus 6076 days ago | link

is there a set of dependancies for json.arc ?

I run:

(pr:tojson (obj apples 5 bananas 18))

and I get:

Error: "reference to undefined identifier: __mz"

Thanks, T.

-----

2 points by thaddeus 6076 days ago | link

ugh!

Ok I found the mz patch you created, but it's a little confusing installing it.

I tried the "patch method" but obviously the command

    "patch <mz.patch"
will not work in the arc top level. i am assuming you're running mzscheme directly to run this command (which i have never done).

I tried downloading your version of arc, but it doesn't work with or sync with "anarki". I get errors.

I find the whole library part of anarki extremely confusing. As much as I am sure people are contributing valuable code, it's like it's a dumping ground with little documentation (some exceptions). People are throwing code in odd directories with odd names and no examples for usage.

wouldn't be nice to have an install like:

  /arc/required/*.arc

  /arc/recommended/patch/before/*.arc; for bug fix code that has to be loaded instead of the relative core arc or mzscheme code, as opposed to after.

  /arc/recommended/patch/after/*.arc; for bug fix code that can be loaded after core arc loads, but before libraries.

  /arc/recommended/libraries/*.arc; 

  /arc/optional/; no optional files or patch files for optional libraries to be in any 'recommended' directories.  

  /arc/optional/patch/*.arc

  /arc/optional/patch/before/*.arc

  /arc/optional/patch/after/*.arc

  /arc/optional/libraries/*.arc

oh and while i'm frustrated.... an ide with a picklist for the optionals that figures out for us what dependancies are in place and manage them.

ugh.....

:)

-----

1 point by thaddeus 6076 days ago | link

Ok,after reading many posts I found the right one; and can see people had already figured out a means to do this....

http://arclanguage.org/item?id=3849

To date I have just been downloading the anarki directory, but tonight I'm going to try using the "git stable" method.

Question for you CatDancer, is your copy of Arc2 the equivalent of 'git stable' ?

I don't find I am using many of the optional features in anarki (so far I only use parsecomb and arcformat which are awesome) and if I do I was thinking I could drop them on top of your arc2 copy.

Thanks, T.

-----

1 point by CatDancer 6075 days ago | link

so far I only use parsecomb and ...

Were you able to get parsecomb to do anything? I was able to get it to make matches, but I couldn't figure out how to get it to tell me which match had been made. (For example, I could have it match a number or a string, but if it matched "123" I would get "123" returned to me but I couldn't figure out how to tell that it was a number that had been matched).

iirc, I ran parsecomb in pg's arc2 by removing the documentation strings from the beginning of the def's in the source.

-----

1 point by thaddeus 6075 days ago | link

Sorry. I meant 'parser'. I use it to feed code into arcformat. Let's me un-expand for presentation purposes.

T.

-----

1 point by CatDancer 6075 days ago | link

is your copy of Arc2 the equivalent of 'git stable'

My copy of Arc2 is pg's original arc2, which I got from http://arclanguage.org/install; I haven't looked at Anarki's stable branch.

-----

1 point by CatDancer 6075 days ago | link

thaddeus, I updated my documentation at http://catdancer.github.com/mz.html to give an explanation of the patch command and how to use it. Take a look and let me know if it's helpful. (Note github has come kind of caching bug with their pages so if you go back to the page your browser will display the old version of the page if it has a copy of it in its cache; you must click the refresh button or press F5 to see the new version).

I also crossed out the explanation of how to apply the patch using "git pull", since as you discovered "git pull" doesn't work if the two git repositories don't share a common ancestor. (I suspect there is a way to do this using git, but I'll have to go looking for it).

-----

1 point by thaddeus 6075 days ago | link

Looks good to me. Thank you for updating your doc.

:)

-----

1 point by CatDancer 6075 days ago | link

"patch" is actually a Unix command, it comes standard with Unix. So you'd type "patch <mz.patch" in your Unix shell, inside your arc directory.

If you're running Windows, if I remember correctly patch comes with cygwin... though I'm not sure.

If you look at mz.patch you can see that it is merely adding one line to ac.scm, so if all else fails, you can edit ac.scm and add the line yourself :-)

mz.patch is a patch against pg's original arc2... I have no idea if it will work with Anarki or not.

-----

1 point by thaddeus 6073 days ago | link

As a note for other n00bs who want to use the mz patch on anarki, it's probably best to just manually add the one line to the ac.scm file. Running the patch will fail with an error "patch already detected". I'm no unix expert but I looked at the anarki verion and there's already extra lines added where the patch expects to place it's code line(And the "add anyway" unix prompt failed for me).

At any rate I'm now playing with your json library CatDancer. Thanks for the help along the way. T.

-----

1 point by CatDancer 6073 days ago | link

I have no idea why I used ((mz integer?) v) instead of (isa v 'int) in json.arc, it sounds like it would have saved you some trouble if you hadn't needed to install the mz patch first.

-----

1 point by thaddeus 6075 days ago | link

Thank you. I will give this another go.

-----

1 point by thaddeus 6077 days ago | link

Ok, that looks like the kind of example I was looking for. Thanks!

-----