1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Servant.Client where
5import Control.Concurrent
6import Control.Monad.Catch
7import Data.ByteString.Lazy
10import Network.HTTP.Client hiding (Proxy)
11import Network.HTTP.Types
13import System.IO.Unsafe
15-- * Accessing APIs as a Client
17-- | 'client' allows you to produce operations to query an API from a client.
18client :: HasClient layout => Proxy layout -> Client layout
19client p = clientWithRoute p defReq
21class HasClient layout where
22 type Client layout :: *
23 clientWithRoute :: Proxy layout -> Req -> Client layout
28 , reqBody :: ByteString
34appendToPath :: String -> Req -> Req
36 req { reqPath = reqPath req ++ "/" ++ p }
38appendToQueryString :: Text -- ^ param name
39 -> Maybe Text -- ^ param value
42appendToQueryString pname pvalue req =
43 req { qs = qs req ++ [(pname, pvalue)]
46setRQBody :: ByteString -> Req -> Req
47setRQBody b req = req { reqBody = b }
49reqToRequest :: (Functor m, MonadThrow m) => Req -> URI -> m Request
50reqToRequest req uri = fmap (setrqb . setQS ) $ parseUrl url
52 where url = show $ nullURI { uriPath = reqPath req }
55 setrqb r = r { requestBody = RequestBodyLBS (reqBody req) }
56 setQS = setQueryString $ queryTextToQuery (qs req)
58{-# NOINLINE __manager #-}
59__manager :: MVar Manager
60__manager = unsafePerformIO (newManager defaultManagerSettings >>= newMVar)
62__withGlobalManager :: (Manager -> IO a) -> IO a
63__withGlobalManager action = modifyMVar __manager $ \ manager -> do
64 result <- action manager
65 return (manager, result)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list