@@ -1,11 +1,9 @@
11{-# LANGUAGE BangPatterns #-}
22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE FunctionalDependencies #-}
54{-# LANGUAGE GADTs #-}
65{-# LANGUAGE GeneralizedNewtypeDeriving #-}
76{-# LANGUAGE KindSignatures #-}
8−{-# LANGUAGE MultiParamTypeClasses #-}
97{-# LANGUAGE OverloadedStrings #-}
108{-# LANGUAGE PatternSynonyms #-}
119{-# LANGUAGE RankNTypes #-}
@@ -15,7 +13,7 @@
1513{-# LANGUAGE ViewPatterns #-}
1614
1715-- Module : Network.PagerDuty.Internal.Types
18−-- Copyright : (c) 2013-2015 Brendan Hay <brendan.g.hay@gmail.com>
16+-- Copyright : (c) 2013-2014 Brendan Hay <brendan.g.hay@gmail.com>
1917-- License : This Source Code Form is subject to the terms of
2018-- the Mozilla Public License, v. 2.0.
2119-- A copy of the MPL can be found in the LICENSE file or
@@ -27,33 +25,35 @@
2725module Network.PagerDuty.Internal.Types where
2826
2927import Control.Applicative
30−import Control.Lens hiding ((.=), Empty)
28+import Control.Lens hiding ((.=))
3129import Control.Monad.IO.Class
32−import Data.Aeson hiding (Error, Key)
33−import qualified Data.Aeson.KeyMap as Map
30+import Data.Aeson hiding (Error)
3431import Data.Aeson.Types (Parser)
3532import Data.ByteString (ByteString)
3633import qualified Data.ByteString.Char8 as BS
3734import Data.ByteString.Conversion hiding (List)
3835import Data.Default.Class
3936import Data.Function (on)
37+import qualified Data.HashMap.Strict as Map
4038import Data.List (deleteBy, intersperse)
4139import Data.Monoid
4240import Data.String
4341import Data.Text (Text)
4442import qualified Data.Text as Text
4543import qualified Data.Text.Encoding as Text
46−import Data.Time (UTCTime, TimeZone, formatTime, utc, timeZoneName)
44+import Data.Time
4745import GHC.TypeLits
4846import Network.HTTP.Client (Manager)
4947import Network.HTTP.Types
5048import Network.HTTP.Types.QueryLike
5149import Network.PagerDuty.Internal.Query
5250import Network.PagerDuty.Internal.TH
53−import Data.Time.Locale.Compat
51+import System.Locale
52+
53+-- FIXME: Verify IncidentId/IncidentKey .. *Id/*Key are actually different or needed
5454
5555newtype CSV a = CSV [a]
56− deriving (Eq, Show, Semigroup, Monoid)
56+ deriving (Eq, Show, Monoid)
5757
5858makePrisms ''CSV
5959
@@ -70,7 +70,7 @@ instance ToByteString a => ToJSON (CSV a) where
7070 toJSON = String . Text.decodeUtf8 . toByteString'
7171
7272newtype List a = L [a]
73− deriving (Eq, Show, Semigroup, Monoid)
73+ deriving (Eq, Show, Monoid)
7474
7575deriveJSON ''List
7676makePrisms ''List
@@ -106,7 +106,7 @@ instance ToJSON Date where
106106
107107instance ToByteString Date where
108108 builder (D d) = builder
109− (formatTime defaultTimeLocale ("%Y-%m-%dT%XZ") d)
109+ (formatTime defaultTimeLocale (iso8601DateFormat $ Just "%XZ") d)
110110
111111instance QueryValues Date
112112
@@ -133,17 +133,14 @@ data Security = Basic | Token
133133 deriving (Eq, Show)
134134
135135data Auth (a :: Security) where
136− AuthBasic :: ByteString -> ByteString -> Auth 'Basic
137− AuthToken :: ByteString -> Auth 'Token
136+ AuthBasic :: ByteString -> ByteString -> Auth Basic
137+ AuthToken :: ByteString -> Auth Token
138138
139139deriving instance Eq (Auth a)
140140deriving instance Show (Auth a)
141141
142142newtype SubDomain = SubDomain { subDomain :: ByteString }
143− deriving (Eq, Show, IsString, ToByteString)
144−
145−mkSubDomain :: ByteString -> SubDomain
146−mkSubDomain = SubDomain
143+ deriving (Eq, Show, IsString)
147144
148145domain :: SubDomain -> ByteString
149146domain (SubDomain s)
@@ -180,7 +177,7 @@ prod :: SubDomain -> Auth s -> Manager -> Env s
180177prod d a m = Env d a m None
181178
182179newtype Code = Code Integer
183− deriving (Eq, Show, Num)
180+ deriving (Eq, Show)
184181
185182deriveJSON ''Code
186183
@@ -202,15 +199,6 @@ description (Code c) =
202199 2012 -> "Your account is expired and cannot use the API"
203200 _ -> "Unrecognised error code"
204201
205−class HasMessage s a | s -> a where
206− -- | A short human-readable message describing the error.
207− message :: Lens' s a
208−
209−class HasErrors s a | s -> a where
210− -- | A list of human-readable reasons for the error. These values,
211− -- and even their format, are subject to change.
212− errors :: Lens' s a
213−
214202data IntegrationError = IntegrationError
215203 { _ieStatus :: Text
216204 , _ieMessage :: Text
@@ -219,53 +207,26 @@ data IntegrationError = IntegrationError
219207
220208deriveRecord ''IntegrationError
221209
222−instance HasMessage IntegrationError Text where message = ieMessage
223−instance HasErrors IntegrationError [Text] where errors = ieErrors
224−
225−status :: Lens' IntegrationError Text
226−status = ieStatus
227−
228−data RESTError = RESTError
210+data RestError = RestError
229211 { _reCode :: Code
230212 , _reMessage :: Text
231213 , _reErrors :: [Text]
232214 } deriving (Eq, Show)
233215
234−deriveRecord ''RESTError
235−
236−instance HasMessage RESTError Text where message = reMessage
237−instance HasErrors RESTError [Text] where errors = reErrors
238−
239−-- | In the case of an error, the PagerDuty error code can give further details
240−-- on the nature of the error.
241−--
242−-- /See:/ 'description'
243−code :: Lens' RESTError Code
244−code = reCode
216+deriveRecord ''RestError
245217
246218data Error
247− = Internal Text
219+ = Internal String
248220 | Integration IntegrationError
249− | REST RESTError
221+ | Rest RestError
250222 deriving (Eq, Show)
251223
252224instance FromJSON Error where
253− parseJSON o = (REST <$> parseJSON o)
225+ parseJSON o = (Rest <$> parseJSON o)
254226 <|> (Integration <$> parseJSON o)
255227
256228makePrisms ''Error
257229
258−instance HasMessage Error Text where
259− message = lens f g
260− where
261− f (Internal x) = x
262− f (Integration s) = _ieMessage s
263− f (REST s) = _reMessage s
264−
265− g (Internal _) x = Internal x
266− g (Integration s) x = Integration $ s { _ieMessage = x }
267− g (REST s) x = REST $ s { _reMessage = x }
268−
269230data Pager = Pager
270231 { _pgOffset :: !Int
271232 -- ^ The offset used in the execution of the query.
@@ -279,13 +240,8 @@ data Pager = Pager
279240
280241makeLenses ''Pager
281242
282−data Page a = Page
283− { _pgItem :: a
284− , _pgPager :: Maybe Pager
285− }
286−
287−instance FromJSON a => FromJSON (Page a) where
288− parseJSON = withObject "paginated" $ \o -> Page
243+instance FromJSON a => FromJSON (a, Maybe Pager) where
244+ parseJSON = withObject "paginated" $ \o -> (,)
289245 <$> parseJSON (Object o)
290246 <*> optional (parse o)
291247 where
@@ -310,13 +266,11 @@ data Path where
310266 Path :: Path
311267 Seg :: ToByteString a => a -> Path
312268
313−instance Semigroup Path where
314− x <> Path = x
315− Path <> y = y
316− Seg x <> Seg y = Seg (builder x <> "/" <> builder y)
317−
318269instance Monoid Path where
319− mempty = Path
270+ mempty = Path
271+ mappend x Path = x
272+ mappend Path y = y
273+ mappend (Seg x) (Seg y) = Seg (builder x <> "/" <> builder y)
320274
321275instance IsString Path where
322276 fromString = Seg
@@ -393,13 +347,13 @@ class Paginate a where
393347 next :: Request a s b -> Maybe Pager -> Maybe (Request a s b)
394348 next rq = maybe Nothing go
395349 where
396− go x | x ^. pgTotal == 0 = Nothing
397− | otherwise = Just $
398− rq & pager ?~ (x & pgOffset +~ x ^. pgTotal)
350+ go x | x^.pgTotal == 0 = Nothing
351+ | otherwise = Just $
352+ rq & pager ?~ (x & pgOffset +~ x^.pgTotal)
399353 & query %~ (add . clear)
400354 where
401355 add :: Query -> Query
402− add = maybe id ((:) . (k,) . Just . Text.encodeUtf8) (x ^. pgQuery)
356+ add = maybe id ((:) . (k,) . Just . Text.encodeUtf8) (x^.pgQuery)
403357
404358 clear :: Query -> Query
405359 clear = deleteBy ((==) `on` fst) (k, Nothing)
@@ -410,9 +364,6 @@ class Paginate a where
410364newtype Key (a :: Symbol) = Key Text
411365 deriving (Eq, Show, IsString)
412366
413−mkKey :: Text -> Key a
414−mkKey = Key
415−
416367instance FromJSON (Key a) where
417368 parseJSON = withText "key" (return . Key)
418369
@@ -433,9 +384,6 @@ type IncidentKey = Key "incident"
433384newtype Id (a :: Symbol) = Id Text
434385 deriving (Eq, Show, IsString)
435386
436−mkId :: Text -> Id a
437−mkId = Id
438−
439387instance FromJSON (Id a) where
440388 parseJSON = withText "id" (return . Id)
441389
@@ -483,10 +431,7 @@ instance QueryLike Empty where
483431 toQuery = const []
484432
485433newtype Address = Address Text
486− deriving (Eq, Show, IsString)
487−
488−mkAddress :: Text -> Address
489−mkAddress = Address
434+ deriving (Eq, Show)
490435
491436deriveJSON ''Address
492437makePrisms ''Address