1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE DeriveGeneric #-}
4module Servant.Client where
6import Control.Concurrent
7import Control.Monad.Catch
8import Data.ByteString.Lazy
12import Network.HTTP.Client hiding (Proxy)
13import Network.HTTP.Types
15import System.IO.Unsafe
17-- * Accessing APIs as a Client
19data Scheme = Http | Https
20 deriving (Show, Eq, Ord, Generic)
22data BaseUrl = BaseUrl {
23 baseUrlScheme :: Scheme,
24 baseUrlHost :: String,
27 deriving (Show, Eq, Ord, Generic)
29httpBaseUrl :: String -> BaseUrl
30httpBaseUrl host = BaseUrl Http host 80
32-- | 'client' allows you to produce operations to query an API from a client.
33client :: HasClient layout => Proxy layout -> Client layout
34client p = clientWithRoute p defReq
36class HasClient layout where
37 type Client layout :: *
38 clientWithRoute :: Proxy layout -> Req -> Client layout
43 , reqBody :: ByteString
49appendToPath :: String -> Req -> Req
51 req { reqPath = reqPath req ++ "/" ++ p }
53appendToQueryString :: Text -- ^ param name
54 -> Maybe Text -- ^ param value
57appendToQueryString pname pvalue req =
58 req { qs = qs req ++ [(pname, pvalue)]
61setRQBody :: ByteString -> Req -> Req
62setRQBody b req = req { reqBody = b }
64reqToRequest :: (Functor m, MonadThrow m) => Req -> BaseUrl -> m Request
65reqToRequest req (BaseUrl scheme host port) = fmap (setrqb . setQS ) $ parseUrl url
67 where url = show $ nullURI { uriScheme = case scheme of
70 , uriAuthority = Just $
71 URIAuth { uriUserInfo = ""
73 , uriPort = ":" ++ show port
75 , uriPath = reqPath req
78 setrqb r = r { requestBody = RequestBodyLBS (reqBody req) }
79 setQS = setQueryString $ queryTextToQuery (qs req)
81{-# NOINLINE __manager #-}
82__manager :: MVar Manager
83__manager = unsafePerformIO (newManager defaultManagerSettings >>= newMVar)
85__withGlobalManager :: (Manager -> IO a) -> IO a
86__withGlobalManager action = modifyMVar __manager $ \ manager -> do
87 result <- action manager
88 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