Last active
April 5, 2019 05:48
-
-
Save jhrcek/8652c059c1d2925957b4805e923a36b9 to your computer and use it in GitHub Desktop.
servant-client basic auth example, calling httpbin.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE TypeOperators #-} | |
module Main where | |
import Data.Aeson (FromJSON, ToJSON) | |
import Data.Proxy (Proxy (Proxy)) | |
import Data.Text (Text) | |
import GHC.Generics | |
import Network.HTTP.Client (defaultManagerSettings, newManager) | |
import Servant.API | |
import Servant.Client (BaseUrl (BaseUrl), ClientM, Scheme (Http), client, | |
mkClientEnv, runClientM) | |
main :: IO () | |
main = do | |
mgr <- newManager defaultManagerSettings | |
runClientM (basicAuthClient u) (mkClientEnv mgr (BaseUrl Http "httpbin.org" 80 "basic-auth")) | |
where u = BasicAuthData "john" "123" | |
type API = BasicAuth "Realm doesnt matter" () :> "john" :> "123" :> Get '[JSON] User | |
api :: Proxy API | |
api = Proxy | |
data User = User | |
{ authenticated :: Bool | |
, user :: Text | |
} deriving (Show, Eq, Generic) | |
instance FromJSON User where | |
instance ToJSON User where | |
basicAuthClient :: BasicAuthData -> ClientM User | |
basicAuthClient = client api |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment