{-# OPTIONS_HADDOCK hide, prune, ignore-exports #-}

module GoPro.Plus.Internal.AuthHTTP where

import           Control.Lens
import           Control.Monad.IO.Class   (MonadIO (..))
import           Data.Aeson               (FromJSON (..))
import qualified Data.ByteString.Lazy     as BL
import           Network.Wreq             (Options, asJSON, getWith, postWith,
                                           putWith, responseBody)
import           Network.Wreq.Types       (Postable, Putable)

import           GoPro.Plus.Auth
import           GoPro.Plus.Internal.HTTP


jgetAuth :: (HasGoProAuth m, MonadIO m, FromJSON a) => String -> m a
jgetAuth :: String -> m a
jgetAuth String
u = m AuthInfo
forall (m :: * -> *). HasGoProAuth m => m AuthInfo
goproAuth m AuthInfo -> (AuthInfo -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \AuthInfo{Int
Text
_resource_owner_id :: AuthInfo -> Text
_refresh_token :: AuthInfo -> Text
_expires_in :: AuthInfo -> Int
_access_token :: AuthInfo -> Text
_resource_owner_id :: Text
_refresh_token :: Text
_expires_in :: Int
_access_token :: Text
..} -> Options -> String -> m a
forall (m :: * -> *) a.
(MonadIO m, FromJSON a) =>
Options -> String -> m a
jgetWith (Text -> Options
authOpts Text
_access_token) String
u


authOptsM :: HasGoProAuth m => m Network.Wreq.Options
authOptsM :: m Options
authOptsM = Text -> Options
authOpts (Text -> Options) -> (AuthInfo -> Text) -> AuthInfo -> Options
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AuthInfo -> Text
_access_token (AuthInfo -> Options) -> m AuthInfo -> m Options
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m AuthInfo
forall (m :: * -> *). HasGoProAuth m => m AuthInfo
goproAuth

jgetWithAuth :: (HasGoProAuth m, MonadIO m, FromJSON a) => Network.Wreq.Options -> String -> m a
jgetWithAuth :: Options -> String -> m a
jgetWithAuth Options
opts String
u = Getting a (Response a) a -> Response a -> a
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting a (Response a) a
forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
responseBody (Response a -> a) -> m (Response a) -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Response a) -> m (Response a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Options -> String -> IO (Response ByteString)
getWith Options
opts String
u IO (Response ByteString)
-> (Response ByteString -> IO (Response a)) -> IO (Response a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Response ByteString -> IO (Response a)
forall (m :: * -> *) a.
(MonadThrow m, FromJSON a) =>
Response ByteString -> m (Response a)
asJSON)

jputAuth :: (HasGoProAuth m, MonadIO m, FromJSON j, Putable a) => String -> a -> m j
jputAuth :: String -> a -> m j
jputAuth String
u a
a = m Options
forall (m :: * -> *). HasGoProAuth m => m Options
authOptsM m Options -> (Options -> m j) -> m j
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Options
o -> Getting j (Response j) j -> Response j -> j
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting j (Response j) j
forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
responseBody (Response j -> j) -> m (Response j) -> m j
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Response j) -> m (Response j)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Options -> String -> a -> IO (Response ByteString)
forall a.
Putable a =>
Options -> String -> a -> IO (Response ByteString)
putWith Options
o String
u a
a IO (Response ByteString)
-> (Response ByteString -> IO (Response j)) -> IO (Response j)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Response ByteString -> IO (Response j)
forall (m :: * -> *) a.
(MonadThrow m, FromJSON a) =>
Response ByteString -> m (Response a)
asJSON)


-- | Proxy a request to GoPro with authentication.
proxyAuth :: (HasGoProAuth m, MonadIO m) => String -> m BL.ByteString
proxyAuth :: String -> m ByteString
proxyAuth String
u = (AuthInfo -> Text
_access_token (AuthInfo -> Text) -> m AuthInfo -> m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m AuthInfo
forall (m :: * -> *). HasGoProAuth m => m AuthInfo
goproAuth) m Text -> (Text -> m ByteString) -> m ByteString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
tok -> Text -> String -> m ByteString
forall (m :: * -> *). MonadIO m => Text -> String -> m ByteString
proxy Text
tok String
u

jpostAuth :: (HasGoProAuth m, MonadIO m, Postable a, FromJSON r) => String -> a -> m r
jpostAuth :: String -> a -> m r
jpostAuth String
u a
v = m Options
forall (m :: * -> *). HasGoProAuth m => m Options
authOptsM m Options -> (Options -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Options
opts -> Getting r (Response r) r -> Response r -> r
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting r (Response r) r
forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
responseBody (Response r -> r) -> m (Response r) -> m r
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Response r) -> m (Response r)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Options -> String -> a -> IO (Response ByteString)
forall a.
Postable a =>
Options -> String -> a -> IO (Response ByteString)
postWith Options
opts String
u a
v IO (Response ByteString)
-> (Response ByteString -> IO (Response r)) -> IO (Response r)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Response ByteString -> IO (Response r)
forall (m :: * -> *) a.
(MonadThrow m, FromJSON a) =>
Response ByteString -> m (Response a)
asJSON)