-- | Blockfrost client version via user agent header

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}

module Blockfrost.Client.UserAgent () where

import Blockfrost.Util.UserAgent
import Data.CaseInsensitive (mk)
import Data.Proxy (Proxy (..))
import Data.Sequence ((<|))
import Servant.API ((:>))
import Servant.Client.Core (Client, HasClient (..), requestHeaders)

instance {-# OVERLAPS #-} (HasClient m api) => HasClient m (UserAgent :> api) where
  type Client m (UserAgent :> api) = Client m api

  clientWithRoute :: Proxy m
-> Proxy (UserAgent :> api)
-> Request
-> Client m (UserAgent :> api)
clientWithRoute Proxy m
m Proxy (UserAgent :> api)
_ Request
req
    = Proxy m -> Proxy api -> Request -> Client m api
forall (m :: * -> *) api.
HasClient m api =>
Proxy m -> Proxy api -> Request -> Client m api
clientWithRoute Proxy m
m (Proxy api
forall {k} (t :: k). Proxy t
Proxy :: Proxy api)
    (Request -> Client m api) -> Request -> Client m api
forall a b. (a -> b) -> a -> b
$ Request
req { requestHeaders = (mk "User-Agent", userAgent) <| requestHeaders req  }

  hoistClientMonad :: forall (mon :: * -> *) (mon' :: * -> *).
Proxy m
-> Proxy (UserAgent :> api)
-> (forall x. mon x -> mon' x)
-> Client mon (UserAgent :> api)
-> Client mon' (UserAgent :> api)
hoistClientMonad Proxy m
pm Proxy (UserAgent :> api)
_ forall x. mon x -> mon' x
nt = Proxy m
-> Proxy api
-> (forall x. mon x -> mon' x)
-> Client mon api
-> Client mon' api
forall (m :: * -> *) api (mon :: * -> *) (mon' :: * -> *).
HasClient m api =>
Proxy m
-> Proxy api
-> (forall x. mon x -> mon' x)
-> Client mon api
-> Client mon' api
forall (mon :: * -> *) (mon' :: * -> *).
Proxy m
-> Proxy api
-> (forall x. mon x -> mon' x)
-> Client mon api
-> Client mon' api
hoistClientMonad Proxy m
pm (Proxy api
forall {k} (t :: k). Proxy t
Proxy :: Proxy api) mon x -> mon' x
forall x. mon x -> mon' x
nt