Skip to content

Commit

Permalink
use RequestBody for attachment content
Browse files Browse the repository at this point in the history
  • Loading branch information
pufferffish authored and simmsb committed Jul 17, 2023
1 parent b7079e7 commit 6a0efe0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 12 additions & 4 deletions calamity/Calamity/HTTP/Channel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ import Calamity.Types.Model.User
import Calamity.Types.Snowflake
import Data.Aeson qualified as Aeson
import Data.Aeson.KeyMap qualified as K
import Data.ByteString.Lazy (ByteString)
import Data.Default.Class
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Word
import Network.HTTP.Client (RequestBody)
import Network.HTTP.Client.MultipartFormData
import Network.HTTP.Req
import Network.Mime
Expand All @@ -49,9 +49,17 @@ import TextShow
data CreateMessageAttachment = CreateMessageAttachment
{ filename :: Text
, description :: Maybe Text
, content :: ByteString
, content :: RequestBody
}
deriving (Show)

instance Show CreateMessageAttachment where
show (CreateMessageAttachment filename description _) = mconcat
[ "CreateMessageAttachment {filename = "
, show filename
, ", description = "
, show description
, ", content = <body>}"
]

data CreateMessageOptions = CreateMessageOptions
{ content :: Maybe Text
Expand Down Expand Up @@ -418,7 +426,7 @@ instance Request (ChannelRequest a) where
& buildRoute
action (CreateMessage _ cm) = \u o -> do
let filePart CreateMessageAttachment {filename, content} n =
(partLBS @IO (T.pack $ "files[" <> show n <> "]") content)
(partFileRequestBody @IO (T.pack $ "files[" <> show n <> "]") "" content)
{ partFilename = Just (T.unpack filename)
, partContentType = Just (defaultMimeLookup filename)
}
Expand Down
10 changes: 5 additions & 5 deletions calamity/Calamity/HTTP/Interaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ instance Request (InteractionRequest a) where
in postWith' (ReqBodyJson jsonBody)
action (CreateResponseMessage _ _ cm) = \u o -> do
let filePart CreateMessageAttachment {filename, content} n =
(partLBS @IO (T.pack $ "files[" <> show n <> "]") content)
(partFileRequestBody @IO (T.pack $ "files[" <> show n <> "]") "" content)
{ partFilename = Just (T.unpack filename)
, partContentType = Just (defaultMimeLookup filename)
}
Expand Down Expand Up @@ -354,7 +354,7 @@ instance Request (InteractionRequest a) where
postWith' body u o
action (CreateResponseUpdate _ _ cm) = \u o -> do
let filePart CreateMessageAttachment {filename, content} n =
(partLBS @IO (T.pack $ "files[" <> show n <> "]") content)
(partFileRequestBody @IO (T.pack $ "files[" <> show n <> "]") "" content)
{ partFilename = Just (T.unpack filename)
, partContentType = Just (defaultMimeLookup filename)
}
Expand Down Expand Up @@ -399,7 +399,7 @@ instance Request (InteractionRequest a) where
action (GetOriginalInteractionResponse _ _) = getWith
action (EditOriginalInteractionResponse _ _ cm) = \u o -> do
let filePart CreateMessageAttachment {filename, content} n =
(partLBS @IO (T.pack $ "files[" <> show n <> "]") content)
(partFileRequestBody @IO (T.pack $ "files[" <> show n <> "]") "" content)
{ partFilename = Just (T.unpack filename)
, partContentType = Just (defaultMimeLookup filename)
}
Expand Down Expand Up @@ -430,7 +430,7 @@ instance Request (InteractionRequest a) where
action (DeleteOriginalInteractionResponse _ _) = deleteWith
action (CreateFollowupMessage _ _ cm) = \u o -> do
let filePart CreateMessageAttachment {filename, content} n =
(partLBS @IO (T.pack $ "files[" <> show n <> "]") content)
(partFileRequestBody @IO (T.pack $ "files[" <> show n <> "]") "" content)
{ partFilename = Just (T.unpack filename)
, partContentType = Just (defaultMimeLookup filename)
}
Expand All @@ -456,7 +456,7 @@ instance Request (InteractionRequest a) where
action GetFollowupMessage {} = getWith
action (EditFollowupMessage _ _ _ cm) = \u o -> do
let filePart CreateMessageAttachment {filename, content} n =
(partLBS @IO (T.pack $ "files[" <> show n <> "]") content)
(partFileRequestBody @IO (T.pack $ "files[" <> show n <> "]") "" content)
{ partFilename = Just (T.unpack filename)
, partContentType = Just (defaultMimeLookup filename)
}
Expand Down
2 changes: 1 addition & 1 deletion calamity/Calamity/HTTP/Webhook.hs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ instance Request (WebhookRequest a) where
action (DeleteWebhookToken _ _) = deleteWith
action (ExecuteWebhook _ _ wh) = \u o -> do
let filePart CreateMessageAttachment {filename, content} n =
(partLBS @IO (T.pack $ "files[" <> show n <> "]") content)
(partFileRequestBody @IO (T.pack $ "files[" <> show n <> "]") "" content)
{ partFilename = Just (T.unpack filename)
, partContentType = Just (defaultMimeLookup filename)
}
Expand Down

0 comments on commit 6a0efe0

Please sign in to comment.