module ReviewBoard.Request (
new,
delete,
save,
discard,
publish,
update_from_changenum,
star,
unstar,
all,
to,
from,
count,
directly,
repositories,
reviewrequest,
comments,
replies,
draft,
diff,
screenshot,
reviewrequests,
reviews,
groups,
group,
users,
user,
set,
file,
line,
repository,
changenum,
RRField(..),
apiGet,
apiPost,
httpGet,
httpPost,
UrlPath,
mkup,
mkpup
) where
import Prelude hiding (all)
import ReviewBoard.Browser
import ReviewBoard.Core
import Network.HTTP hiding (user)
import Control.Monad.Trans
import qualified Control.Monad.State as S
import Data.Maybe
import Network.URI
new = mkup "new"
delete = mkup "delete"
save = mkup "save"
discard = mkup "discard"
publish = mkup "publish"
update_from_changenum = mkup "repositories"
star = mkup "star"
unstar = mkup "unstar"
all = mkup "all"
to = mkup "to"
from = mkup "from"
count = mkup "count"
directly = mkup "directly"
repositories = mkup "repositories"
reviewrequest = mkup "reviewrequest"
comments = mkup "comments"
replies = mkup "replies"
draft = mkup "draft"
diff = mkup "diff"
screenshot = mkup "screenshot"
reviewrequests = mkpup "reviewrequests" :: (Maybe Integer -> UrlPath -> UrlPath)
reviews = mkpup "reviews" :: (Maybe Integer -> UrlPath -> UrlPath)
groups = mkpup "groups" :: (Maybe String -> UrlPath -> UrlPath)
group = mkpup "group" :: (Maybe String -> UrlPath -> UrlPath)
users = mkpup "users" :: (Maybe String -> UrlPath -> UrlPath)
user = mkpup "user" :: (Maybe String -> UrlPath -> UrlPath)
set = mkpup "set" :: (Maybe RRField -> UrlPath -> UrlPath)
file = mkpup "file" :: (Maybe Integer -> UrlPath -> UrlPath)
line = mkpup "line" :: (Maybe Integer -> UrlPath -> UrlPath)
repository id = mkup "repository" . mkup (show id)
changenum n = mkup "changenum " . mkup (show n)
data RRField
= STATUS
| PUBLIC
| SUMMARY
| DESCRIPTION
| TESTING_DONE
| BUGS_CLOSED
| BRANCH
| TARGET_GROUPS
| TARGET_PEOPLE
deriving (Eq, Enum, Bounded)
rrFieldMap :: [(RRField, String)]
rrFieldMap =
[ (STATUS, "status")
, (PUBLIC, "public")
, (SUMMARY, "summary")
, (DESCRIPTION, "description")
, (TESTING_DONE, "testing_done")
, (BUGS_CLOSED, "bugs_closed")
, (BRANCH, "branch")
, (TARGET_GROUPS, "target_groups")
, (TARGET_PEOPLE, "target_people") ]
instance Show RRField where
show = fromJust . flip lookup rrFieldMap
apiGet :: (UrlPath -> UrlPath) -> [FormVar] -> RBAction RBResponse
apiGet u vs = mkApiURI (u "") >>= rbRequest API GET vs
apiPost :: (UrlPath -> UrlPath) -> [FormVar] -> RBAction RBResponse
apiPost u vs = mkApiURI (u "") >>= rbRequest API POST vs
httpGet :: String -> [FormVar] -> RBAction RBResponse
httpGet u vs = mkHttpURI u >>= rbRequest HTTP GET vs
httpPost :: String -> [FormVar] -> RBAction RBResponse
httpPost u vs = mkHttpURI u >>= rbRequest HTTP POST vs
rbRequest :: RBRequestType -> RequestMethod -> [FormVar] -> URI -> RBAction RBResponse
rbRequest rt rm vs u = do
let form = Form rm u vs
runRequest rt form return
type UrlPath = String
mkup :: String -> (UrlPath -> UrlPath)
mkup s = ((s ++ "/") ++)
mkpup :: Show a => String -> (Maybe a -> UrlPath -> UrlPath)
mkpup s = \p u -> maybe (noparam u) (flip param u) p
where
noparam = mkup s
param i = mkup s . mkup (show i)