-----------------------------------------------------------------------------
-- |
-- License     :  BSD-3-Clause
-- Maintainer  :  Todd Mohney <toddmohney@gmail.com>
--
-- The public keys API, as described at
-- <https://developer.github.com/v3/users/keys/>
module GitHub.Endpoints.Users.PublicSSHKeys (
    -- * Querying public SSH keys
    publicSSHKeys',
    publicSSHKeysR,
    publicSSHKeysFor',
    publicSSHKeysForR,
    publicSSHKey',
    publicSSHKeyR,

    -- ** Create
    createUserPublicSSHKey',
    createUserPublicSSHKeyR,

    -- ** Delete
    deleteUserPublicSSHKey',
    deleteUserPublicSSHKeyR,
) where

import GitHub.Data
import GitHub.Internal.Prelude
import GitHub.Request
import Prelude ()

-- | Querying public SSH keys.
publicSSHKeysFor' :: Name Owner -> IO (Either Error (Vector PublicSSHKeyBasic))
publicSSHKeysFor' :: Name Owner -> IO (Either Error (Vector PublicSSHKeyBasic))
publicSSHKeysFor' user :: Name Owner
user =
    GenRequest 'MtJSON 'RO (Vector PublicSSHKeyBasic)
-> IO (Either Error (Vector PublicSSHKeyBasic))
forall (mt :: MediaType *) a.
ParseResponse mt a =>
GenRequest mt 'RO a -> IO (Either Error a)
executeRequest' (GenRequest 'MtJSON 'RO (Vector PublicSSHKeyBasic)
 -> IO (Either Error (Vector PublicSSHKeyBasic)))
-> GenRequest 'MtJSON 'RO (Vector PublicSSHKeyBasic)
-> IO (Either Error (Vector PublicSSHKeyBasic))
forall a b. (a -> b) -> a -> b
$ Name Owner
-> FetchCount -> GenRequest 'MtJSON 'RO (Vector PublicSSHKeyBasic)
publicSSHKeysForR Name Owner
user FetchCount
FetchAll

-- | Querying public SSH keys.
-- See <https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user>
publicSSHKeysForR :: Name Owner -> FetchCount -> Request 'RO (Vector PublicSSHKeyBasic)
publicSSHKeysForR :: Name Owner
-> FetchCount -> GenRequest 'MtJSON 'RO (Vector PublicSSHKeyBasic)
publicSSHKeysForR user :: Name Owner
user =
    Paths
-> QueryString
-> FetchCount
-> GenRequest 'MtJSON 'RO (Vector PublicSSHKeyBasic)
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery ["users", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, "keys"] []

-- | Querying the authenticated users' public SSH keys
publicSSHKeys' :: Auth -> IO (Either Error (Vector PublicSSHKey))
publicSSHKeys' :: Auth -> IO (Either Error (Vector PublicSSHKey))
publicSSHKeys' auth :: Auth
auth =
    Auth
-> GenRequest 'MtJSON 'RA (Vector PublicSSHKey)
-> IO (Either Error (Vector PublicSSHKey))
forall am (mt :: MediaType *) a (rw :: RW).
(AuthMethod am, ParseResponse mt a) =>
am -> GenRequest mt rw a -> IO (Either Error a)
executeRequest Auth
auth GenRequest 'MtJSON 'RA (Vector PublicSSHKey)
publicSSHKeysR

-- | Querying the authenticated users' public SSH keys
-- See <https://developer.github.com/v3/users/keys/#list-your-public-keys>
publicSSHKeysR :: Request 'RA (Vector PublicSSHKey)
publicSSHKeysR :: GenRequest 'MtJSON 'RA (Vector PublicSSHKey)
publicSSHKeysR =
    Paths
-> QueryString -> GenRequest 'MtJSON 'RA (Vector PublicSSHKey)
forall (mt :: RW) a. Paths -> QueryString -> Request mt a
query ["user", "keys"] []

-- | Querying a public SSH key
publicSSHKey' :: Auth -> Id PublicSSHKey -> IO (Either Error PublicSSHKey)
publicSSHKey' :: Auth -> Id PublicSSHKey -> IO (Either Error PublicSSHKey)
publicSSHKey' auth :: Auth
auth keyId :: Id PublicSSHKey
keyId =
    Auth
-> GenRequest 'MtJSON 'RA PublicSSHKey
-> IO (Either Error PublicSSHKey)
forall am (mt :: MediaType *) a (rw :: RW).
(AuthMethod am, ParseResponse mt a) =>
am -> GenRequest mt rw a -> IO (Either Error a)
executeRequest Auth
auth (GenRequest 'MtJSON 'RA PublicSSHKey
 -> IO (Either Error PublicSSHKey))
