Finally all basic operations work! It is messy, but it is progress!

test-github-pull-request
Thomas Hintz 14 years ago
parent dec49a68cd
commit 6ecbeddc0f

@ -8,10 +8,8 @@
*last-sig* *last-sig*
list-objects list-buckets bucket-exists? create-bucket! delete-bucket! get-object)
;; procs ;; procs
;put-object delete-object) list-objects list-buckets bucket-exists? create-bucket! delete-bucket! get-object put-object! delete-object!)
(import scheme chicken srfi-1 extras srfi-13 data-structures ports posix) (import scheme chicken srfi-1 extras srfi-13 data-structures ports posix)
(use base64 sha1 http-client uri-common intarweb srfi-19 hmac ssax sxpath) (use base64 sha1 http-client uri-common intarweb srfi-19 hmac ssax sxpath)
@ -69,7 +67,7 @@
(define *last-sig* #f) (define *last-sig* #f)
(define amazon-ns (make-parameter '(x . "http://s3.amazonaws.com/doc/2006-03-01/"))) (define amazon-ns (make-parameter '(x . "http://s3.amazonaws.com/doc/2006-03-01/")))
(define (aws-headers bucket path verb) (define (aws-headers bucket path verb content-type content-length)
(let ((n (current-date 0))) (let ((n (current-date 0)))
(headers `((date #(,(intarweb-date n) ())) (headers `((date #(,(intarweb-date n) ()))
(authorization #(aws ((access-key . ,(access-key)) (authorization #(aws ((access-key . ,(access-key))
@ -80,14 +78,16 @@
(if bucket (string-append bucket "/") "") (if bucket (string-append bucket "/") "")
(if path path "")) (if path path ""))
date: (sig-date n) date: (sig-date n)
content-type: "application/x-www-form-urlencoded"))))))))) content-type: content-type)))))
(content-type ,(string->symbol content-type))
(content-length ,content-length)))))
(define (aws-request bucket path verb #!key no-auth) (define (aws-request bucket path verb #!key no-auth (content-type "") (content-length 0))
(make-request (make-request
method: (string->symbol verb) method: (string->symbol verb)
uri: (uri-reference (string-append "http" (if (https) "s" "") "://" (if bucket (string-append bucket ".") "") uri: (uri-reference (string-append "http" (if (https) "s" "") "://" (if bucket (string-append bucket ".") "")
"s3.amazonaws.com" (if path (string-append "/" path) ""))) "s3.amazonaws.com" (if path (string-append "/" path) "")))
headers: (if no-auth (headers '()) (aws-headers bucket path verb)))) headers: (if no-auth (headers '()) (aws-headers bucket path verb content-type content-length))))
(define (aws-xml-parser path ns) (define (aws-xml-parser path ns)
(lambda () (lambda ()
@ -95,19 +95,20 @@
(ssax:xml->sxml (current-input-port) ns)))) (ssax:xml->sxml (current-input-port) ns))))
(define (perform-aws-request bucket path #!key (define (perform-aws-request bucket path #!key
(content #f)
(sxpath '()) (sxpath '())
(verb (if content "PUT" "GET")) (body '())
(verb "GET")
(ns '((x . "http://s3.amazonaws.com/doc/2006-03-01/"))) (ns '((x . "http://s3.amazonaws.com/doc/2006-03-01/")))
(no-xml #f) (no-xml #f)
(params '()) (no-auth #f)
(no-auth #f)) (content-type "application/x-www-form-urlencoded")
(content-length 0))
;(handle-exceptions ;(handle-exceptions
; exn ; exn
; ((condition-property-accessor 'client-error 'body) exn) ; ((condition-property-accessor 'client-error 'body) exn)
(with-input-from-request (with-input-from-request
(aws-request bucket path verb no-auth: no-auth) (aws-request bucket path verb no-auth: no-auth content-type: content-type content-length: content-length)
params body
(if no-xml (if no-xml
read-string read-string
(aws-xml-parser sxpath ns)))) (aws-xml-parser sxpath ns))))
@ -124,7 +125,7 @@
(handle-exceptions (handle-exceptions
exn exn
(assert-404 exn) (assert-404 exn)
(perform-aws-request #f bucket verb: "HEAD" no-xml: #t params: '((max-keys . "0"))) (perform-aws-request #f bucket verb: "HEAD" no-xml: #t)
#t)) #t))
(define (create-bucket! bucket) (define (create-bucket! bucket)
@ -133,12 +134,14 @@
(define (delete-bucket! bucket) (define (delete-bucket! bucket)
(perform-aws-request bucket #f verb: "DELETE" no-xml: #t)) (perform-aws-request bucket #f verb: "DELETE" no-xml: #t))
(define (set-object! bucket key object) #f) (define (put-object! bucket key object)
;(perform-aws-request bucket key '() (perform-aws-request bucket key verb: "PUT" content-type: "text/plain" body: (lambda () (print object))
content-length: (string-length object) no-xml: #t))
(define (get-object bucket key) (define (get-object bucket key)
(perform-aws-request bucket key no-xml: #t)) (perform-aws-request bucket key no-xml: #t))
(define (delete-object bucket key) #f) (define (delete-object! bucket key)
(perform-aws-request bucket key no-xml: #t verb: "DELETE"))
) )

@ -14,7 +14,10 @@
(test-assert "Create Bucket" (create-bucket! *b*)) (test-assert "Create Bucket" (create-bucket! *b*))
(test "Bucket Exists 2" #t (bucket-exists? *b*)) (test "Bucket Exists 2" #t (bucket-exists? *b*))
(test-assert "List Buckets" (list-buckets)) ; should test this more specifically... (test-assert "List Buckets" (list-buckets)) ; should test this more specifically...
(test "List Bucket Objects" '() (list-objects *b*)) (test "List Bucket Objects 1" '() (list-objects *b*))
(test-assert "Put Object" (put-object! *b* "key" "value"))
(test "List Bucket Objects 2" '("key") (list-objects *b*))
(test-assert "Delete Object" (delete-object! *b* "key"))
(test-assert "Delete Bucket" (delete-bucket! *b*))) (test-assert "Delete Bucket" (delete-bucket! *b*)))
(test-exit) (test-exit)
Loading…
Cancel
Save