Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add `couch'`, fix replicateDB

Signed-off-by: Alexander Dorofeev <aka.spin@gmail.com>
  • Loading branch information...
commit 939fb068d5b67bcddd5e1187ff1f2ed300740bbd 1 parent 26c03e4
@akaspin authored
View
2  ChangeLog
@@ -1,4 +1,4 @@
- In Work, Version 0.7.5 (Stable) * Refactorings and cleanups. 09.03.2012, Version 0.7.1 (Stable) * Add `couchPrefix` to `CouchConnection`. 09.03.2012, Version 0.7.0 (Stable) * Rename `mkParam` to `viewQP` and add param name to arguments. * Add view query helpers. 09.03.2012, Version 0.6.0 (Stable) * Remove `couchPutView_` * Rename `couchPutView'` to `couchPutView` * `couchViewPost` and `couchViewPost'` * Remove `quoteQueryParam` * `mkParam` * Update deps constraints 15.02.2012, Version 0.5.3 (Stable) * Fix views with "update_seq" 04.02.2012, Version 0.5.1 (Stable) * Docs. * Quote query parameter helper. 03.02.2012, Version 0.5.0 (Stable) * Expose parser-independent methods in main module. * Expose Implicit methods. * Remove ambiguous database parameter from connection. * Add unambiguous database parameter in all methods. 03.02.2012, Version 0.4.2 * Update dependencies contraints. 24.01.2012, Version 0.4.1 * `couchSecureDB` lost db argument. 24.01.2012, Version 0.4.0 * Detailed error type. * Throw exception on `304`. * Database API lost db argument. * `couchViewPut*` -> `couchPutView` 22.01.2012, Version 0.3.0.1 * Constraints in cabal 19.01.2012, Version 0.3.0.0 * Fix for new http-conduit types 16.01.2012, Version 0.2.1.1 * Fixes for couchViewPut and couchSecureDB. 16.01.2012, Version 0.2.1 * Secure DB. 16.01.2012, Version 0.2.0.1 * Avoid escaping path in couch' 16.01.2012, Version 0.2.0 * Low-lewel API * "Don't care" versions of couchPut. * Brain-free couchRev'. * All database methods ignores DB in connection. * couchPutDB' renamed to couchPutDB_. * Database replication. 14.01.2012, Version 0.1.3.0 * Safe version of couchViewPut 14.01.2012, Version 0.1.2.0 * Authentification 11.01.2012, Version 0.1.1.0
+ In Work, Version 0.7.5 (Stable) * Refactorings and cleanups. * Path-independent `couch'`. 09.03.2012, Version 0.7.1 (Stable) * Add `couchPrefix` to `CouchConnection`. 09.03.2012, Version 0.7.0 (Stable) * Rename `mkParam` to `viewQP` and add param name to arguments. * Add view query helpers. 09.03.2012, Version 0.6.0 (Stable) * Remove `couchPutView_` * Rename `couchPutView'` to `couchPutView` * `couchViewPost` and `couchViewPost'` * Remove `quoteQueryParam` * `mkParam` * Update deps constraints 15.02.2012, Version 0.5.3 (Stable) * Fix views with "update_seq" 04.02.2012, Version 0.5.1 (Stable) * Docs. * Quote query parameter helper. 03.02.2012, Version 0.5.0 (Stable) * Expose parser-independent methods in main module. * Expose Implicit methods. * Remove ambiguous database parameter from connection. * Add unambiguous database parameter in all methods. 03.02.2012, Version 0.4.2 * Update dependencies contraints. 24.01.2012, Version 0.4.1 * `couchSecureDB` lost db argument. 24.01.2012, Version 0.4.0 * Detailed error type. * Throw exception on `304`. * Database API lost db argument. * `couchViewPut*` -> `couchPutView` 22.01.2012, Version 0.3.0.1 * Constraints in cabal 19.01.2012, Version 0.3.0.0 * Fix for new http-conduit types 16.01.2012, Version 0.2.1.1 * Fixes for couchViewPut and couchSecureDB. 16.01.2012, Version 0.2.1 * Secure DB. 16.01.2012, Version 0.2.0.1 * Avoid escaping path in couch' 16.01.2012, Version 0.2.0 * Low-lewel API * "Don't care" versions of couchPut. * Brain-free couchRev'. * All database methods ignores DB in connection. * couchPutDB' renamed to couchPutDB_. * Database replication. 14.01.2012, Version 0.1.3.0 * Safe version of couchViewPut 14.01.2012, Version 0.1.2.0 * Authentification 11.01.2012, Version 0.1.1.0
* API Changes. `couchViewPut` moved to Database.CouchDB.Design and lost
language argument.
* Tests. 10.01.2012, Version 0.1.0.1
View
2  couchdb-conduit.cabal
@@ -1,5 +1,5 @@
name: couchdb-conduit
-version: 0.7.5
+version: 0.7.6
cabal-version: >= 1.8
build-type: Simple
stability: Stable
View
4 src/Database/CouchDB/Conduit/DB.hs
@@ -30,7 +30,7 @@ import qualified Network.HTTP.Types as HT
import Database.CouchDB.Conduit.Internal.Connection
(MonadCouch(..), Path, mkPath)
-import Database.CouchDB.Conduit.LowLevel (couch, protect, protect')
+import Database.CouchDB.Conduit.LowLevel (couch, couch', protect, protect')
-- | Create CouchDB database.
@@ -91,7 +91,7 @@ couchReplicateDB :: MonadCouch m =>
-> Bool -- ^ Cancel flag
-> ResourceT m ()
couchReplicateDB source target createTarget continuous cancel =
- void $ couch HT.methodPost "_replicate" [] []
+ void $ couch' HT.methodPost (const "/_replicate") [] []
reqBody protect'
where
reqBody = H.RequestBodyLBS $ A.encode $ A.object [
View
6 src/Database/CouchDB/Conduit/Internal/Connection.hs
@@ -75,7 +75,7 @@ mkPath :: [Path] -- ^ Path fragments be escaped.
-> Path
mkPath = BLB.toByteString . HT.encodePathSegments .
map TE.decodeUtf8 . filter (/="")
-
+
-----------------------------------------------------------------------------
-- Connection
-----------------------------------------------------------------------------
@@ -97,8 +97,8 @@ data CouchConnection = CouchConnection {
, couchPass :: B.ByteString
-- ^ CouchDB password. By default is 'B.empty'.
, couchPrefix :: B.ByteString
- -- ^ CouchDB database prefix. It will prepended to DB pathes.
- -- Must be fully valid DB name fragment.
+ -- ^ CouchDB database prefix. It will prepended to first fragment of
+ -- request path. Must be fully valid DB name fragment.
}
instance Default CouchConnection where
View
34 src/Database/CouchDB/Conduit/LowLevel.hs
@@ -4,8 +4,14 @@
-- | Low-level method and tools of accessing CouchDB.
module Database.CouchDB.Conduit.LowLevel (
+ -- * Response
CouchResponse,
+
+ -- * Low-level access
couch,
+ couch',
+
+ -- * Response protection
protect,
protect'
) where
@@ -42,20 +48,40 @@ type CouchResponse m = H.Response (Source m B.ByteString)
couch :: MonadCouch m =>
HT.Method -- ^ Method
-> Path -- ^ Correct 'Path' with escaped fragments.
+ -- 'couchPrefix' will be prepended to path.
-> HT.RequestHeaders -- ^ Headers
-> HT.Query -- ^ Query args
-> H.RequestBody m -- ^ Request body
-> (CouchResponse m -> ResourceT m (CouchResponse m))
-- ^ Protect function. See 'protect'
-> ResourceT m (CouchResponse m)
-couch meth path hdrs qs reqBody protectFn = do
+couch meth path =
+ couch' meth withPrefix
+ where
+ withPrefix prx
+ | B.null prx = path
+ | otherwise = "/" <> prx <> B.tail path
+
+-- | More generalized version of 'couch'. Instead 'Path' it takes function
+-- what takes prefix and returns a path.
+couch' :: MonadCouch m =>
+ HT.Method -- ^ Method
+ -> (Path -> Path) -- ^ 'couchPrefix'->Path function. Output must
+ -- be correct 'Path' with escaped fragments.
+ -> HT.RequestHeaders -- ^ Headers
+ -> HT.Query -- ^ Query args
+ -> H.RequestBody m -- ^ Request body
+ -> (CouchResponse m -> ResourceT m (CouchResponse m))
+ -- ^ Protect function. See 'protect'
+ -> ResourceT m (CouchResponse m)
+couch' meth pathFn hdrs qs reqBody protectFn = do
conn <- lift couchConnection
let req = H.def
{ H.method = meth
, H.host = couchHost conn
, H.requestHeaders = hdrs
, H.port = couchPort conn
- , H.path = withPrefix $ couchPrefix conn
+ , H.path = pathFn $ couchPrefix conn
, H.queryString = HT.renderQuery False qs
, H.requestBody = reqBody
, H.checkStatus = const . const $ Nothing }
@@ -64,10 +90,6 @@ couch meth path hdrs qs reqBody protectFn = do
(couchLogin conn) (couchPass conn) req
res <- H.http req' (fromJust $ couchManager conn)
protectFn res
- where
- withPrefix prx
- | B.null prx = path
- | otherwise = "/" <> prx <> B.tail path
-- | Protect 'H.Response' from bad status codes. If status code in list
-- of status codes - just return response. Otherwise - throw 'CouchError'.
Please sign in to comment.
Something went wrong with that request. Please try again.