module Network.Riak.HTTP.JSON
    (put, tryPut, putWithRandomKey, putWithIndexes, get, defaultClient)
where

import Network.Riak.HTTP.Types
import Network.Riak.HTTP.Internal
import Network.HTTP
import Data.Aeson
import Data.ByteString.Lazy (ByteString(..))
import qualified Data.ByteString.Lazy.Char8 as BLC

put :: (ToJSON v) => Client -> Bucket -> Key -> v -> IO (Either String ())
put c b k v = putWithIndexes c b k [] v

putWithRandomKey :: (ToJSON v) => Client -> Bucket -> v
                 -> IO (Either String Key)
putWithRandomKey c b = putWithContentTypeWithRandomKey "application/json" c
    b . encode

-- | Try a put, finding a client where it succeeds
tryPut :: (ToJSON v) => [Client] -> Bucket -> Key -> v
       -> IO (Either String ())
tryPut c b k v = tryPutWithIndexes c b k [] v

putWithIndexes :: (ToJSON v) => Client -> Bucket -> Key -> [IndexTag] -> v
               -> IO (Either String ())
putWithIndexes client bucket key indexTags value = putWithContentType
    "application/json" client bucket key indexTags (encode value)

tryPutWithIndexes :: (ToJSON v) => [Client] -> Bucket -> Key -> [IndexTag]
                  -> v -> IO (Either String ())
tryPutWithIndexes clients bucket key indexTags value = tryClients clients $
    \client -> putWithIndexes client bucket key indexTags value
