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-- | Convenience function for creating 'URIAuth's.
18mkHost :: String -> Int -> URIAuth
19mkHost hostName port = URIAuth "" hostName (":" ++ show port)
21-- | 'client' allows you to produce operations to query an API from a client.
22client :: HasClient layout => Proxy layout -> Client layout
23client p = clientWithRoute p defReq
25class HasClient layout where
26 type Client layout :: *
27 clientWithRoute :: Proxy layout -> Req -> Client layout
32 , reqBody :: ByteString
38appendToPath :: String -> Req -> Req
40 req { reqPath = reqPath req ++ "/" ++ p }
42appendToQueryString :: Text -- ^ param name
43 -> Maybe Text -- ^ param value
46appendToQueryString pname pvalue req =
47 req { qs = qs req ++ [(pname, pvalue)]
50setRQBody :: ByteString -> Req -> Req
51setRQBody b req = req { reqBody = b }
53reqToRequest :: (Functor m, MonadThrow m) => Req -> URIAuth -> m Request
54reqToRequest req host = fmap (setrqb . setQS ) $ parseUrl url
56 where url = show $ nullURI { uriScheme = "http:"
57 , uriAuthority = Just host
58 , uriPath = reqPath req
61 setrqb r = r { requestBody = RequestBodyLBS (reqBody req) }
62 setQS = setQueryString $ queryTextToQuery (qs req)
64{-# NOINLINE __manager #-}
65__manager :: MVar Manager
66__manager = unsafePerformIO (newManager defaultManagerSettings >>= newMVar)
68__withGlobalManager :: (Manager -> IO a) -> IO a
69__withGlobalManager action = modifyMVar __manager $ \ manager -> do
70 result <- action manager
71 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