Skip to content

Commit

Permalink
API fixes + remove top level export
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinprakash96 committed Aug 18, 2024
1 parent d88eeda commit 6d45c9f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 74 deletions.
3 changes: 1 addition & 2 deletions azure-blob-storage/azure-blob-storage.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ common common-options

library
import: common-options
exposed-modules: Azure.Blob
Azure.Blob.DeleteBlob
exposed-modules: Azure.Blob.DeleteBlob
Azure.Blob.GetBlob
Azure.Blob.PutBlob
Azure.Blob.Types
Expand Down
28 changes: 0 additions & 28 deletions azure-blob-storage/src/Azure/Blob.hs

This file was deleted.

17 changes: 9 additions & 8 deletions azure-blob-storage/src/Azure/Blob/DeleteBlob.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
module Azure.Blob.DeleteBlob
( deleteBlobObject
, deleteBlobObjectEither
, DeleteBlob (..)
) where

import Azure.Auth (defaultAzureCredential)
Expand All @@ -24,6 +25,14 @@ import UnliftIO (MonadIO (..), throwString)
import qualified Azure.Types as Auth
import qualified Data.Text as Text

data DeleteBlob = DeleteBlob
{ accountName :: !AccountName
, containerName :: !ContainerName
, blobName :: !BlobName
, tokenStore :: !Auth.Token
}
deriving stock (Eq, Generic)

deleteBlobObject ::
MonadIO m =>
DeleteBlob ->
Expand All @@ -49,14 +58,6 @@ deleteBlobObjectEither getBlobReq = do
Right _ -> Right ()
Left err -> Left err

data DeleteBlob = DeleteBlob
{ accountName :: !AccountName
, containerName :: !ContainerName
, blobName :: !BlobName
, tokenStore :: !Auth.Token
}
deriving stock (Eq, Generic)

type DeleteBlobApi =
Capture "container-name" ContainerName
:> Capture "blob-name" BlobName
Expand Down
43 changes: 25 additions & 18 deletions azure-blob-storage/src/Azure/Blob/GetBlob.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
module Azure.Blob.GetBlob
( getBlobObject
, getBlobObjectEither
, GetBlob (..)
) where

import Azure.Auth (defaultAzureCredential)
import Azure.Blob.Types (AccountName (..), BlobName (..), ContainerName (..))
import Azure.Blob.Utils (blobStorageResourceUrl, mkBlobHostUrl)
import Data.ByteString (ByteString, fromStrict, toStrict)
import Data.Data (Proxy (..))
import Data.List.NonEmpty (NonEmpty ((:|)))
Expand All @@ -24,17 +26,26 @@ import Servant.API
import Servant.Client (BaseUrl (..), ClientM, Scheme (..), client, mkClientEnv, runClientM)
import UnliftIO (MonadIO (..), throwString)

import Azure.Blob.Utils (blobStorageResourceUrl, mkBlobHostUrl)
import qualified Azure.Types as Auth
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as Text
import qualified Network.HTTP.Media as M

data GetBlob = GetBlob
{ accountName :: !AccountName
, containerName :: !ContainerName
, blobName :: !BlobName
, tokenStore :: !Auth.Token
}
deriving stock (Eq, Generic)

getBlobObject ::
MonadIO m =>
GetBlob ->
m ByteString
getBlobObject getBlobReq = do
res <- liftIO $ getBlobObjectEither getBlobReq
FilePath ->
m ()
getBlobObject getBlobReq fp = do
res <- liftIO $ getBlobObjectEither getBlobReq fp
case res of
Left err ->
throwString $ show err
Expand All @@ -44,23 +55,17 @@ getBlobObject getBlobReq = do
getBlobObjectEither ::
MonadIO m =>
GetBlob ->
m (Either Text ByteString)
getBlobObjectEither getBlobReq = do
FilePath ->
m (Either Text ())
getBlobObjectEither getBlobReq fp = do
res <-
liftIO $
callGetBlobClient getBlobObjectApi getBlobReq
pure $
case res of
Right r -> Right r
Left err -> Left err

