on master help sign in
inline performAJAX
commit11e01e767fa162179460da00764059ee9e78d809 authorGreg Hale <imalsogreg@gmail.com> dateSun 7 Feb 2016, 13:02 (11 years ago) parent19b3e44
2 files changed+36−20
src/Servant/Common/Req.hs+6−5 src/Servant/Reflex.hs+30−15
src/Servant/Common/Req.hs+6−5
@@ -55,7 +55,7 @@ data ServantError
5555 deriving (Show, Typeable)
5656
5757instance Exception ServantError
58−
58+
5959data Req = Req
6060 { reqPath :: String
6161 , qs :: QueryText
@@ -129,18 +129,19 @@ performRequestNoBody = undefined
129129-- performRequest :: Method -> Req -> BaseUrl -> Manager
130130-- -> ExceptT ServantError IO ( Int, ByteString, MediaType
131131-- , [HTTP.Header], Response ByteString)
132+-- performRequest :: MonadWidget t m => Method -> Req -> BaseUrl ->
132133-- performRequest reqMethod req reqHost manager = do
133134-- partialRequest <- liftIO $ reqToRequest req reqHost
134−
135+--
135136-- let request = partialRequest { Client.method = reqMethod
136137-- , checkStatus = \ _status _headers _cookies -> Nothing
137138-- }
138−
139+--
139140-- eResponse <- liftIO $ catchConnectionError $ Client.httpLbs request manager
140141-- case eResponse of
141142-- Left err ->
142143-- throwE . ConnectionError $ SomeException err
143−
144+--
144145-- Right response -> do
145146-- let status = Client.responseStatus response
146147-- body = Client.responseBody response
@@ -151,7 +152,7 @@ performRequestNoBody = undefined
151152-- Just t -> case parseAccept t of
152153-- Nothing -> throwE $ InvalidContentTypeHeader (cs t) body
153154-- Just t' -> pure t'
154−-- unless (status_code >= 200 && status_code < 300) $
155+-- unless (status_code >= 200 && status_code < 300) $
155156-- throwE $ FailureResponse status ct body
156157-- return (status_code, body, ct, hrds, response)
157158
src/Servant/Reflex.hs+30−15
@@ -84,20 +84,36 @@ class HasReflexClient layout where
8484 -> Client (Input layout) (Output layout)
8585
8686
87−-- -- | If you have a 'Get' endpoint in your API, the client
88−-- -- side querying function that is created when calling 'client'
89−-- -- will just require an argument that specifies the scheme, host
90−-- -- and port to send the request to.
91−-- instance
92−-- #if MIN_VERSION_base(4,8,0)
93−-- {-# OVERLAPPABLE #-}
94−-- #endif
95−-- (MimeUnrender ct result) => HasReflexClient (Get (ct ': cts) result) where
96−-- type Input (Get (ct ': cts) result) = ()
97−-- -- type Output (Get (ct' : cts) result) = result
98−-- type Client (Get (ct ': cts) result) = Final result
99−-- clientWithRoute Proxy req baseurl _ =
100−-- snd <$> performRequestCT (Proxy :: Proxy ct) H.methodGet req baseurl
87+-- | If you have a 'Get' endpoint in your API, the client
88+-- side querying function that is created when calling 'client'
89+-- will just require an argument that specifies the scheme, host
90+-- and port to send the request to.
91+instance
92+#if MIN_VERSION_base(4,8,0)
93+ {-# OVERLAPPABLE #-}
94+#endif
95+ (MimeUnrender ct result) => HasReflexClient (Get (ct ': cts) result) where
96+ type Input (Get (ct ': cts) result) = ()
97+ type Output (Get (ct' : cts) result) = result
98+ clientWithRoute Proxy req baseurl trigEvents = performAJAX requestBuilder responseParser
99+ where
100+ requestBuilder _ = XhrRequest "GET" (showBaseUrl baseurl) def
101+ responseParser xhrResp =
102+
103+performAJAX
104+ :: (MonadWidget t m)
105+ => (a -> XhrRequest)
106+ -- ^ Function to build the request
107+ -> (XhrResponse -> b)
108+ -- ^ Function to parse the response
109+ -> Event t a
110+ -> m (Event t (a, b))
111+performAJAX mkRequest parseResponse req =
112+ performEventAsync $ ffor req $ \a cb -> do
113+ _ <- newXMLHttpRequest (mkRequest a) $ \response ->
114+ liftIO $ cb (a, parseResponse response)
115+ return ()
116+
101117
102118instance
103119#if MIN_VERSION_base(4,8,0)
@@ -106,7 +122,6 @@ instance
106122 HasReflexClient (Get (ct ': cts) ()) where
107123 type Input (Get (ct ': cts) ()) = ()
108124 type Output (Get (ct ': cts) ()) = ()
109− -- type Client (Get (ct ': cts) ()) = Final ()
110125 clientWithRoute Proxy req baseurl trigEvents =
111126 performAJAX requestBuilder responseParser trigEvents
112127 where