Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparently handle Base64 encoding #29

Merged
merged 3 commits into from
Jun 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/gogol-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ library
ghc-options: -Wall

exposed-modules:
Network.Google.Data.JSON
Network.Google.Data.Base64
, Network.Google.Data.JSON
, Network.Google.Data.Numeric
, Network.Google.Data.Time
, Network.Google.Prelude
Expand All @@ -58,6 +59,7 @@ library
, http-media >= 0.6
, http-types >= 0.8.6
, lens >= 4.4
, memory >= 0.8
, resourcet >= 1.1
, scientific >= 0.3
, servant >= 0.4.4
Expand Down
53 changes: 53 additions & 0 deletions core/src/Network/Google/Data/Base64.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

-- |
-- Module : Network.Google.Data.Base64
-- Copyright : (c) 2013-2016 Brendan Hay
-- License : Mozilla Public License, v. 2.0.
-- Maintainer : Brendan Hay <[email protected]>
-- Stability : provisional
-- Portability : non-portable (GHC extensions)
--
module Network.Google.Data.Base64
( Base64 (..)
, _Base64
) where

import Control.Lens (Iso', iso)
import Data.Aeson (FromJSON (..), ToJSON (..))
import qualified Data.ByteArray.Encoding as BA
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS8
import Data.Data (Data, Typeable)
import Data.Hashable
import qualified Data.Text.Encoding as Text
import GHC.Generics (Generic)
import Network.Google.Data.JSON (parseJSONText, toJSONText)
import Web.HttpApiData (FromHttpApiData (..),
ToHttpApiData (..))

-- | Base64 encoded binary data.
--
-- Encoding\/decoding is automatically deferred to serialisation and deserialisation
-- respectively.
newtype Base64 = Base64 { unBase64 :: ByteString }
deriving (Eq, Read, Ord, Data, Typeable, Generic)

instance Hashable Base64

_Base64 :: Iso' Base64 ByteString
_Base64 = iso unBase64 Base64

instance ToHttpApiData Base64 where
toQueryParam = Text.decodeUtf8 . toHeader
toHeader = BA.convertToBase BA.Base64 . unBase64

instance FromHttpApiData Base64 where
parseQueryParam = parseHeader . Text.encodeUtf8
parseHeader = either fail (pure . Base64) . BA.convertFromBase BA.Base64

instance Show Base64 where show = show . BS8.unpack . unBase64
instance FromJSON Base64 where parseJSON = parseJSONText "Base64"
instance ToJSON Base64 where toJSON = toJSONText
49 changes: 25 additions & 24 deletions core/src/Network/Google/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@ module Network.Google.Prelude
( module Export
) where

import Control.Lens as Export (Lens', lens, mapping,
( # ), (^.), _Just)
import Data.Data as Export (Data, Typeable)
import Data.Hashable as Export
import Data.HashMap.Strict as Export (HashMap)
import Data.Int as Export (Int32, Int64)
import Data.Maybe as Export
import Data.Monoid as Export (mempty, (<>))
import Data.Proxy as Export
import Data.Text as Export (Text)
import Data.Time as Export (Day, TimeOfDay, UTCTime)
import Data.Word as Export (Word32, Word64, Word8)
import GHC.Generics as Export (Generic)
import Network.Google.Data.JSON as Export
import Network.Google.Data.Numeric as Export
import Network.Google.Data.Time as Export
import Network.Google.Types as Export
import Network.HTTP.Client as Export (RequestBody)
import Numeric.Natural as Export (Natural)
import Prelude as Export hiding (product)
import Servant.API as Export hiding (getResponse)
import Servant.Utils.Links as Export hiding (Link)
import Web.HttpApiData as Export (FromHttpApiData (..),
ToHttpApiData (..))
import Control.Lens as Export (Lens', lens, mapping, ( # ), (^.), _Just)
import Data.ByteString as Export (ByteString)
import Data.Data as Export (Data, Typeable)
import Data.Hashable as Export
import Data.HashMap.Strict as Export (HashMap)
import Data.Int as Export (Int32, Int64)
import Data.Maybe as Export
import Data.Monoid as Export (mempty, (<>))
import Data.Proxy as Export
import Data.Text as Export (Text)
import Data.Time as Export (Day, TimeOfDay, UTCTime)
import Data.Word as Export (Word32, Word64, Word8)
import GHC.Generics as Export (Generic)
import Network.HTTP.Client as Export (RequestBody)
import Numeric.Natural as Export (Natural)
import Prelude as Export hiding (product)
import Servant.API as Export hiding (getResponse)
import Servant.Utils.Links as Export hiding (Link)
import Web.HttpApiData as Export (FromHttpApiData (..), ToHttpApiData (..))

import Network.Google.Data.Base64 as Export
import Network.Google.Data.JSON as Export
import Network.Google.Data.Numeric as Export
import Network.Google.Data.Time as Export
import Network.Google.Types as Export
6 changes: 3 additions & 3 deletions gen/src/Gen/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ externalLit = \case
Nat -> TyCon "Natural"
Float -> TyCon "Double"
Double -> TyCon "Double"
Byte -> TyCon "Word8"
Byte -> TyCon "ByteString"
UInt32 -> TyCon "Word32"
UInt64 -> TyCon "Word64"
Int32 -> TyCon "Int32"
Expand All @@ -586,7 +586,7 @@ internalLit = \case
Nat -> TyApp (TyCon "Textual") (TyCon "Nat")
Float -> TyApp (TyCon "Textual") (TyCon "Double")
Double -> TyApp (TyCon "Textual") (TyCon "Double")
Byte -> TyApp (TyCon "Textual") (TyCon "Word8")
Byte -> TyCon "Base64"
UInt32 -> TyApp (TyCon "Textual") (TyCon "Word32")
UInt64 -> TyApp (TyCon "Textual") (TyCon "Word64")
Int32 -> TyApp (TyCon "Textual") (TyCon "Int32")
Expand Down Expand Up @@ -617,7 +617,7 @@ iso = \case
TLit DateTime -> Just (var "_DateTime")
TLit Float -> Just (var "_Coerce")
TLit Double -> Just (var "_Coerce")
TLit Byte -> Just (var "_Coerce")
TLit Byte -> Just (var "_Base64")
TLit UInt32 -> Just (var "_Coerce")
TLit UInt64 -> Just (var "_Coerce")
TLit Int32 -> Just (var "_Coerce")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ instance ToJSON AccountBidderLocationItem where
--
-- /See:/ 'privateData' smart constructor.
data PrivateData = PrivateData'
{ _pdReferencePayload :: !(Maybe (Textual Word8))
{ _pdReferencePayload :: !(Maybe Base64)
, _pdReferenceId :: !(Maybe Text)
} deriving (Eq,Show,Data,Typeable,Generic)

Expand All @@ -641,11 +641,11 @@ privateData =
, _pdReferenceId = Nothing
}

pdReferencePayload :: Lens' PrivateData (Maybe Word8)
pdReferencePayload :: Lens' PrivateData (Maybe ByteString)
pdReferencePayload
= lens _pdReferencePayload
(\ s a -> s{_pdReferencePayload = a})
. mapping _Coerce
. mapping _Base64

pdReferenceId :: Lens' PrivateData (Maybe Text)
pdReferenceId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4468,7 +4468,7 @@ instance ToJSON UserPhone where
--
-- /See:/ 'userPhoto' smart constructor.
data UserPhoto = UserPhoto'
{ _upPhotoData :: !(Maybe (Textual Word8))
{ _upPhotoData :: !(Maybe Base64)
, _upEtag :: !(Maybe Text)
, _upHeight :: !(Maybe (Textual Int32))
, _upKind :: !Text
Expand Down Expand Up @@ -4512,10 +4512,10 @@ userPhoto =
}

-- | Base64 encoded photo data
upPhotoData :: Lens' UserPhoto (Maybe Word8)
upPhotoData :: Lens' UserPhoto (Maybe ByteString)
upPhotoData
= lens _upPhotoData (\ s a -> s{_upPhotoData = a}) .
mapping _Coerce
mapping _Base64

-- | ETag of the resource.
upEtag :: Lens' UserPhoto (Maybe Text)
Expand Down
6 changes: 3 additions & 3 deletions gogol-bigquery/gen/Network/Google/BigQuery/Types/Product.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2590,7 +2590,7 @@ instance ToJSON ProjectListProjectsItem where
--
-- /See:/ 'bigtableColumn' smart constructor.
data BigtableColumn = BigtableColumn'
{ _bcQualifierEncoded :: !(Maybe (Textual Word8))
{ _bcQualifierEncoded :: !(Maybe Base64)
, _bcFieldName :: !(Maybe Text)
, _bcQualifierString :: !(Maybe Text)
, _bcOnlyReadLatest :: !(Maybe Bool)
Expand Down Expand Up @@ -2633,11 +2633,11 @@ bigtableColumn =
-- qualifier. However, if the qualifier is not a valid BigQuery field
-- identifier i.e. does not match [a-zA-Z][a-zA-Z0-9_]*, a valid identifier
-- must be provided as field_name.
bcQualifierEncoded :: Lens' BigtableColumn (Maybe Word8)
bcQualifierEncoded :: Lens' BigtableColumn (Maybe ByteString)
bcQualifierEncoded
= lens _bcQualifierEncoded
(\ s a -> s{_bcQualifierEncoded = a})
. mapping _Coerce
. mapping _Base64

-- | [Optional] If the qualifier is not a valid BigQuery field identifier
-- i.e. does not match [a-zA-Z][a-zA-Z0-9_]*, a valid identifier must be
Expand Down
6 changes: 3 additions & 3 deletions gogol-books/gen/Network/Google/Books/Types/Product.hs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ instance ToJSON ReviewSource where
--
-- /See:/ 'annotationData' smart constructor.
data AnnotationData = AnnotationData'
{ _annEncodedData :: !(Maybe (Textual Word8))
{ _annEncodedData :: !(Maybe Base64)
, _annKind :: !Text
, _annData :: !(Maybe JSONValue)
, _annSelfLink :: !(Maybe Text)
Expand Down Expand Up @@ -452,11 +452,11 @@ annotationData =
}

-- | Base64 encoded data for this annotation data.
annEncodedData :: Lens' AnnotationData (Maybe Word8)
annEncodedData :: Lens' AnnotationData (Maybe ByteString)
annEncodedData
= lens _annEncodedData
(\ s a -> s{_annEncodedData = a})
. mapping _Coerce
. mapping _Base64

-- | Resource Type
annKind :: Lens' AnnotationData Text
Expand Down
Loading