data GetBlob = GetBlob
{ accountName :: !AccountName
, containerName :: !ContainerName
, blobName :: !BlobName
, tokenStore :: !Auth.Token
}
deriving stock (Eq, Generic)
case res of
Right bs -> do
liftIO $ LBS.writeFile fp (fromStrict bs)
pure $ Right ()
Left err -> pure $ Left err

-- | Phantom type to encapsulate the data type in servant client types
data Blob
Expand All @@ -82,6 +87,8 @@ instance Accept Blob where
:| [ "application" M.// "octet-stream"
, "text" M.// "csv"
, "application" M.// "x-dbt"
, "image" M.// "jpeg"
, "image" M.// "png"
]

instance MimeRender Blob ByteString where
Expand Down
18 changes: 17 additions & 1 deletion azure-blob-storage/src/Azure/Blob/PutBlob.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
Expand All @@ -6,14 +7,16 @@
module Azure.Blob.PutBlob
( putBlobObjectEither
, putBlobObject
, PutBlob (..)
) where

import Azure.Auth (defaultAzureCredential)
import Azure.Blob.Types (BlobName (..), BlobType (..), ContainerName (..), PutBlob (..))
import Azure.Blob.Types (AccountName (..), BlobName (..), BlobType (..), ContainerName (..))
import Azure.Blob.Utils (blobStorageResourceUrl, mkBlobHostUrl)
import Data.ByteString (ByteString)
import Data.Data (Proxy (..))
import Data.Text (Text)
import GHC.Generics (Generic)
import Network.HTTP.Client.TLS (newTlsManager)
import Servant.API
import Servant.Client (BaseUrl (..), ClientM, Scheme (..), client, mkClientEnv, runClientM)
Expand All @@ -22,6 +25,19 @@ import UnliftIO (MonadIO (..), throwString)
import qualified Azure.Types as Auth
import qualified Data.Text as Text

{- | Adds a blob to a container.
You should have appropriate (Write) permissions in order to perform this operation.
-}
data PutBlob = PutBlob
{ accountName :: !AccountName
, containerName :: !ContainerName
, blobName :: !BlobName
, tokenStore :: !Auth.Token
, body :: !ByteString -- TODO: Add chunked upload
}
deriving stock (Eq, Generic)

{- | Upload a blob to a blob container.
Errors will be thrown in IO. For variant where error is
Expand Down
17 changes: 0 additions & 17 deletions azure-blob-storage/src/Azure/Blob/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Azure.Blob.Types
( BlobName (..)
, ContainerName (..)
, AccountName (..)
, PutBlob (..)
, BlobType (..)
, UserDelegationRequest (..)
, UserDelegationResponse (..)
Expand All @@ -17,16 +16,13 @@ module Azure.Blob.Types
) where

import Data.Aeson (ToJSON (..), object, (.=))
import Data.ByteString (ByteString)
import Data.Text (Text)
import GHC.Generics (Generic)
import Servant.API (ToHttpApiData)
import Xmlbf (FromXml (..), ToXml (..), element, pElement, pText, text)

import qualified Data.HashMap.Strict as HashMap

import qualified Azure.Types as Auth

newtype AccountName = AccountName
{ unAccountName :: Text
}
Expand All @@ -51,19 +47,6 @@ data BlobType
| AppendBlob
deriving stock (Eq, Show, Generic)

{- | Adds a blob to a container.
You should have appropriate (Write) permissions in order to perform this operation.
-}
data PutBlob = PutBlob
{ accountName :: !AccountName
, containerName :: !ContainerName
, blobName :: !BlobName
, tokenStore :: !Auth.Token
, body :: !ByteString -- TODO: Add chunked upload
}
deriving stock (Eq, Generic)

{- | The fields are supposed to be ISO format strings
TODO: make these UTCTime formats
-}
Expand Down

0 comments on commit 6d45c9f

Please sign in to comment.