on develop help sign in
Replacing usage of Servant.Common.Text with http-api-data
51d2997Brendan Hay
core/gogol-core.cabal+2
@@ -46,12 +46,14 @@ library
46 aeson >= 0.8
46 aeson >= 0.8
47 , attoparsec >= 0.11.3
47 , attoparsec >= 0.11.3
48 , base >= 4.7
48 , base >= 4.7
49+ , bifunctors >= 0.1
49 , bytestring >= 0.9
50 , bytestring >= 0.9
50 , case-insensitive >= 1.2
51 , case-insensitive >= 1.2
51 , conduit >= 1.1
52 , conduit >= 1.1
52 , dlist >= 0.7
53 , dlist >= 0.7
53 , exceptions >= 0.6
54 , exceptions >= 0.6
54 , hashable >= 1.2
55 , hashable >= 1.2
56+ , http-api-data >= 0.2
55 , http-client >= 0.4.4
57 , http-client >= 0.4.4
56 , http-media >= 0.6
58 , http-media >= 0.6
57 , http-types >= 0.8.6
59 , http-types >= 0.8.6
core/src/Network/Google/Data/Time.hs+27−36
@@ -22,49 +22,53 @@ module Network.Google.Data.Time
22
22
23import Control.Lens
23import Control.Lens
24import Data.Aeson
24import Data.Aeson
25−import Data.Aeson.Encode
26−import qualified Data.Aeson.Types as Aeson
25+import qualified Data.Aeson.Types as Aeson
27import Data.Attoparsec.Text
26import Data.Attoparsec.Text
28−import Data.Bits ((.&.))
27+import Data.Bifunctor (first, second)
28+import Data.Bits ((.&.))
29−import Data.Char (ord)
29+import Data.Char (ord)
30−import Data.Data (Data, Typeable)
30+import Data.Data (Data, Typeable)
31−import Data.Text (Text)
31+import Data.Text (Text)
32−import qualified Data.Text as Text
32+import qualified Data.Text as Text
33−import qualified Data.Text.Lazy as LText
34−import qualified Data.Text.Lazy.Builder as Build
35import Data.Time
33import Data.Time
36import GHC.Generics
34import GHC.Generics
37−import Servant.Common.Text
35+import Web.HttpApiData (FromHttpApiData (..), ToHttpApiData (..))
38
36
39-- | This SHOULD be a time in the format of hh:mm:ss. It is
37-- | This SHOULD be a time in the format of hh:mm:ss. It is
40-- recommended that you use the "date-time" format instead of "time"
38-- recommended that you use the "date-time" format instead of "time"
41-- unless you need to transfer only the time part.
39-- unless you need to transfer only the time part.
42−newtype Time' = Time' TimeOfDay
40+newtype Time' = Time' { unTime :: TimeOfDay }
43 deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
41 deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
44
42
45_Time :: Iso' Time' TimeOfDay
43_Time :: Iso' Time' TimeOfDay
46−_Time = iso (\(Time' t) -> t) Time'
44+_Time = iso unTime Time'
45+
46+instance ToHttpApiData Time' where
47+ toQueryParam = Text.pack . show . unTime
48+
49+instance FromHttpApiData Time' where
50+ parseQueryParam = second Time' . parseText timeParser
47
51
48-- | This SHOULD be a date in the format of YYYY-MM-DD. It is
52-- | This SHOULD be a date in the format of YYYY-MM-DD. It is
49-- recommended that you use the "date-time" format instead of "date"
53-- recommended that you use the "date-time" format instead of "date"
50-- unless you need to transfer only the date part.
54-- unless you need to transfer only the date part.
51−newtype Date' = Date' Day
55+newtype Date' = Date' { unDate :: Day }
52− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
56+ deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, ToHttpApiData, FromHttpApiData)
53
57
54_Date :: Iso' Date' Day
58_Date :: Iso' Date' Day
55−_Date = iso (\(Date' t) -> t) Date'
59+_Date = iso unDate Date'
56
60
57-- | This SHOULD be a date in ISO 8601 format of YYYY-MM-
61-- | This SHOULD be a date in ISO 8601 format of YYYY-MM-
58-- DDThh:mm:ssZ in UTC time. This is the recommended form of date/timestamp.
62-- DDThh:mm:ssZ in UTC time. This is the recommended form of date/timestamp.
59−newtype DateTime' = DateTime' UTCTime
63+newtype DateTime' = DateTime' { unDateTime :: UTCTime }
60− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
64+ deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, ToHttpApiData, FromHttpApiData)
61
65
62_DateTime :: Iso' DateTime' UTCTime
66_DateTime :: Iso' DateTime' UTCTime
63−_DateTime = iso (\(DateTime' t) -> t) DateTime'
67+_DateTime = iso unDateTime DateTime'
64
68
65−instance FromText Time' where fromText = fmap Time' . parseText timeParser
69+instance ToJSON Time' where toJSON = String . toQueryParam
66−instance FromText Date' where fromText = fmap Date' . parseText dayParser
70+instance ToJSON Date' where toJSON = String . toQueryParam
67−instance FromText DateTime' where fromText = Aeson.parseMaybe parseJSON . String
71+instance ToJSON DateTime' where toJSON = toJSON . unDateTime
68
72
69instance FromJSON Time' where
73instance FromJSON Time' where
70 parseJSON = fmap Time' . withText "time" (run timeParser)
74 parseJSON = fmap Time' . withText "time" (run timeParser)
@@ -75,21 +79,8 @@ instance FromJSON Date' where
75instance FromJSON DateTime' where
79instance FromJSON DateTime' where
76 parseJSON = fmap DateTime' . parseJSON
80 parseJSON = fmap DateTime' . parseJSON
77
81
78−-- FIXME: Revisit once aeson-0.10 is more widely available.
79−instance ToText Time' where toText = Text.pack . show . view _Time
80−instance ToText Date' where toText = Text.pack . show . view _Date
81−instance ToText DateTime' where toText = encodeText
82−
83−-- FIXME: Revisit once aeson-0.10 is more widely available.
84−instance ToJSON Time' where toJSON = String . toText
85−instance ToJSON Date' where toJSON = String . toText
86−instance ToJSON DateTime' where toJSON = toJSON . view _DateTime
87−
88−parseText :: Parser a -> Text -> Maybe a
82+parseText :: Parser a -> Text -> Either Text a
89−parseText p = either (const Nothing) Just . parseOnly p
83+parseText p = first Text.pack . parseOnly p
90−
91−encodeText :: ToJSON a => a -> Text
92−encodeText = LText.toStrict . Build.toLazyText . encodeToTextBuilder . toJSON
93
84
94-- | Parse a time of the form @HH:MM:SS@.
85-- | Parse a time of the form @HH:MM:SS@.
95timeParser :: Parser TimeOfDay
86timeParser :: Parser TimeOfDay