{-# 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 :: forall (m :: * -> *) a.
(HasGoProAuth m, MonadIO m, FromJSON a) =>
String -> m a
jgetAuth String
u = forall (m :: * -> *). HasGoProAuth m => m AuthInfo
goproAuth 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
..} -> 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 :: forall (m :: * -> *). HasGoProAuth m => m Options
authOptsM = Text -> Options
authOpts forall b c a. (b -> c) -> (a -> b) -> a -> c
. AuthInfo -> Text
_access_token forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasGoProAuth m => m AuthInfo
goproAuth

jgetWithAuth :: (HasGoProAuth m, MonadIO m, FromJSON a) => Network.Wreq.Options -> String -> m a
jgetWithAuth :: forall (m :: * -> *) a.
(HasGoProAuth m, MonadIO m, FromJSON a) =>
Options -> String -> m a
jgetWithAuth Options
opts String
u = forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
responseBody forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Options -> String -> IO (Response ByteString)
getWith Options
opts String
u forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= 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 :: forall (m :: * -> *) j a.
(HasGoProAuth m, MonadIO m, FromJSON j, Putable a) =>
String -> a -> m j
jputAuth String
u a
a = forall (m :: * -> *). HasGoProAuth m => m Options
authOptsM forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Options
o -> forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
responseBody forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a.
Putable a =>
Options -> String -> a -> IO (Response ByteString)
putWith Options
o String
u a
a forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= 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 :: forall (m :: * -> *).
(HasGoProAuth m, MonadIO m) =>
String -> m ByteString
proxyAuth String
u = (AuthInfo -> Text
_access_token forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasGoProAuth m => m AuthInfo
goproAuth) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
tok -> 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 :: forall (m :: * -> *) a r.
(HasGoProAuth m, MonadIO m, Postable a, FromJSON r) =>
String -> a -> m r
jpostAuth String
u a
v = forall (m :: * -> *). HasGoProAuth m => m Options
authOptsM forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Options
opts -> forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
responseBody forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a.
Postable a =>
Options -> String -> a -> IO (Response ByteString)
postWith Options
opts String
u a
v forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a.
(MonadThrow m, FromJSON a) =>
Response ByteString -> m (Response a)
asJSON)