@@ -25,11 +25,17 @@ import Data.Text (Text)
25import Network.HTTP.Conduit
25import Network.HTTP.Conduit
26import qualified Network.HTTP.Types.Method as NHTM
26import qualified Network.HTTP.Types.Method as NHTM
27import qualified Network.HTTP.Types.Status as NHTS
27import qualified Network.HTTP.Types.Status as NHTS
28+import Prelude hiding (head)
29import Database.Bloodhound.Types
30import Database.Bloodhound.Types
30import Database.Bloodhound.Types.Class
31import Database.Bloodhound.Types.Class
31import Database.Bloodhound.Types.Instances
32import Database.Bloodhound.Types.Instances
34+-- find way to avoid destructuring Servers and Indexes?
35+-- make get, post, put, delete helpers.
36+-- make dispatch take URL last for better variance and
37+-- utilization of partial application
33mkShardCount :: Int -> Maybe ShardCount
39mkShardCount :: Int -> Maybe ShardCount
34mkShardCount n
40mkShardCount n
35 | n < 1 = Nothing
41 | n < 1 = Nothing
@@ -47,9 +53,9 @@ responseIsError resp = NHTS.statusCode (responseStatus resp) > 299
48emptyBody = L.pack ""
54emptyBody = L.pack ""
50−dispatch :: String -> Method -> Maybe L.ByteString
56+dispatch :: Method -> String -> Maybe L.ByteString
51 -> IO Reply
57 -> IO Reply
52−dispatch url method body = do
58+dispatch method url body = do
53 initReq <- parseUrl url
59 initReq <- parseUrl url
54 let reqBody = RequestBodyLBS $ fromMaybe emptyBody body
60 let reqBody = RequestBodyLBS $ fromMaybe emptyBody body
55 let req = initReq { method = method
61 let req = initReq { method = method
@@ -60,6 +66,16 @@ dispatch url method body = do
60joinPath :: [String] -> String
66joinPath :: [String] -> String
61joinPath = intercalate "/"
67joinPath = intercalate "/"
69+delete = flip (dispatch NHTM.methodDelete) $ Nothing
70+get = flip (dispatch NHTM.methodGet) $ Nothing
71+head = flip (dispatch NHTM.methodHead) $ Nothing
72+put = dispatch NHTM.methodPost
73+post = dispatch NHTM.methodPost
75+-- indexDocument s ix name doc = put (root </> s </> ix </> name </> doc) (Just encode doc)
76+-- http://hackage.haskell.org/package/http-client-lens-0.1.0/docs/Network-HTTP-Client-Lens.html
77+-- https://github.com/supki/libjenkins/blob/master/src/Jenkins/Rest/Internal.hs
63getStatus :: Server -> IO (Maybe (Status Version))
79getStatus :: Server -> IO (Maybe (Status Version))
64getStatus (Server server) = do
80getStatus (Server server) = do
65 request <- parseUrl $ joinPath [server]
81 request <- parseUrl $ joinPath [server]
@@ -68,36 +84,31 @@ getStatus (Server server) = do
69createIndex :: Server -> IndexSettings -> IndexName -> IO Reply
85createIndex :: Server -> IndexSettings -> IndexName -> IO Reply
70createIndex (Server server) indexSettings (IndexName indexName) =
86createIndex (Server server) indexSettings (IndexName indexName) =
71− dispatch url method body where
87+ put url body
72− url = joinPath [server, indexName]
88+ where url = joinPath [server, indexName]
73− method = NHTM.methodPut
74− body = Just $ encode indexSettings
89+ body = Just $ encode indexSettings
76deleteIndex :: Server -> IndexName -> IO Reply
91deleteIndex :: Server -> IndexName -> IO Reply
77deleteIndex (Server server) (IndexName indexName) =
92deleteIndex (Server server) (IndexName indexName) =
78− dispatch url method body where
79− url = joinPath [server, indexName]
93+ delete $ joinPath [server, indexName]
80− method = NHTM.methodDelete
83respIsTwoHunna :: Reply -> Bool
95respIsTwoHunna :: Reply -> Bool
84respIsTwoHunna resp = NHTS.statusCode (responseStatus resp) == 200
96respIsTwoHunna resp = NHTS.statusCode (responseStatus resp) == 200
86existentialQuery url = do
98existentialQuery url = do
87− reply <- dispatch url NHTM.methodHead Nothing
99+ reply <- head url
88 return (reply, respIsTwoHunna reply)
100 return (reply, respIsTwoHunna reply)
90indexExists :: Server -> IndexName -> IO Bool
102indexExists :: Server -> IndexName -> IO Bool
91indexExists (Server server) (IndexName indexName) = do
103indexExists (Server server) (IndexName indexName) = do
92 (reply, exists) <- existentialQuery url
104 (reply, exists) <- existentialQuery url
93− return exists where
105+ return exists
94− url = joinPath [server, indexName]
106+ where url = joinPath [server, indexName]
96refreshIndex :: Server -> IndexName -> IO Reply
108refreshIndex :: Server -> IndexName -> IO Reply
97−refreshIndex (Server server) (IndexName indexName) = dispatch url method body where
109+refreshIndex (Server server) (IndexName indexName) =
98− url = joinPath [server, indexName, "_refresh"]
111+ where url = joinPath [server, indexName, "_refresh"]
99− method = NHTM.methodPost
102stringifyOCIndex oci = case oci of
113stringifyOCIndex oci = case oci of
103 OpenIndex -> "_open"
114 OpenIndex -> "_open"
@@ -105,11 +116,9 @@ stringifyOCIndex oci = case oci of
106openOrCloseIndexes :: OpenCloseIndex -> Server -> IndexName -> IO Reply
117openOrCloseIndexes :: OpenCloseIndex -> Server -> IndexName -> IO Reply
107openOrCloseIndexes oci (Server server) (IndexName indexName) =
118openOrCloseIndexes oci (Server server) (IndexName indexName) =
108− dispatch url method body where
119+ post url Nothing
109− ociString = stringifyOCIndex oci
120+ where ociString = stringifyOCIndex oci
110− url = joinPath [server, indexName, ociString]
121+ url = joinPath [server, indexName, ociString]
111− method = NHTM.methodPost
114openIndex :: Server -> IndexName -> IO Reply
123openIndex :: Server -> IndexName -> IO Reply
115openIndex = openOrCloseIndexes OpenIndex
124openIndex = openOrCloseIndexes OpenIndex
@@ -120,40 +129,31 @@ closeIndex = openOrCloseIndexes CloseIndex
120createMapping :: ToJSON a => Server -> IndexName
129createMapping :: ToJSON a => Server -> IndexName
121 -> MappingName -> a -> IO Reply
130 -> MappingName -> a -> IO Reply
122createMapping (Server server) (IndexName indexName) (MappingName mappingName) mapping =
131createMapping (Server server) (IndexName indexName) (MappingName mappingName) mapping =
123− dispatch url method body where
132+ put url body
124− url = joinPath [server, indexName, mappingName, "_mapping"]
133+ where url = joinPath [server, indexName, mappingName, "_mapping"]
125− method = NHTM.methodPut
126− body = Just $ encode mapping
134+ body = Just $ encode mapping
128deleteMapping :: Server -> IndexName -> MappingName -> IO Reply
136deleteMapping :: Server -> IndexName -> MappingName -> IO Reply
129deleteMapping (Server server) (IndexName indexName) (MappingName mappingName) =
137deleteMapping (Server server) (IndexName indexName) (MappingName mappingName) =
130− dispatch url method body where
131− url = joinPath [server, indexName, mappingName, "_mapping"]
138+ delete $ joinPath [server, indexName, mappingName, "_mapping"]
132− method = NHTM.methodDelete
135indexDocument :: ToJSON doc => Server -> IndexName -> MappingName
140indexDocument :: ToJSON doc => Server -> IndexName -> MappingName
136 -> doc -> DocId -> IO Reply
141 -> doc -> DocId -> IO Reply
137indexDocument (Server server) (IndexName indexName)
142indexDocument (Server server) (IndexName indexName)
138 (MappingName mappingName) document (DocId docId) =
143 (MappingName mappingName) document (DocId docId) =
139− dispatch url method body where
144+ put url body
140− url = joinPath [server, indexName, mappingName, docId]
145+ where url = joinPath [server, indexName, mappingName, docId]
141− method = NHTM.methodPut
142− body = Just (encode document)
146+ body = Just (encode document)
144deleteDocument :: Server -> IndexName -> MappingName
148deleteDocument :: Server -> IndexName -> MappingName
145 -> DocId -> IO Reply
149 -> DocId -> IO Reply
146deleteDocument (Server server) (IndexName indexName)
150deleteDocument (Server server) (IndexName indexName)
147 (MappingName mappingName) (DocId docId) =
151 (MappingName mappingName) (DocId docId) =
148− dispatch url method body where
149− url = joinPath [server, indexName, mappingName, docId]
152+ delete $ joinPath [server, indexName, mappingName, docId]
150− method = NHTM.methodDelete
153bulk :: Server -> [BulkOperation] -> IO Reply
154bulk :: Server -> [BulkOperation] -> IO Reply
154−bulk (Server server) bulkOps = dispatch url method body where
155+bulk (Server server) bulkOps = post url body where
155 url = joinPath [server, "_bulk"]
156 url = joinPath [server, "_bulk"]
156− method = NHTM.methodPost
157 body = Just $ collapseStream bulkOps
157 body = Just $ collapseStream bulkOps
159collapseStream :: [BulkOperation] -> L.ByteString
159collapseStream :: [BulkOperation] -> L.ByteString
@@ -201,20 +201,18 @@ getStreamChunk (BulkUpdate (IndexName indexName)
201getDocument :: Server -> IndexName -> MappingName
201getDocument :: Server -> IndexName -> MappingName
202 -> DocId -> IO Reply
202 -> DocId -> IO Reply
203getDocument (Server server) (IndexName indexName) (MappingName mappingName) (DocId docId) =
203getDocument (Server server) (IndexName indexName) (MappingName mappingName) (DocId docId) =
204− dispatch url method body where
205− url = joinPath [server, indexName, mappingName, docId]
204+ get $ joinPath [server, indexName, mappingName, docId]
206− method = NHTM.methodGet
209documentExists :: Server -> IndexName -> MappingName
206documentExists :: Server -> IndexName -> MappingName
210 -> DocId -> IO Bool
207 -> DocId -> IO Bool
211−documentExists (Server server) (IndexName indexName) (MappingName mappingName) (DocId docId) = do
208+documentExists (Server server) (IndexName indexName)
209+ (MappingName mappingName) (DocId docId) = do
212 (reply, exists) <- existentialQuery url
210 (reply, exists) <- existentialQuery url
213 return exists where
211 return exists where
214 url = joinPath [server, indexName, mappingName, docId]
212 url = joinPath [server, indexName, mappingName, docId]
216dispatchSearch :: String -> Search -> IO Reply
214dispatchSearch :: String -> Search -> IO Reply
217−dispatchSearch url search = dispatch url NHTM.methodPost (Just (encode search))
215+dispatchSearch url search = post url (Just (encode search))
219searchAll :: Server -> Search -> IO Reply
217searchAll :: Server -> Search -> IO Reply
220searchAll (Server server) search = dispatchSearch url search where
218searchAll (Server server) search = dispatchSearch url search where