2015-01-21 08:47:23 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2016-03-13 22:21:36 +01:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2015-01-21 08:47:23 +01:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE KindSignatures #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-10-02 14:38:19 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-11-29 05:53:50 +01:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2015-01-21 08:47:23 +01:00
|
|
|
|
2015-07-22 12:55:44 +02:00
|
|
|
module Servant.JSSpec.CustomHeaders where
|
2015-01-21 08:47:23 +01:00
|
|
|
|
2015-08-17 23:56:29 +02:00
|
|
|
import Control.Lens
|
|
|
|
import Data.Monoid
|
|
|
|
import Data.Proxy
|
2015-10-02 14:38:19 +02:00
|
|
|
import Data.Text (pack)
|
2015-08-17 23:56:29 +02:00
|
|
|
import GHC.TypeLits
|
2016-07-08 09:11:34 +02:00
|
|
|
import Servant.API.ContentTypes
|
2015-08-17 23:56:29 +02:00
|
|
|
import Servant.JS.Internal
|
2015-01-21 08:47:23 +01:00
|
|
|
|
|
|
|
-- | This is a hypothetical combinator that fetches an Authorization header.
|
|
|
|
-- The symbol in the header denotes what kind of authentication we are
|
|
|
|
-- using -- Basic, Digest, whatever.
|
|
|
|
data Authorization (sym :: Symbol) a
|
|
|
|
|
2016-07-08 09:11:34 +02:00
|
|
|
instance (KnownSymbol sym, HasForeign lang NoContent api)
|
|
|
|
=> HasForeign lang NoContent (Authorization sym a :> api) where
|
|
|
|
type Foreign NoContent (Authorization sym a :> api) = Foreign NoContent api
|
2015-01-21 08:47:23 +01:00
|
|
|
|
2016-06-02 09:49:55 +02:00
|
|
|
foreignFor lang ftype Proxy req = foreignFor lang ftype (Proxy :: Proxy api) $
|
2016-02-17 22:47:30 +01:00
|
|
|
req & reqHeaders <>~
|
2016-07-08 09:11:34 +02:00
|
|
|
[ ReplaceHeaderArg (Arg "Authorization" NoContent)
|
2016-02-17 22:47:30 +01:00
|
|
|
$ tokenType (pack . symbolVal $ (Proxy :: Proxy sym)) ]
|
2015-01-21 08:47:23 +01:00
|
|
|
where
|
2015-01-21 09:32:06 +01:00
|
|
|
tokenType t = t <> " {Authorization}"
|
|
|
|
|
|
|
|
-- | This is a combinator that fetches an X-MyLovelyHorse header.
|
|
|
|
data MyLovelyHorse a
|
|
|
|
|
2016-07-08 09:11:34 +02:00
|
|
|
instance (HasForeign lang NoContent api)
|
|
|
|
=> HasForeign lang NoContent (MyLovelyHorse a :> api) where
|
|
|
|
type Foreign NoContent (MyLovelyHorse a :> api) = Foreign NoContent api
|
2015-01-21 09:32:06 +01:00
|
|
|
|
2016-06-02 09:49:55 +02:00
|
|
|
foreignFor lang ftype Proxy req = foreignFor lang ftype (Proxy :: Proxy api) $
|
2016-07-08 09:11:34 +02:00
|
|
|
req & reqHeaders <>~ [ ReplaceHeaderArg (Arg "X-MyLovelyHorse" NoContent) tpl ]
|
2015-01-21 09:32:06 +01:00
|
|
|
where
|
|
|
|
tpl = "I am good friends with {X-MyLovelyHorse}"
|
2015-01-22 01:41:06 +01:00
|
|
|
|
|
|
|
-- | This is a combinator that fetches an X-WhatsForDinner header.
|
|
|
|
data WhatsForDinner a
|
|
|
|
|
2016-07-08 09:11:34 +02:00
|
|
|
instance (HasForeign lang NoContent api)
|
|
|
|
=> HasForeign lang NoContent (WhatsForDinner a :> api) where
|
|
|
|
type Foreign NoContent (WhatsForDinner a :> api) = Foreign NoContent api
|
2015-01-22 01:41:06 +01:00
|
|
|
|
2016-06-02 09:49:55 +02:00
|
|
|
foreignFor lang ftype Proxy req = foreignFor lang ftype (Proxy :: Proxy api) $
|
2016-07-08 09:11:34 +02:00
|
|
|
req & reqHeaders <>~ [ ReplaceHeaderArg (Arg "X-WhatsForDinner" NoContent) tpl ]
|
2015-01-22 01:41:06 +01:00
|
|
|
where
|
|
|
|
tpl = "I would like {X-WhatsForDinner} with a cherry on top."
|