Skip to content

Commit eac5751

Browse files
authored
Merge pull request #455 from matsubara0507/add-endpoint-create-gist
Add endpoint to create gist
2 parents 16bba48 + c44824b commit eac5751

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/GitHub.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ module GitHub (
6262
-- Missing endpoints:
6363
--
6464
-- * Query a specific revision of a gist
65-
-- * Create a gist
6665
-- * Edit a gist
6766
-- * List gist commits
6867
-- * Check if a gist is starred
6968
-- * Fork a gist
7069
-- * List gist forks
7170
gistsR,
7271
gistR,
72+
createGistR,
7373
starGistR,
7474
unstarGistR,
7575
deleteGistR,

src/GitHub/Data/Gists.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,35 @@ instance FromJSON GistComment where
8989
<*> o .: "body"
9090
<*> o .: "updated_at"
9191
<*> o .: "id"
92+
93+
data NewGist = NewGist
94+
{ newGistDescription :: !(Maybe Text)
95+
, newGistFiles :: !(HashMap Text NewGistFile)
96+
, newGistPublic :: !(Maybe Bool)
97+
} deriving (Show, Data, Typeable, Eq, Generic)
98+
99+
instance NFData NewGist where rnf = genericRnf
100+
instance Binary NewGist
101+
102+
instance ToJSON NewGist where
103+
toJSON NewGist { newGistDescription = description
104+
, newGistFiles = files
105+
, newGistPublic = public
106+
} = object $ filter notNull
107+
[ "description" .= description
108+
, "files" .= files
109+
, "public" .= public
110+
]
111+
where
112+
notNull (_, Null) = False
113+
notNull (_, _) = True
114+
115+
data NewGistFile = NewGistFile
116+
{ newGistFileContent :: !Text
117+
} deriving (Show, Data, Typeable, Eq, Generic)
118+
119+
instance NFData NewGistFile where rnf = genericRnf
120+
instance Binary NewGistFile
121+
122+
instance ToJSON NewGistFile where
123+
toJSON (NewGistFile c) = object ["content" .= c]

src/GitHub/Endpoints/Gists.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
module GitHub.Endpoints.Gists (
88
gistsR,
99
gistR,
10+
createGistR,
1011
starGistR,
1112
unstarGistR,
1213
deleteGistR,
@@ -28,6 +29,11 @@ gistR :: Name Gist -> Request k Gist
2829
gistR gid =
2930
query ["gists", toPathPart gid] []
3031

32+
-- | Create a new gist
33+
-- See <https://docs.github.com/rest/reference/gists#create-a-gist>
34+
createGistR :: NewGist -> Request 'RW Gist
35+
createGistR ngist = command Post ["gists"] (encode ngist)
36+
3137
-- | Star a gist by the authenticated user.
3238
-- See <https://developer.github.com/v3/gists/#star-a-gist>
3339
starGistR :: Name Gist -> GenRequest 'MtUnit 'RW ()

0 commit comments

Comments
 (0)