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

Some updates and fixes #133

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ cabal.sandbox.config
*.iml
dist
dist-newstyle
stack.yaml.lock
35 changes: 25 additions & 10 deletions src/Servant/Client/MultipartFormData.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
Expand All @@ -20,9 +21,9 @@ import Control.Monad.IO.Class
import Control.Monad.Reader.Class
import Data.Bifunctor (bimap)
import Data.Binary.Builder (toLazyByteString)
import Data.ByteString.Lazy hiding (pack, any)
import Data.Proxy
import Data.ByteString.Lazy hiding (any, pack)
import qualified Data.List.NonEmpty as NonEmpty
import Data.Proxy
import qualified Data.Sequence as Sequence
import Data.Text (pack)
import Data.Typeable (Typeable)
Expand All @@ -34,10 +35,15 @@ import Network.HTTP.Types
import qualified Network.HTTP.Types as H
import qualified Network.HTTP.Types.Header as HTTP
import Servant.API
import Servant.Client
import Servant.Client (BaseUrl,
ClientError (DecodeFailure, FailureResponse, InvalidContentTypeHeader, UnsupportedContentType),
ClientM, HasClient,
baseUrl,
makeClientRequest,
manager)
import qualified Servant.Client.Core as Core
import Servant.Client.Internal.HttpClient (catchConnectionError, clientResponseToResponse,
requestToClientRequest)
import Servant.Client.Internal.HttpClient (catchConnectionError,
clientResponseToResponse)

-- | A type that can be converted to a multipart/form-data value.
class ToMultipartFormData a where
Expand All @@ -50,12 +56,21 @@ data MultipartFormDataReqBody a

instance (Core.RunClient m, ToMultipartFormData b, MimeUnrender ct a, cts' ~ (ct ': cts)
) => HasClient m (MultipartFormDataReqBody b :> Post cts' a) where
type Client m (MultipartFormDataReqBody b :> Post cts' a) = b-> ClientM a
clientWithRoute _pm Proxy req reqData =

type Client m (MultipartFormDataReqBody b :> Post cts' a) = b -> ClientM a

clientWithRoute _pm Proxy req reqData = do
clientEnv <- ask
let requestToClientRequest' req' baseurl' = do
let requestWithoutBody = requestToClientRequest baseurl' req'
requestWithoutBody <-
#if ! MIN_VERSION_servant_client(0,20,0)
pure $
#endif
makeClientRequest clientEnv baseurl' req'
formDataBody (toMultipartFormData reqData) requestWithoutBody
in snd <$> performRequestCT' requestToClientRequest' (Proxy :: Proxy ct) H.methodPost req
snd <$> performRequestCT' requestToClientRequest' (Proxy :: Proxy ct) H.methodPost req

hoistClientMonad _ _ _ _ = error "unreachable"

-- copied `performRequest` from servant-0.11, then modified so it takes a variant of `requestToClientRequest`
-- as an argument.
Expand Down Expand Up @@ -104,5 +119,5 @@ performRequestCT' requestToClientRequest' ct reqMethod req = do
let coreResponse = clientResponseToResponse id _response
unless (any (matches respCT) acceptCTS) $ throwError $ UnsupportedContentType respCT coreResponse
case mimeUnrender ct respBody of
Left err -> throwError $ DecodeFailure (pack err) coreResponse
Left err -> throwError $ DecodeFailure (pack err) coreResponse
Right val -> return (hdrs, val)
1 change: 0 additions & 1 deletion src/Web/Telegram/API/Bot/API.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API
Expand Down
1 change: 0 additions & 1 deletion src/Web/Telegram/API/Bot/API/Chats.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API.Chats
Expand Down
10 changes: 6 additions & 4 deletions src/Web/Telegram/API/Bot/API/Core.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API.Core
( -- * Types
Expand All @@ -24,7 +23,10 @@ import Control.Monad.Trans.Reader
import Data.Text (Text)
import Network.HTTP.Client (Manager)
import Servant.API
import Servant.Client
import Servant.Client (BaseUrl (BaseUrl), ClientEnv,
ClientError, ClientM,
Scheme (Https), mkClientEnv,
runClientM)

-- | Telegram Bot's Token
newtype Token = Token Text
Expand All @@ -44,7 +46,7 @@ runClient' tcm token = runClientM (runReaderT tcm token)

-- | Runs 'TelegramClient'
runClient :: TelegramClient a -> Token -> Manager -> IO (Either ClientError a)
runClient tcm token manager = runClient' tcm token (ClientEnv manager telegramBaseUrl Nothing)
runClient tcm token manager = runClient' tcm token (mkClientEnv manager telegramBaseUrl)

-- | Runs 'TelegramClient'
runTelegramClient :: Token -> Manager -> TelegramClient a -> IO (Either ClientError a)
Expand All @@ -54,7 +56,7 @@ asking :: Monad m => (t -> m b) -> ReaderT t m b
asking op = ask >>= \t -> lift $ op t

run :: BaseUrl -> (Token -> a -> ClientM b) -> Token -> a -> Manager -> IO (Either ClientError b)
run b e t r m = runClientM (e t r) (ClientEnv m b Nothing)
run b e t r m = runClientM (e t r) (mkClientEnv m b)

run_ :: Monad m => (a -> b -> m c) -> b -> ReaderT a m c
run_ act request = asking $ flip act request
Expand Down
1 change: 0 additions & 1 deletion src/Web/Telegram/API/Bot/API/Edit.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API.Edit
Expand Down
1 change: 0 additions & 1 deletion src/Web/Telegram/API/Bot/API/Get.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API.Get
Expand Down
1 change: 0 additions & 1 deletion src/Web/Telegram/API/Bot/API/Messages.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API.Messages
Expand Down
1 change: 0 additions & 1 deletion src/Web/Telegram/API/Bot/API/Payments.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API.Payments
Expand Down
1 change: 0 additions & 1 deletion src/Web/Telegram/API/Bot/API/Queries.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API.Queries
Expand Down
1 change: 0 additions & 1 deletion src/Web/Telegram/API/Bot/API/Updates.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Web.Telegram.API.Bot.API.Updates
Expand Down
Loading