-(define (uri-fetch uri)
- (log-info "GET ~a" uri)
- (define resp (http-get uri))
- (define status (http-response-code resp))
- (define body (http-response-body resp))
- (log-debug "finished GET ~a status:~a body length:~a"
- uri status (string-length body))
- ; TODO Handle redirects
- (if (= status 200)
- (let*
- ([url-digest
- (hash-sha1 uri)]
- [cache-file-path
- (expand-user-path (string-append "~/.tt/cache/" url-digest))])
- (display-to-file
- body cache-file-path
- #:exists 'replace)
- body)
- ; TODO A more-informative exception
- (raise status)))
+(define (uri-fetch use-cache uri)
+ (define cache-file-path
+ (expand-user-path (string-append "~/.tt/cache/" (hash-sha1 uri))))
+ (if (and use-cache (file-exists? cache-file-path))
+ (begin
+ (log-info "uri-fetch cached ~a" uri)
+ (file->string cache-file-path))
+ (begin
+ (log-info "uri-fetch new ~a" uri)
+ (let* ([resp (http-get uri)]
+ [status (http-response-code resp)]
+ [body (http-response-body resp)])
+ (log-debug "finished GET ~a status:~a body length:~a"
+ uri status (string-length body))
+ ; TODO Handle redirects
+ (if (= status 200)
+ (begin
+ (display-to-file body cache-file-path #:exists 'replace)
+ body)
+ ; TODO A more-informative exception
+ (raise status))))))