(module api mzscheme
(require (lib "contract.ss")
(lib "etc.ss")
(lib "plt-match.ss")
(lib "pretty.ss")
(all-except (lib "list.ss" "srfi" "1") any)
(lib "time.ss" "srfi" "19")
(file "base.ss")
(file "config.ss")
(file "request.ss")
(file "result-parse.ss")
(file "result-struct.ss")
(file "url.ss"))
(define (last-updated)
(parse-update-element
(send-request (last-updated-url))))
(define (get-tags)
(parse-tags-element
(send-request (get-tags-url))))
(define (rename-tag! old new)
(parse-result-element
(send-request (rename-tag-url old new))))
(define get-posts
(opt-lambda ([tag empty] [date empty] [url empty])
(parse-posts-element
(send-request (get-posts-url tag date url)))))
(define recent-posts
(opt-lambda ([tag empty] [count empty])
(parse-posts-element
(send-request (recent-posts-url tag count)))))
(define all-posts
(opt-lambda ([tag empty])
(parse-posts-element
(send-request (all-posts-url tag)))))
(define post-dates
(opt-lambda ([tag empty])
(parse-dates-element
(send-request (post-dates-url tag)))))
(define add-post!
(opt-lambda (p [replace? empty] [shared? empty])
(match p
[(struct post (url description extended tags date))
(add-post/raw! url
description
(if extended extended empty)
(if (null? tags) empty tags)
(if date date empty)
replace?
shared?)])))
(define add-post/raw!
(opt-lambda (url description [extended empty] [tags empty] [date empty] [replace? empty] [shared? empty])
(parse-result-element
(send-request (add-post-url url description extended tags date replace? shared?)))))
(define (delete-post! post)
(delete-post/raw! (post-url post)))
(define (delete-post/raw! url)
(parse-result-element
(send-request (delete-post-url url))))
(define (all-bundles)
(parse-bundles-element
(send-request (all-bundles-url))))
(define (update-bundle! bundle)
(update-bundle/raw! (bundle-name bundle)
(bundle-tags bundle)))
(define (update-bundle/raw! name tags)
(parse-result-element
(send-request (set-bundle-url name tags))))
(define (delete-bundle! bundle)
(delete-bundle/raw! (bundle-name bundle)))
(define (delete-bundle/raw! name)
(parse-result-element
(send-request (delete-bundle-url name))))
(provide/contract
[last-updated (-> srfi:date?)]
[get-tags (-> (listof (cons/c string? integer?)))]
[rename-tag! (-> string? string? void?)]
[get-posts (opt-> ()
((maybe/c string?)
(maybe/c srfi:date?)
(maybe/c string?))
(listof post?))]
[recent-posts (opt-> ()
((maybe/c string?) (maybe/c (and/c integer? (between/c 0 100))))
(listof post?))]
[all-posts (opt-> () ((maybe/c string?)) (listof post?))]
[post-dates (opt-> () ((maybe/c string?)) (listof (cons/c srfi:date? integer?)))]
[add-post! (opt-> (post?)
((maybe/c boolean?)
(maybe/c boolean?))
void?)]
[add-post/raw! (opt-> (string? string?)
((maybe/c string?)
(maybe/c (listof string?))
(maybe/c srfi:date?)
(maybe/c boolean?)
(maybe/c boolean?))
void?)]
[delete-post! (-> string? void?)]
[delete-post/raw! (-> string? void?)]
[all-bundles (-> string?)]
[update-bundle! (-> bundle? void?)]
[update-bundle/raw! (-> string? (listof string?) void?)]
[delete-bundle! (-> bundle? void?)]
[delete-bundle/raw! (-> string? void?)])
)