From ace909795d4c0d8dd412f60612df8195a20827e2 Mon Sep 17 00:00:00 2001 From: Alex Chapman Date: Tue, 14 Apr 2009 10:00:09 +1000 Subject: [PATCH] couchdb in progress --- basis/http/client/client.factor | 45 +++++++++++++++++---------------- extra/couchdb/couchdb.factor | 10 ++++---- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/basis/http/client/client.factor b/basis/http/client/client.factor index 1ba32cc61d..22d772d2b6 100644 --- a/basis/http/client/client.factor +++ b/basis/http/client/client.factor @@ -1,12 +1,12 @@ ! Copyright (C) 2005, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors assocs kernel math math.parser namespaces make +USING: accessors assocs debugger kernel math math.parser namespaces make sequences strings splitting calendar continuations accessors vectors math.order hashtables byte-arrays destructors io io.sockets io.streams.string io.files io.timeouts io.pathnames io.encodings io.encodings.string io.encodings.ascii io.encodings.utf8 io.encodings.8-bit io.encodings.binary io.crlf -io.streams.duplex fry ascii urls urls.encoding present +io.streams.duplex fry ascii urls urls.encoding present prettyprint http http.parsers http.client.post-data ; IN: http.client @@ -82,7 +82,7 @@ SYMBOL: redirects redirects get max-redirects < [ request get clone swap "location" header redirect-url - "GET" >>method swap with-http-request + "GET" >>method swap (with-http-request) ] [ too-many-redirects ] if ; inline recursive : read-chunk-size ( -- n ) @@ -105,9 +105,7 @@ SYMBOL: redirects request get url>> url-addr ascii drop 1 minutes over set-timeout ; -PRIVATE> - -: with-http-request ( request quot: ( chunk -- ) -- response ) +: (with-http-request) ( request quot: ( chunk -- ) -- response ) swap request [ [ @@ -129,23 +127,26 @@ PRIVATE> [ do-redirect ] [ nip ] if ] with-variable ; inline recursive +PRIVATE> + : ( url method -- request ) swap >>method swap >url ensure-port >>url ; inline -PRIVATE> - : success? ( code -- ? ) 200 299 between? ; -ERROR: download-failed response data ; +! ERROR: download-failed response data ; -M: download-failed error. - "HTTP request failed:" print nl - [ response>> . ] [ data>> . ] bi ; +! M: download-failed error. +! "HTTP request failed:" print nl +! [ response>> . ] [ data>> . ] bi ; +ERROR: download-failed response ; -: check-response* ( response data -- response data ) - over code>> success? [ download-failed ] unless ; +: check-response ( response -- response ) + dup code>> success? [ download-failed ] unless ; +! : check-response ( response data -- response data ) + ! over code>> success? [ download-failed ] unless ; : check-response-with-body ( response body -- response body ) [ >>body check-response ] keep ; @@ -166,17 +167,17 @@ M: download-failed error. : with-http-get ( url quot -- response ) [ ] dip with-http-request check-response ; inline -: ( url -- request ) - "DELETE" >>method ; +! : ( url -- request ) +! "DELETE" ; -: http-delete ( url -- response data ) - http-request ; +! : http-delete ( url -- response ) +! http-request ; -: ( url -- request ) - "TRACE" >>method ; +! : ( url -- request ) +! "TRACE" >>method ; -: http-trace ( url -- response data ) - http-request ; +! : http-trace ( url -- response ) +! http-request ; : download-name ( url -- name ) present file-name "?" split1 drop "/" ?tail drop ; diff --git a/extra/couchdb/couchdb.factor b/extra/couchdb/couchdb.factor index 3419244d72..c586287b2e 100644 --- a/extra/couchdb/couchdb.factor +++ b/extra/couchdb/couchdb.factor @@ -46,7 +46,7 @@ PREDICATE: file-exists-error < couchdb-error couch-request ; : couch-delete ( url -- assoc ) - couch-request ; + "DELETE" couch-request ; : response-ok ( assoc -- assoc ) "ok" over delete-at* and t assert= ; @@ -57,9 +57,9 @@ PREDICATE: file-exists-error < couchdb-error ! server TUPLE: server { host string } { port integer } { uuids vector } { uuids-to-cache integer } ; -: default-couch-host "localhost" ; -: default-couch-port 5984 ; -: default-uuids-to-cache 100 ; +: default-couch-host ( -- host ) "localhost" ; inline +: default-couch-port ( -- port ) 5984 ; inline +: default-uuids-to-cache ( -- n ) 100 ; inline : ( host port -- server ) V{ } clone default-uuids-to-cache server boa ; @@ -123,7 +123,7 @@ C: db db-url "_all_docs" append couch-get ; : ( assoc -- post-data ) - >json "application/json" ; + >json "application/json" swap >>data ; ! documents : id> ( assoc -- id ) "_id" swap at ;