Skip to content

Commit

Permalink
Nicities
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinprakash96 committed Aug 4, 2024
1 parent c0bea6a commit d30d752
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 35 deletions.
7 changes: 4 additions & 3 deletions azure-blob-storage/azure-blob-storage.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ common common-options
library
import: common-options
exposed-modules: Azure.Blob
Azure.Blob.GetBlob
Azure.Blob.DeleteBlob
Azure.Blob.Types
Azure.Blob.UserDelegationKey
Azure.Blob.GetBlob
Azure.Blob.PutBlob
Azure.Blob.Types
Azure.Blob.SharedAccessSignature
Azure.Blob.UserDelegationKey
Azure.Blob.Utils
build-depends: azure-auth
, aeson
, base64-bytestring
Expand Down
15 changes: 7 additions & 8 deletions azure-blob-storage/src/Azure/Blob.hs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
module Azure.Blob
(
-- ** Variants to fetch a blob object
( -- ** Variants to fetch a blob object
getBlobObject
, getBlobObjectEither

-- ** Variants to upload a blob to Blob storage
-- ** Variants to upload a blob to Blob storage
, putBlobObject
, putBlobObjectEither

-- ** Variants for deleting a blob object
-- ** Variants for deleting a blob object
, deleteBlobObject
, deleteBlobObjectEither

-- ** Generating a Shared Access Signature URI
-- ** Generating a Shared Access Signature URI
, generateSas

-- ** Types for dealing with Blob storage functions
-- ** Types for dealing with Blob storage functions
, AccountName (..)
, ContainerName (..)
, BlobName (..)
) where

import Azure.Blob.PutBlob
import Azure.Blob.GetBlob
import Azure.Blob.DeleteBlob
import Azure.Blob.GetBlob
import Azure.Blob.PutBlob
import Azure.Blob.SharedAccessSignature
import Azure.Blob.Types
8 changes: 2 additions & 6 deletions azure-blob-storage/src/Azure/Blob/DeleteBlob.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Azure.Blob.DeleteBlob

import Azure.Auth (defaultAzureCredential)
import Azure.Blob.Types (AccountName (..), BlobName (..), ContainerName (..))
import Azure.Blob.Utils (blobStorageResourceUrl, mkBlobHostUrl)
import Data.Data (Proxy (..))
import Data.Text (Text)
import GHC.Generics (Generic)
Expand All @@ -23,9 +24,6 @@ import UnliftIO (MonadIO (..), throwString)
import qualified Azure.Types as Auth
import qualified Data.Text as Text

blobStorageResourceUrl :: Text
blobStorageResourceUrl = "https://storage.azure.com/"

deleteBlobObject ::
MonadIO m =>
DeleteBlob ->
Expand Down Expand Up @@ -80,11 +78,9 @@ callDeleteBlobClient action DeleteBlob{accountName, containerName, blobName, tok
liftIO $
runClientM
(action containerName blobName ("Bearer " <> atAccessToken) "2020-04-08")
(mkClientEnv manager $ BaseUrl Https mkHostUrl 443 "")
(mkClientEnv manager $ BaseUrl Https (mkBlobHostUrl accountName) 443 "")
pure $ case res of
Left err -> do
Left . Text.pack $ show err
Right _ -> do
pure ()
where
mkHostUrl = Text.unpack (unAccountName accountName) <> ".blob.core.windows.net"
8 changes: 2 additions & 6 deletions azure-blob-storage/src/Azure/Blob/GetBlob.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ 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.Text as Text
import qualified Network.HTTP.Media as M

blobStorageResourceUrl :: Text
blobStorageResourceUrl = "https://storage.azure.com/"

getBlobObject ::
MonadIO m =>
GetBlob ->
Expand Down Expand Up @@ -106,11 +104,9 @@ callGetBlobClient action GetBlob{accountName, containerName, blobName, tokenStor
liftIO $
runClientM
(action containerName blobName ("Bearer " <> atAccessToken) "2020-04-08")
(mkClientEnv manager $ BaseUrl Https mkHostUrl 443 "")
(mkClientEnv manager $ BaseUrl Https (mkBlobHostUrl accountName) 443 "")
pure $ case res of
Left err -> do
Left . Text.pack $ show err
Right response -> do
Right response
where
mkHostUrl = Text.unpack (unAccountName accountName) <> ".blob.core.windows.net"
10 changes: 3 additions & 7 deletions azure-blob-storage/src/Azure/Blob/PutBlob.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module Azure.Blob.PutBlob
) where

import Azure.Auth (defaultAzureCredential)
import Azure.Blob.Types (AccountName (..), BlobName (..), BlobType (..), ContainerName (..), PutBlob (..))
import Azure.Blob.Types (BlobName (..), BlobType (..), ContainerName (..), PutBlob (..))
import Azure.Blob.Utils (blobStorageResourceUrl, mkBlobHostUrl)
import Data.ByteString (ByteString)
import Data.Data (Proxy (..))
import Data.Text (Text)
Expand All @@ -21,9 +22,6 @@ import UnliftIO (MonadIO (..), throwString)
import qualified Azure.Types as Auth
import qualified Data.Text as Text

blobStorageResourceUrl :: Text
blobStorageResourceUrl = "https://storage.azure.com/"

{- | Upload a blob to a blob container.
Errors will be thrown in IO. For variant where error is
Expand Down Expand Up @@ -79,11 +77,9 @@ callPutBlobClient action PutBlob{accountName, containerName, blobName, tokenStor
liftIO $
runClientM
(action containerName blobName ("Bearer " <> atAccessToken) "2020-04-08" (Text.pack $ show BlockBlob) body)
(mkClientEnv manager $ BaseUrl Https mkHostUrl 443 "")
(mkClientEnv manager $ BaseUrl Https (mkBlobHostUrl accountName) 443 "")
pure $ case res of
Left err ->
Left . Text.pack $ show err
Right _ ->
Right ()
where
mkHostUrl = Text.unpack (unAccountName accountName) <> ".blob.core.windows.net"
4 changes: 1 addition & 3 deletions azure-blob-storage/src/Azure/Blob/SharedAccessSignature.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Azure.Blob.Types
, sasResourceToText
)
import Azure.Blob.UserDelegationKey (callGetUserDelegationKeyApi, getUserDelegationKeyApi)
import Azure.Blob.Utils (blobStorageResourceUrl)
import Crypto.Hash.SHA256 (hmac)
import Data.Text (Text)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
Expand All @@ -33,9 +34,6 @@ import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Char8 as C8
import qualified Data.Text as Text

blobStorageResourceUrl :: Text
blobStorageResourceUrl = "https://storage.azure.com/"

-- TODO: We need to add support for empty fields here. Eg: signedAuthorizedUserObjectId
generateSas ::
MonadIO m =>
Expand Down
4 changes: 2 additions & 2 deletions azure-blob-storage/src/Azure/Blob/UserDelegationKey.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Azure.Blob.Types
, UserDelegationRequest (..)
, UserDelegationResponse (..)
)
import Azure.Blob.Utils (mkBlobHostUrl)
import Data.Data (Proxy (..))
import Data.Text (Text)
import Network.HTTP.Client.TLS (newTlsManager)
Expand Down Expand Up @@ -57,7 +58,7 @@ callGetUserDelegationKeyApi action accountName Auth.AccessToken{atAccessToken} r
liftIO $
runClientM
(action showResType showComp ("Bearer " <> atAccessToken) "2022-11-02" req)
(mkClientEnv manager $ BaseUrl Https mkHostUrl 443 "")
(mkClientEnv manager $ BaseUrl Https (mkBlobHostUrl accountName) 443 "")
pure $ case res of
Left err ->
Left . Text.pack $ show err
Expand All @@ -66,4 +67,3 @@ callGetUserDelegationKeyApi action accountName Auth.AccessToken{atAccessToken} r
where
showComp = "userdelegationkey"
showResType = "service"
mkHostUrl = Text.unpack (unAccountName accountName) <> ".blob.core.windows.net"
15 changes: 15 additions & 0 deletions azure-blob-storage/src/Azure/Blob/Utils.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Azure.Blob.Utils
( blobStorageResourceUrl
, mkBlobHostUrl
) where

import Azure.Blob.Types (AccountName (..))
import Data.Text (Text)

import qualified Data.Text as Text

blobStorageResourceUrl :: Text
blobStorageResourceUrl = "https://storage.azure.com/"

mkBlobHostUrl :: AccountName -> String
mkBlobHostUrl (AccountName accountName) = Text.unpack accountName <> ".blob.core.windows.net"

0 comments on commit d30d752

Please sign in to comment.