@@ -23,26 +23,18 @@ module Servant.Reflex
2323 , module Servant.Common.BaseUrl
2424 ) where
2525
26−import Control.Applicative ((<$>), liftA2)
27−import Control.Monad.Trans.Except
28−import qualified Data.ByteString.Char8 as BS
29−import Data.ByteString.Lazy (ByteString)
30−import qualified Data.CaseInsensitive as CI
31−import Data.List
26+import Control.Applicative
3227import Data.Proxy
3328import Data.String.Conversions
34−import Data.Maybe (maybeToList)
35−import Data.Text (unpack)
36−import Data.Traversable (sequenceA)
29+import Data.Text (Text)
30+import qualified Data.Text as T
31+import Data.Text.Encoding
3732import GHC.TypeLits
38−import qualified Network.HTTP.Media as M
3933import Servant.API
4034import Servant.Common.BaseUrl
4135import Servant.Common.Req
42−import Servant.API.ContentTypes
4336import Reflex
4437import Reflex.Dom
45−import Reflex.Dom.Xhr
4638
4739-- * Accessing APIs as a Client
4840
@@ -92,7 +84,7 @@ instance (MonadWidget t m, KnownSymbol capture, ToHttpApiData a, HasClient t m s
9284 => HasClient t m (Capture capture a :> sublayout) where
9385
9486 type Client t m (Capture capture a :> sublayout) =
95− Behavior t (Either String a) -> Client t m sublayout
87+ Behavior t (Either Text a) -> Client t m sublayout
9688
9789 clientWithRoute Proxy q req baseurl val =
9890 clientWithRoute (Proxy :: Proxy sublayout)
@@ -100,7 +92,7 @@ instance (MonadWidget t m, KnownSymbol capture, ToHttpApiData a, HasClient t m s
10092 (prependToPathParts p req)
10193 baseurl
10294
103− where p = (fmap . fmap) (unpack . toUrlPiece) val
95+ where p = (fmap . fmap) (toUrlPiece) val
10496
10597-- VERB (Returning content) --
10698instance {-# OVERLAPPABLE #-}
@@ -111,9 +103,9 @@ instance {-# OVERLAPPABLE #-}
111103 Event t () -> m (Event t (ReqResult a))
112104 -- TODO how to access input types here?
113105 -- ExceptT ServantError IO a
114− clientWithRoute Proxy q req baseurl =
106+ clientWithRoute Proxy _ req baseurl =
115107 performRequestCT (Proxy :: Proxy ct) method req' baseurl
116− where method = BS.unpack $ reflectMethod (Proxy :: Proxy method)
108+ where method = decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
117109 req' = req { reqMethod = method }
118110
119111-- -- VERB (No content) --
@@ -124,9 +116,9 @@ instance {-# OVERLAPPING #-}
124116 Event t () -> m (Event t (ReqResult NoContent))
125117 -- TODO: how to access input types here?
126118 -- ExceptT ServantError IO NoContent
127− clientWithRoute Proxy q req baseurl =
119+ clientWithRoute Proxy _ req baseurl =
128120 performRequestNoBody method req baseurl
129− where method = BS.unpack $ reflectMethod (Proxy :: Proxy method)
121+ where method = decodeUtf8 $ reflectMethod (Proxy :: Proxy method)
130122
131123-- -- HEADERS Verb (Content) --
132124-- -- Headers combinator not treated in fully general case,
@@ -184,9 +176,9 @@ instance (KnownSymbol sym, ToHttpApiData a,
184176 => HasClient t m (Header sym a :> sublayout) where
185177
186178 type Client t m (Header sym a :> sublayout) =
187− Behavior t (Either String a) -> Client t m sublayout
179+ Behavior t (Either Text a) -> Client t m sublayout
188180
189− clientWithRoute Proxy q req baseurl mval =
181+ clientWithRoute Proxy q req baseurl _ =
190182 clientWithRoute (Proxy :: Proxy sublayout)
191183 q
192184 req
@@ -196,8 +188,6 @@ instance (KnownSymbol sym, ToHttpApiData a,
196188 -- )
197189 baseurl
198190
199− where hname = symbolVal (Proxy :: Proxy sym)
200−
201191-- | Using a 'HttpVersion' combinator in your API doesn't affect the client
202192-- functions.
203193instance HasClient t m sublayout
@@ -240,7 +230,7 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout, Reflex t)
240230
241231 type Client t m (QueryParam sym a :> sublayout) =
242232 -- TODO (Maybe a), or (Maybe (Maybe a))? (should the user be able to send a Nothing)
243− Behavior t (Either String a) -> Client t m sublayout
233+ Behavior t (Either Text a) -> Client t m sublayout
244234
245235 -- if mparam = Nothing, we don't add it to the query string
246236 clientWithRoute Proxy q req baseurl mparam =
@@ -248,8 +238,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout, Reflex t)
248238 (req {qParams = paramPair : qParams req}) baseurl
249239
250240 where pname = symbolVal (Proxy :: Proxy sym)
251− p prm = QueryPartParam $ (fmap . fmap) (unpack . toQueryParam) prm
252− paramPair = (pname, p mparam)
241+ p prm = QueryPartParam $ (fmap . fmap) (toQueryParam) prm
242+ paramPair = (T.pack pname, p mparam)
253243
254244-- | If you use a 'QueryParams' in one of your endpoints in your API,
255245-- the corresponding querying function will automatically take
@@ -288,9 +278,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout, Reflex t)
288278 clientWithRoute Proxy q req baseurl paramlist =
289279 clientWithRoute (Proxy :: Proxy sublayout) q req' baseurl
290280
291− where req' = req { qParams = (pname, params') : qParams req }
281+ where req' = req { qParams = (T.pack pname, params') : qParams req }
292282 pname = symbolVal (Proxy :: Proxy sym)
293− params' = QueryPartParams $ (fmap . fmap) (unpack . toQueryParam)
283+ params' = QueryPartParams $ (fmap . fmap) (toQueryParam)
294284 paramlist
295285
296286
@@ -328,7 +318,7 @@ instance (KnownSymbol sym, HasClient t m sublayout, Reflex t)
328318 clientWithRoute (Proxy :: Proxy sublayout) q req' baseurl
329319
330320 where req' = req { qParams = thisPair : qParams req }
331− thisPair = (pName, QueryPartFlag flag) :: (String, QueryPart t)
321+ thisPair = (T.pack pName, QueryPartFlag flag) :: (Text, QueryPart t)
332322 pName = symbolVal (Proxy :: Proxy sym)
333323
334324
@@ -336,16 +326,16 @@ instance (KnownSymbol sym, HasClient t m sublayout, Reflex t)
336326-- back the full `Response`.
337327-- TODO redo
338328instance (MonadWidget t m) => HasClient t m Raw where
339− type Client t m Raw = Behavior t (Either String XhrRequest)
329+ type Client t m Raw = Behavior t (Either Text (XhrRequest ()))
340330 -> Event t ()
341331 -> m (Event t (ReqResult ()))
342332
343333 -- clientWithRoute :: Proxy Raw -> Proxy m -> Req -> BaseUrl -> Client t m Raw
344− clientWithRoute p q req baseurl xhrs triggers = do
334+ clientWithRoute _ _ _ baseurl xhrs triggers = do
345335 let xhrs' = liftA2 (\x path -> case x of
346336 Left e -> Left e
347337 Right jx -> Right $ jx {_xhrRequest_url = path
348− ++ _xhrRequest_url jx})
338+ <> _xhrRequest_url jx})
349339 xhrs
350340 (showBaseUrl <$> current baseurl)
351341 reqs = tag xhrs' triggers
@@ -386,14 +376,14 @@ instance (MimeRender ct a, HasClient t m sublayout, Reflex t)
386376 => HasClient t m (ReqBody (ct ': cts) a :> sublayout) where
387377
388378 type Client t m (ReqBody (ct ': cts) a :> sublayout) =
389− Behavior t (Either String a) -> Client t m sublayout
379+ Behavior t (Either Text a) -> Client t m sublayout
390380
391381 clientWithRoute Proxy q req baseurl body =
392382 clientWithRoute (Proxy :: Proxy sublayout) q req' baseurl
393383 where req' = req { reqBody = bodyBytesCT }
394384 ctProxy = Proxy :: Proxy ct
395− ctString = show $ contentType ctProxy
396− --ctString = BS.unpack . CI.original . M.mainType $ contentType ctProxy
385+ ctString = T.pack $ show $ contentType ctProxy
386+ --ctString = decodeUtf8 . CI.original . M.mainType $ contentType ctProxy
397387 bodyBytesCT = Just $ (fmap . fmap)
398388 (\b -> (mimeRender ctProxy b, ctString))
399389 body
@@ -404,7 +394,7 @@ instance (KnownSymbol path, HasClient t m sublayout, Reflex t) => HasClient t m
404394
405395 clientWithRoute Proxy q req baseurl =
406396 clientWithRoute (Proxy :: Proxy sublayout) q
407− (prependToPathParts (constant (Right p)) req)
397+ (prependToPathParts (constant (Right $ T.pack p)) req)
408398 baseurl
409399
410400 where p = symbolVal (Proxy :: Proxy path)