-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathDefinitions.hs
More file actions
279 lines (235 loc) · 8.88 KB
/
Definitions.hs
File metadata and controls
279 lines (235 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
-----------------------------------------------------------------------------
-- |
-- License : BSD-3-Clause
-- Maintainer : Oleg Grenrus <oleg.grenrus@iki.fi>
--
module GitHub.Data.Definitions where
import GitHub.Internal.Prelude
import Prelude ()
import Control.Monad (mfilter)
import Data.Aeson.Types (Parser)
import Network.HTTP.Client (HttpException)
import qualified Control.Exception as E
import qualified Data.ByteString as BS
import qualified Data.Text as T
import qualified Network.HTTP.Types as Types
import GitHub.Data.Id (Id)
import GitHub.Data.Name (Name)
import GitHub.Data.URL (URL (..))
-- | Errors have been tagged according to their source, so you can more easily
-- dispatch and handle them.
data Error
= HTTPError !HttpException -- ^ A HTTP error occurred. The actual caught error is included.
| ParseError !Text -- ^ An error in the parser itself.
| JsonError !Text -- ^ The JSON is malformed or unexpected.
| UserError !Text -- ^ Incorrect input.
deriving (Show, Typeable)
instance E.Exception Error
-- | Type of the repository owners.
data OwnerType = OwnerUser | OwnerOrganization
deriving (Eq, Ord, Enum, Bounded, Show, Read, Generic, Typeable, Data)
instance NFData OwnerType
instance Binary OwnerType
data SimpleUser = SimpleUser
{ simpleUserId :: !(Id User)
, simpleUserLogin :: !(Name User)
, simpleUserAvatarUrl :: !URL
, simpleUserUrl :: !URL
}
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData SimpleUser where rnf = genericRnf
instance Binary SimpleUser
data SimpleOrganization = SimpleOrganization
{ simpleOrganizationId :: !(Id Organization)
, simpleOrganizationLogin :: !(Name Organization)
, simpleOrganizationUrl :: !URL
, simpleOrganizationAvatarUrl :: !URL
}
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData SimpleOrganization where rnf = genericRnf
instance Binary SimpleOrganization
-- | Sometimes we don't know the type of the owner, e.g. in 'Repo'
data SimpleOwner = SimpleOwner
{ simpleOwnerId :: !(Id Owner)
, simpleOwnerLogin :: !(Name Owner)
, simpleOwnerUrl :: !URL
, simpleOwnerAvatarUrl :: !URL
, simpleOwnerType :: !OwnerType
}
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData SimpleOwner where rnf = genericRnf
instance Binary SimpleOwner
data User = User
{ userId :: !(Id User)
, userLogin :: !(Name User)
, userName :: !(Maybe Text)
, userType :: !OwnerType -- ^ Should always be 'OwnerUser'
, userCreatedAt :: !UTCTime
, userPublicGists :: !Int
, userAvatarUrl :: !URL
, userFollowers :: !Int
, userFollowing :: !Int
, userHireable :: !(Maybe Bool)
, userBlog :: !(Maybe Text)
, userBio :: !(Maybe Text)
, userPublicRepos :: !Int
, userLocation :: !(Maybe Text)
, userCompany :: !(Maybe Text)
, userEmail :: !(Maybe Text)
, userUrl :: !URL
, userHtmlUrl :: !URL
}
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData User where rnf = genericRnf
instance Binary User
data Organization = Organization
{ organizationId :: !(Id Organization)
, organizationLogin :: !(Name Organization)
, organizationName :: !(Maybe Text)
, organizationType :: !OwnerType -- ^ Should always be 'OwnerOrganization'
, organizationBlog :: !(Maybe Text)
, organizationLocation :: !(Maybe Text)
, organizationFollowers :: !Int
, organizationCompany :: !(Maybe Text)
, organizationAvatarUrl :: !URL
, organizationPublicGists :: !Int
, organizationHtmlUrl :: !URL
, organizationEmail :: !(Maybe Text)
, organizationFollowing :: !Int
, organizationPublicRepos :: !Int
, organizationUrl :: !URL
, organizationCreatedAt :: !UTCTime
}
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData Organization where rnf = genericRnf
instance Binary Organization
-- | In practic, you cam't have concrete values of 'Owner'.
newtype Owner = Owner (Either User Organization)
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData Owner where rnf = genericRnf
instance Binary Owner
fromOwner :: Owner -> Either User Organization
fromOwner (Owner owner) = owner
-- JSON instances
instance FromJSON OwnerType where
parseJSON = withText "Owner type" $ \t ->
case t of
"User" -> pure $ OwnerUser
"Organization" -> pure $ OwnerOrganization
_ -> fail $ "Unknown owner type: " ++ T.unpack t
instance FromJSON SimpleUser where
parseJSON = withObject "SimpleUser" $ \obj -> do
SimpleUser
<$> obj .: "id"
<*> obj .: "login"
<*> obj .: "avatar_url"
<*> obj .: "url"
instance FromJSON SimpleOrganization where
parseJSON = withObject "SimpleOrganization" $ \obj ->
SimpleOrganization
<$> obj .: "id"
<*> obj .: "login"
<*> obj .: "url"
<*> obj .: "avatar_url"
instance FromJSON SimpleOwner where
parseJSON = withObject "SimpleOwner" $ \obj -> do
SimpleOwner
<$> obj .: "id"
<*> obj .: "login"
<*> obj .: "url"
<*> obj .: "avatar_url"
<*> obj .: "type"
parseUser :: Object -> Parser User
parseUser obj = User
<$> obj .: "id"
<*> obj .: "login"
<*> obj .:? "name"
<*> obj .: "type"
<*> obj .: "created_at"
<*> obj .: "public_gists"
<*> obj .: "avatar_url"
<*> obj .: "followers"
<*> obj .: "following"
<*> obj .:? "hireable"
<*> obj .:? "blog"
<*> obj .:? "bio"
<*> obj .: "public_repos"
<*> obj .:? "location"
<*> obj .:? "company"
<*> obj .:? "email"
<*> obj .: "url"
<*> obj .: "html_url"
parseOrganization :: Object -> Parser Organization
parseOrganization obj = Organization
<$> obj .: "id"
<*> obj .: "login"
<*> obj .:? "name"
<*> obj .: "type"
<*> obj .:? "blog"
<*> obj .:? "location"
<*> obj .: "followers"
<*> obj .:? "company"
<*> obj .: "avatar_url"
<*> obj .: "public_gists"
<*> obj .: "html_url"
<*> obj .:? "email"
<*> obj .: "following"
<*> obj .: "public_repos"
<*> obj .: "url"
<*> obj .: "created_at"
instance FromJSON User where
parseJSON = mfilter ((== OwnerUser) . userType) . withObject "User" parseUser
instance FromJSON Organization where
parseJSON = withObject "Organization" parseOrganization
instance FromJSON Owner where
parseJSON = withObject "Owner" $ \obj -> do
t <- obj .: "type"
case t of
OwnerUser -> Owner . Left <$> parseUser obj
OwnerOrganization -> Owner . Right <$> parseOrganization obj
-- | Filter members returned in the list.
data OrgMemberFilter
= OrgMemberFilter2faDisabled -- ^ Members without two-factor authentication enabled. Available for organization owners.
| OrgMemberFilterAll -- ^ All members the authenticated user can see.
deriving (Show, Eq, Ord, Enum, Bounded, Typeable, Data, Generic)
-- | Filter members returned by their role.
data OrgMemberRole
= OrgMemberRoleAll -- ^ All members of the organization, regardless of role.
| OrgMemberRoleAdmin -- ^ Organization owners.
| OrgMemberRoleMember -- ^ Non-owner organization members.
deriving (Show, Eq, Ord, Enum, Bounded, Typeable, Data, Generic)
-- | Request query string
type QueryString = [(BS.ByteString, [EscapeItem])]
newtype EscapeItem = Esc Types.EscapeItem deriving (Eq,Ord, Show)
unwrapEsc :: [(BS.ByteString, [EscapeItem])] -> [(BS.ByteString, [Types.EscapeItem])]
unwrapEsc qs = map t qs
where t (bs, items) = (bs, map unesc items)
unesc (Esc i) = i
wrapEsc :: [(BS.ByteString, [Types.EscapeItem])] -> [(BS.ByteString, [EscapeItem])]
wrapEsc qs = map t qs
where t (bs, items) = (bs, map Esc items)
instance Hashable EscapeItem where
hashWithSalt salt (Esc (Types.QE b)) =
salt `hashWithSalt` (0 :: Int)
`hashWithSalt` b
hashWithSalt salt (Esc (Types.QN b)) =
salt `hashWithSalt` (1 :: Int)
`hashWithSalt` b
-- | Count of elements
type Count = Int
-------------------------------------------------------------------------------
-- IssueLabel
-------------------------------------------------------------------------------
data IssueLabel = IssueLabel
{ labelColor :: !Text
, labelUrl :: !URL
, labelName :: !(Name IssueLabel)
}
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData IssueLabel where rnf = genericRnf
instance Binary IssueLabel
instance FromJSON IssueLabel where
parseJSON = withObject "IssueLabel" $ \o -> IssueLabel
<$> o .: "color"
<*> o .:? "url" .!= URL "" -- in events there aren't URL
<*> o .: "name"