-----------------------------------------------------------------------------
-- |
-- License     :  BSD-3-Clause
-- Maintainer  :  Oleg Grenrus <oleg.grenrus@iki.fi>
--
-- The Github Repo Contents API, as documented at
-- <https://developer.github.com/v3/repos/contents/>
module GitHub.Endpoints.Repos.Contents (
    -- * Querying contents
    contentsForR,
    readmeForR,
    archiveForR,

    -- ** Create
    createFileR,

    -- ** Update
    updateFileR,

    -- ** Delete
    deleteFileR,

    module GitHub.Data
    ) where

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

import Data.Maybe (maybeToList)
import qualified Data.Text.Encoding as TE
import Network.URI (URI)

contentsForR
    :: Name Owner
    -> Name Repo
    -> Text            -- ^ file or directory
    -> Maybe Text      -- ^ Git commit
    -> Request k Content
contentsForR :: forall (k :: RW).
Name Owner -> Name Repo -> Text -> Maybe Text -> Request k Content
contentsForR Name Owner
user Name Repo
repo Text
path Maybe Text
ref =
    Paths -> QueryString -> Request k Content
forall (mt :: RW) a. Paths -> QueryString -> Request mt a
query [Text
"repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"contents", Text
path] QueryString
qs
  where
    qs :: QueryString
qs =  QueryString -> (Text -> QueryString) -> Maybe Text -> QueryString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Text
r -> [(ByteString
"ref", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> (Text -> ByteString) -> Text -> Maybe ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf8 (Text -> Maybe ByteString) -> Text -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Text
r)]) Maybe Text
ref

readmeForR :: Name Owner -> Name Repo -> Request k Content
readmeForR :: forall (k :: RW). Name Owner -> Name Repo -> Request k Content
readmeForR Name Owner
user Name Repo
repo =
    Paths -> QueryString -> Request k Content
forall (mt :: RW) a. Paths -> QueryString -> Request mt a
query [Text
"repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"readme"] []

-- | Get archive link.
-- See <https://developer.github.com/v3/repos/contents/#get-archive-link>
archiveForR
    :: Name Owner
    -> Name Repo
    -> ArchiveFormat   -- ^ The type of archive to retrieve
    -> Maybe Text      -- ^ Git commit
    -> GenRequest 'MtRedirect rw URI
archiveForR :: forall (rw :: RW).
Name Owner
-> Name Repo
-> ArchiveFormat
-> Maybe Text
-> GenRequest 'MtRedirect rw URI
archiveForR Name Owner
user Name Repo
repo ArchiveFormat
format Maybe Text
ref = Paths -> QueryString -> GenRequest 'MtRedirect rw URI
forall (mt :: MediaType (*)) (rw :: RW) a.
Paths -> QueryString -> GenRequest mt rw a
Query Paths
path []
  where
    path :: Paths
path = [Text
"repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, ArchiveFormat -> Text
forall a. IsPathPart a => a -> Text
toPathPart ArchiveFormat
format] Paths -> Paths -> Paths
forall a. Semigroup a => a -> a -> a
<> Maybe Text -> Paths
forall a. Maybe a -> [a]
maybeToList Maybe Text
ref

-- | Create a file.
-- See <https://developer.github.com/v3/repos/contents/#create-a-file>
createFileR
    :: Name Owner
    -> Name Repo
    -> CreateFile
    -> Request 'RW ContentResult
createFileR :: Name Owner -> Name Repo -> CreateFile -> Request 'RW ContentResult
createFileR Name Owner
user Name Repo
repo CreateFile
body =
    CommandMethod -> Paths -> ByteString -> Request 'RW ContentResult
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Put [Text
"repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"contents", CreateFile -> Text
createFilePath CreateFile
body] (CreateFile -> ByteString
forall a. ToJSON a => a -> ByteString
encode CreateFile
body)

-- | Update a file.
-- See <https://developer.github.com/v3/repos/contents/#update-a-file>
updateFileR
    :: Name Owner
    -> Name Repo
    -> UpdateFile
    -> Request 'RW ContentResult
updateFileR :: Name Owner -> Name Repo -> UpdateFile -> Request 'RW ContentResult
updateFileR Name Owner
user Name Repo
repo UpdateFile
body =
    CommandMethod -> Paths -> ByteString -> Request 'RW ContentResult
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Put [Text
"repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"contents", UpdateFile -> Text
updateFilePath UpdateFile
body] (UpdateFile -> ByteString
forall a. ToJSON a => a -> ByteString
encode UpdateFile
body)

-- | Delete a file.
-- See <https://developer.github.com/v3/repos/contents/#delete-a-file>
deleteFileR
    :: Name Owner
    -> Name Repo
    -> DeleteFile
    -> GenRequest 'MtUnit 'RW ()
deleteFileR :: Name Owner -> Name Repo -> DeleteFile -> GenRequest 'MtUnit 'RW ()
deleteFileR Name Owner
user Name Repo
repo DeleteFile
body =
    CommandMethod -> Paths -> ByteString -> GenRequest 'MtUnit 'RW ()
forall (mt :: MediaType (*)) a.
CommandMethod -> Paths -> ByteString -> GenRequest mt 'RW a
Command CommandMethod
Delete [Text
"repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"contents", DeleteFile -> Text
deleteFilePath DeleteFile
body] (DeleteFile -> ByteString
forall a. ToJSON a => a -> ByteString
encode DeleteFile
body)