From c7960f4eea56365bf10239a61ebc45b7ce24cc9d Mon Sep 17 00:00:00 2001 From: MATSUBARA Nobutada Date: Thu, 31 Dec 2020 01:20:48 +0900 Subject: [PATCH 1/3] Add endpoint to create gist --- src/GitHub.hs | 2 +- src/GitHub/Data/Gists.hs | 29 +++++++++++++++++++++++++++++ src/GitHub/Endpoints/Gists.hs | 6 ++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/GitHub.hs b/src/GitHub.hs index 6b5f8d36..e00a53cb 100644 --- a/src/GitHub.hs +++ b/src/GitHub.hs @@ -62,7 +62,6 @@ module GitHub ( -- Missing endpoints: -- -- * Query a specific revision of a gist - -- * Create a gist -- * Edit a gist -- * List gist commits -- * Check if a gist is starred @@ -70,6 +69,7 @@ module GitHub ( -- * List gist forks gistsR, gistR, + createGistR, starGistR, unstarGistR, deleteGistR, diff --git a/src/GitHub/Data/Gists.hs b/src/GitHub/Data/Gists.hs index 3e1fbe79..04c98bae 100644 --- a/src/GitHub/Data/Gists.hs +++ b/src/GitHub/Data/Gists.hs @@ -89,3 +89,32 @@ instance FromJSON GistComment where <*> o .: "body" <*> o .: "updated_at" <*> o .: "id" + +data NewGist = NewGist + { newGistDescription :: !Text + , newGistFiles :: !(HashMap Text NewGistFile) + , newGistPublic :: !Bool + } deriving (Show, Data, Typeable, Eq, Generic) + +instance NFData NewGist where rnf = genericRnf +instance Binary NewGist + +instance ToJSON NewGist where + toJSON (NewGist { newGistDescription = description + , newGistFiles = files + , newGistPublic = public + }) = object + [ "description" .= description + , "files" .= files + , "public" .= public + ] + +data NewGistFile = NewGistFile + { newGistFileContent :: !Text + } deriving (Show, Data, Typeable, Eq, Generic) + +instance NFData NewGistFile where rnf = genericRnf +instance Binary NewGistFile + +instance ToJSON NewGistFile where + toJSON (NewGistFile c) = object ["content" .= c] diff --git a/src/GitHub/Endpoints/Gists.hs b/src/GitHub/Endpoints/Gists.hs index 783e0588..de8e6c20 100644 --- a/src/GitHub/Endpoints/Gists.hs +++ b/src/GitHub/Endpoints/Gists.hs @@ -7,6 +7,7 @@ module GitHub.Endpoints.Gists ( gistsR, gistR, + createGistR, starGistR, unstarGistR, deleteGistR, @@ -28,6 +29,11 @@ gistR :: Name Gist -> Request k Gist gistR gid = query ["gists", toPathPart gid] [] +-- | Create a new gist +-- See +createGistR :: NewGist -> Request 'RW Gist +createGistR ngist = command Post ["gists"] (encode ngist) + -- | Star a gist by the authenticated user. -- See starGistR :: Name Gist -> GenRequest 'MtUnit 'RW () From 91424a994ab2a3c353e5356e065c341c3c638b83 Mon Sep 17 00:00:00 2001 From: MATSUBARA Nobutada Date: Fri, 1 Jan 2021 12:30:19 +0900 Subject: [PATCH 2/3] Refactor: remove brackets that are redundant --- src/GitHub/Data/Gists.hs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/GitHub/Data/Gists.hs b/src/GitHub/Data/Gists.hs index 04c98bae..b9402943 100644 --- a/src/GitHub/Data/Gists.hs +++ b/src/GitHub/Data/Gists.hs @@ -100,14 +100,14 @@ instance NFData NewGist where rnf = genericRnf instance Binary NewGist instance ToJSON NewGist where - toJSON (NewGist { newGistDescription = description - , newGistFiles = files - , newGistPublic = public - }) = object - [ "description" .= description - , "files" .= files - , "public" .= public - ] + toJSON NewGist { newGistDescription = description + , newGistFiles = files + , newGistPublic = public + } = object + [ "description" .= description + , "files" .= files + , "public" .= public + ] data NewGistFile = NewGistFile { newGistFileContent :: !Text From c44824bb6c41aad7fc9ac59e4686e138c9d67066 Mon Sep 17 00:00:00 2001 From: MATSUBARA Nobutada Date: Fri, 1 Jan 2021 15:10:37 +0900 Subject: [PATCH 3/3] Fix: use Maybe a for optional params with filter notNull --- src/GitHub/Data/Gists.hs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/GitHub/Data/Gists.hs b/src/GitHub/Data/Gists.hs index b9402943..b6d1b673 100644 --- a/src/GitHub/Data/Gists.hs +++ b/src/GitHub/Data/Gists.hs @@ -91,9 +91,9 @@ instance FromJSON GistComment where <*> o .: "id" data NewGist = NewGist - { newGistDescription :: !Text + { newGistDescription :: !(Maybe Text) , newGistFiles :: !(HashMap Text NewGistFile) - , newGistPublic :: !Bool + , newGistPublic :: !(Maybe Bool) } deriving (Show, Data, Typeable, Eq, Generic) instance NFData NewGist where rnf = genericRnf @@ -103,11 +103,14 @@ instance ToJSON NewGist where toJSON NewGist { newGistDescription = description , newGistFiles = files , newGistPublic = public - } = object + } = object $ filter notNull [ "description" .= description , "files" .= files , "public" .= public ] + where + notNull (_, Null) = False + notNull (_, _) = True data NewGistFile = NewGistFile { newGistFileContent :: !Text