@@ -15,7 +15,7 @@
15{-# LANGUAGE ViewPatterns #-}
15{-# LANGUAGE ViewPatterns #-}
17-- Module : Network.PagerDuty.Internal.Types
17-- Module : Network.PagerDuty.Internal.Types
18−-- Copyright : (c) 2013-2014 Brendan Hay <brendan.g.hay@gmail.com>
18+-- Copyright : (c) 2013-2015 Brendan Hay <brendan.g.hay@gmail.com>
19-- License : This Source Code Form is subject to the terms of
19-- License : This Source Code Form is subject to the terms of
20-- the Mozilla Public License, v. 2.0.
20-- the Mozilla Public License, v. 2.0.
21-- A copy of the MPL can be found in the LICENSE file or
21-- A copy of the MPL can be found in the LICENSE file or
@@ -27,35 +27,33 @@
27module Network.PagerDuty.Internal.Types where
27module Network.PagerDuty.Internal.Types where
29import Control.Applicative
29import Control.Applicative
30−import Control.Lens hiding ((.=))
30+import Control.Lens hiding ((.=), Empty)
31import Control.Monad.IO.Class
31import Control.Monad.IO.Class
32−import Data.Aeson hiding (Error)
32+import Data.Aeson hiding (Error, Key)
33+import qualified Data.Aeson.KeyMap as Map
33import Data.Aeson.Types (Parser)
34import Data.Aeson.Types (Parser)
34import Data.ByteString (ByteString)
35import Data.ByteString (ByteString)
35import qualified Data.ByteString.Char8 as BS
36import qualified Data.ByteString.Char8 as BS
36import Data.ByteString.Conversion hiding (List)
37import Data.ByteString.Conversion hiding (List)
37import Data.Default.Class
38import Data.Default.Class
38import Data.Function (on)
39import Data.Function (on)
39−import qualified Data.HashMap.Strict as Map
40import Data.List (deleteBy, intersperse)
40import Data.List (deleteBy, intersperse)
41import Data.Monoid
41import Data.Monoid
42import Data.String
42import Data.String
43import Data.Text (Text)
43import Data.Text (Text)
44import qualified Data.Text as Text
44import qualified Data.Text as Text
45import qualified Data.Text.Encoding as Text
45import qualified Data.Text.Encoding as Text
46−import Data.Time
46+import Data.Time (UTCTime, TimeZone, formatTime, utc, timeZoneName)
47import GHC.TypeLits
47import GHC.TypeLits
48import Network.HTTP.Client (Manager)
48import Network.HTTP.Client (Manager)
49import Network.HTTP.Types
49import Network.HTTP.Types
50import Network.HTTP.Types.QueryLike
50import Network.HTTP.Types.QueryLike
51import Network.PagerDuty.Internal.Query
51import Network.PagerDuty.Internal.Query
52import Network.PagerDuty.Internal.TH
52import Network.PagerDuty.Internal.TH
53−import System.Locale
53+import Data.Time.Locale.Compat
55−-- FIXME: Verify IncidentId/IncidentKey .. *Id/*Key are actually different or needed
57newtype CSV a = CSV [a]
55newtype CSV a = CSV [a]
58− deriving (Eq, Show, Monoid)
56+ deriving (Eq, Show, Semigroup, Monoid)
60makePrisms ''CSV
58makePrisms ''CSV
@@ -72,7 +70,7 @@ instance ToByteString a => ToJSON (CSV a) where
72 toJSON = String . Text.decodeUtf8 . toByteString'
70 toJSON = String . Text.decodeUtf8 . toByteString'
74newtype List a = L [a]
72newtype List a = L [a]
75− deriving (Eq, Show, Monoid)
73+ deriving (Eq, Show, Semigroup, Monoid)
77deriveJSON ''List
75deriveJSON ''List
78makePrisms ''List
76makePrisms ''List
@@ -108,7 +106,7 @@ instance ToJSON Date where
109instance ToByteString Date where
107instance ToByteString Date where
110 builder (D d) = builder
108 builder (D d) = builder
111− (formatTime defaultTimeLocale (iso8601DateFormat $ Just "%XZ") d)
109+ (formatTime defaultTimeLocale ("%Y-%m-%dT%XZ") d)
113instance QueryValues Date
111instance QueryValues Date
@@ -135,14 +133,17 @@ data Security = Basic | Token
135 deriving (Eq, Show)
133 deriving (Eq, Show)
137data Auth (a :: Security) where
135data Auth (a :: Security) where
138− AuthBasic :: ByteString -> ByteString -> Auth Basic
136+ AuthBasic :: ByteString -> ByteString -> Auth 'Basic
139− AuthToken :: ByteString -> Auth Token
137+ AuthToken :: ByteString -> Auth 'Token
141deriving instance Eq (Auth a)
139deriving instance Eq (Auth a)
142deriving instance Show (Auth a)
140deriving instance Show (Auth a)
144newtype SubDomain = SubDomain { subDomain :: ByteString }
142newtype SubDomain = SubDomain { subDomain :: ByteString }
145− deriving (Eq, Show, IsString)
143+ deriving (Eq, Show, IsString, ToByteString)
145+mkSubDomain :: ByteString -> SubDomain
146+mkSubDomain = SubDomain
147domain :: SubDomain -> ByteString
148domain :: SubDomain -> ByteString
148domain (SubDomain s)
149domain (SubDomain s)
@@ -179,7 +180,7 @@ prod :: SubDomain -> Auth s -> Manager -> Env s
179prod d a m = Env d a m None
180prod d a m = Env d a m None
181newtype Code = Code Integer
182newtype Code = Code Integer
182− deriving (Eq, Show)
183+ deriving (Eq, Show, Num)
184deriveJSON ''Code
185deriveJSON ''Code
@@ -278,8 +279,13 @@ data Pager = Pager
279makeLenses ''Pager
280makeLenses ''Pager
284+ , _pgPager :: Maybe Pager
281−instance FromJSON a => FromJSON (a, Maybe Pager) where
287+instance FromJSON a => FromJSON (Page a) where
282− parseJSON = withObject "paginated" $ \o -> (,)
288+ parseJSON = withObject "paginated" $ \o -> Page
283 <$> parseJSON (Object o)
289 <$> parseJSON (Object o)
284 <*> optional (parse o)
290 <*> optional (parse o)
@@ -304,11 +310,13 @@ data Path where
304 Path :: Path
310 Path :: Path
305 Seg :: ToByteString a => a -> Path
311 Seg :: ToByteString a => a -> Path
313+instance Semigroup Path where
316+ Seg x <> Seg y = Seg (builder x <> "/" <> builder y)
307instance Monoid Path where
318instance Monoid Path where
308− mempty = Path
319+ mempty = Path
311− mappend (Seg x) (Seg y) = Seg (builder x <> "/" <> builder y)
313instance IsString Path where
321instance IsString Path where
314 fromString = Seg
322 fromString = Seg
@@ -385,13 +393,13 @@ class Paginate a where
385 next :: Request a s b -> Maybe Pager -> Maybe (Request a s b)
393 next :: Request a s b -> Maybe Pager -> Maybe (Request a s b)
386 next rq = maybe Nothing go
394 next rq = maybe Nothing go
388− go x | x^.pgTotal == 0 = Nothing
396+ go x | x ^. pgTotal == 0 = Nothing
389− | otherwise = Just $
397+ | otherwise = Just $
390− rq & pager ?~ (x & pgOffset +~ x^.pgTotal)
398+ rq & pager ?~ (x & pgOffset +~ x ^. pgTotal)
391 & query %~ (add . clear)
399 & query %~ (add . clear)
393 add :: Query -> Query
401 add :: Query -> Query
394− add = maybe id ((:) . (k,) . Just . Text.encodeUtf8) (x^.pgQuery)
402+ add = maybe id ((:) . (k,) . Just . Text.encodeUtf8) (x ^. pgQuery)
396 clear :: Query -> Query
404 clear :: Query -> Query
397 clear = deleteBy ((==) `on` fst) (k, Nothing)
405 clear = deleteBy ((==) `on` fst) (k, Nothing)
@@ -402,6 +410,9 @@ class Paginate a where
402newtype Key (a :: Symbol) = Key Text
410newtype Key (a :: Symbol) = Key Text
403 deriving (Eq, Show, IsString)
411 deriving (Eq, Show, IsString)
413+mkKey :: Text -> Key a
405instance FromJSON (Key a) where
416instance FromJSON (Key a) where
406 parseJSON = withText "key" (return . Key)
417 parseJSON = withText "key" (return . Key)
@@ -422,6 +433,9 @@ type IncidentKey = Key "incident"
422newtype Id (a :: Symbol) = Id Text
433newtype Id (a :: Symbol) = Id Text
423 deriving (Eq, Show, IsString)
434 deriving (Eq, Show, IsString)
425instance FromJSON (Id a) where
439instance FromJSON (Id a) where
426 parseJSON = withText "id" (return . Id)
440 parseJSON = withText "id" (return . Id)
@@ -469,7 +483,10 @@ instance QueryLike Empty where
469 toQuery = const []
483 toQuery = const []
471newtype Address = Address Text
485newtype Address = Address Text
472− deriving (Eq, Show)
486+ deriving (Eq, Show, IsString)
488+mkAddress :: Text -> Address
474deriveJSON ''Address
491deriveJSON ''Address
475makePrisms ''Address
492makePrisms ''Address