-> GenRequest 'MtJSON 'RA PublicSSHKey
-> IO (Either Error PublicSSHKey)
forall a b. (a -> b) -> a -> b
$ Id PublicSSHKey -> GenRequest 'MtJSON 'RA PublicSSHKey
publicSSHKeyR Id PublicSSHKey
keyId

-- | Querying a public SSH key.
-- See <https://developer.github.com/v3/users/keys/#get-a-single-public-key>
publicSSHKeyR :: Id PublicSSHKey -> Request 'RA PublicSSHKey
publicSSHKeyR :: Id PublicSSHKey -> GenRequest 'MtJSON 'RA PublicSSHKey
publicSSHKeyR keyId :: Id PublicSSHKey
keyId =
    Paths -> QueryString -> GenRequest 'MtJSON 'RA PublicSSHKey
forall (mt :: RW) a. Paths -> QueryString -> Request mt a
query ["user", "keys", Id PublicSSHKey -> Text
forall a. IsPathPart a => a -> Text
toPathPart Id PublicSSHKey
keyId] []

-- | Create a public SSH key
createUserPublicSSHKey' :: Auth -> NewPublicSSHKey -> IO (Either Error PublicSSHKey)
createUserPublicSSHKey' :: Auth -> NewPublicSSHKey -> IO (Either Error PublicSSHKey)
createUserPublicSSHKey' auth :: Auth
auth key :: NewPublicSSHKey
key =
    Auth
-> GenRequest 'MtJSON 'RW PublicSSHKey
-> IO (Either Error PublicSSHKey)
forall am (mt :: MediaType *) a (rw :: RW).
(AuthMethod am, ParseResponse mt a) =>
am -> GenRequest mt rw a -> IO (Either Error a)
executeRequest Auth
auth (GenRequest 'MtJSON 'RW PublicSSHKey
 -> IO (Either Error PublicSSHKey))
-> GenRequest 'MtJSON 'RW PublicSSHKey
-> IO (Either Error PublicSSHKey)
forall a b. (a -> b) -> a -> b
$ NewPublicSSHKey -> GenRequest 'MtJSON 'RW PublicSSHKey
createUserPublicSSHKeyR NewPublicSSHKey
key

-- | Create a public SSH key.
-- See <https://developer.github.com/v3/users/keys/#create-a-public-key>.
createUserPublicSSHKeyR :: NewPublicSSHKey -> Request 'RW PublicSSHKey
createUserPublicSSHKeyR :: NewPublicSSHKey -> GenRequest 'MtJSON 'RW PublicSSHKey
createUserPublicSSHKeyR key :: NewPublicSSHKey
key =
    CommandMethod
-> Paths -> ByteString -> GenRequest 'MtJSON 'RW PublicSSHKey
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Post ["user", "keys"] (NewPublicSSHKey -> ByteString
forall a. ToJSON a => a -> ByteString
encode NewPublicSSHKey
key)

deleteUserPublicSSHKey' :: Auth -> Id PublicSSHKey -> IO (Either Error ())
deleteUserPublicSSHKey' :: Auth -> Id PublicSSHKey -> IO (Either Error ())
deleteUserPublicSSHKey' auth :: Auth
auth keyId :: Id PublicSSHKey
keyId =
    Auth -> GenRequest 'MtUnit 'RW () -> IO (Either Error ())
forall am (mt :: MediaType *) a (rw :: RW).
(AuthMethod am, ParseResponse mt a) =>
am -> GenRequest mt rw a -> IO (Either Error a)
executeRequest Auth
auth (GenRequest 'MtUnit 'RW () -> IO (Either Error ()))
-> GenRequest 'MtUnit 'RW () -> IO (Either Error ())
forall a b. (a -> b) -> a -> b
$ Id PublicSSHKey -> GenRequest 'MtUnit 'RW ()
deleteUserPublicSSHKeyR Id PublicSSHKey
keyId

-- | Delete a public SSH key.
-- See <https://developer.github.com/v3/users/keys/#delete-a-public-key>
deleteUserPublicSSHKeyR :: Id PublicSSHKey -> GenRequest 'MtUnit 'RW ()
deleteUserPublicSSHKeyR :: Id PublicSSHKey -> GenRequest 'MtUnit 'RW ()
deleteUserPublicSSHKeyR keyId :: Id PublicSSHKey
keyId =
    CommandMethod -> Paths -> ByteString -> GenRequest 'MtUnit 'RW ()
forall (mt :: MediaType *) a.
CommandMethod -> Paths -> ByteString -> GenRequest mt 'RW a
Command CommandMethod
Delete ["user", "keys", Id PublicSSHKey -> Text
forall a. IsPathPart a => a -> Text
toPathPart Id PublicSSHKey
keyId] ByteString
forall a. Monoid a => a
mempty