From 4818b8518e0ab84eccdfce52f4043208a4da0561 Mon Sep 17 00:00:00 2001 From: Steffen Jost Date: Thu, 23 Jun 2022 19:10:38 +0200 Subject: [PATCH] chore(avs): add basic servant api for person search --- src/Handler/Utils.hs | 2 + src/Handler/Utils/Servant/AVS.hs | 72 ++++++++++++++++++++++++++++++++ src/Utils.hs | 14 +++++++ 3 files changed, 88 insertions(+) create mode 100644 src/Handler/Utils/Servant/AVS.hs diff --git a/src/Handler/Utils.hs b/src/Handler/Utils.hs index 5d635e60e..e65d1a41b 100644 --- a/src/Handler/Utils.hs +++ b/src/Handler/Utils.hs @@ -29,6 +29,8 @@ import Handler.Utils.AuthorshipStatement as Handler.Utils import Handler.Utils.Term as Handler.Utils +import Handler.Utils.Servant.AVS as Handler.Utils + import Control.Monad.Logger diff --git a/src/Handler/Utils/Servant/AVS.hs b/src/Handler/Utils/Servant/AVS.hs new file mode 100644 index 000000000..afcb50a4e --- /dev/null +++ b/src/Handler/Utils/Servant/AVS.hs @@ -0,0 +1,72 @@ +{-# OPTIONS_GHC -fno-warn-orphans #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeOperators #-} + +module Handler.Utils.Servant.AVS where + +import Import +import Servant +import Servant.Client +import qualified Network.HTTP.Client as HTTP (newManager, defaultManagerSettings) + +data AvsPersonQuery = AvsPersonQuery + { avsPersonQueryCardNo :: Maybe String + , avsPersonQueryFirstName :: Maybe String + , avsPersonQueryLastName :: Maybe String + , avsPersonQueryInternalPersonalNo :: Maybe String + , avsPersonQueryVersionNo :: Maybe String + } + deriving (Eq, Ord, Read, Show, Generic, Typeable) + +instance Default AvsPersonQuery where + def = AvsPersonQuery Nothing Nothing Nothing Nothing Nothing + +deriveJSON defaultOptions + { fieldLabelModifier = mconcat . drop 3 . splitCamel + , omitNothingFields = True + , tagSingleConstructors = True + } ''AvsPersonQuery + +{- + data PersonResponse = Person {..TODO..} + data StatusResponse = StatusResponse +-} + + -- data StatusQuery = StatusQuery +newtype AvsStatusQuery = AvsStatusQuery (Set UserMatriculation) +deriveJSON defaultOptions ''AvsStatusQuery + +type AvsPersonResponse = Value +type AvsStatusResponse = Value + +type AVS = BasicAuth "avs_fradrive" String :> "FraVSMService" :> "v1" :> "PersonSearch" :> ReqBody '[JSON] AvsPersonQuery :> Post '[JSON] AvsPersonResponse + -- :<|> ("PersonStatus" :> ReqBody '[JSON] AvsStatusQuery :> Post '[JSON] AvsStatusResponse)) + +avsApi :: Proxy AVS +avsApi = Proxy + + +avsPersonSearch :: BasicAuthData -> AvsPersonQuery -> ClientM AvsPersonResponse +-- avsPersonStatus :: BasicAuthData -> AvsStatusQuery -> ClientM AvsStatusResponse +--(avsPersonSearch :<|> avsPersonStatus) = client avsApi +avsPersonSearch = client avsApi + +avsServer :: BaseUrl +avsServer = BaseUrl + { baseUrlScheme = Https + , baseUrlHost = "skytest.fra.fraport.de" + , baseUrlPort = 80 + , baseUrlPath = "" + } + + +run :: IO () +run = do + manager' <- HTTP.newManager HTTP.defaultManagerSettings + let query = avsPersonSearch (BasicAuthData "foo" "bar") $ def { avsPersonQueryFirstName = Just "Steffen" } + res <- runClientM query (mkClientEnv manager' avsServer) + case res of + Left err -> putStrLn $ "Error: " ++ tshow err + Right resp -> do + print resp \ No newline at end of file diff --git a/src/Utils.hs b/src/Utils.hs index 9408b2d94..36087b0cb 100644 --- a/src/Utils.hs +++ b/src/Utils.hs @@ -388,6 +388,20 @@ stepTextCounter text fromText :: (IsString a, Textual t) => t -> a fromText = fromString . unpack +{- +-- | Captialize the first character and leave all others as they were +textToCapital :: Text -> Text +textToCapital s + | Just (h,t) <- Text.uncons s + = Text.Cons (Char.toUpper h) t + | otherwise = s + +snakecase2camelcase :: Text -> Text +snakecase2camelcase t = Text.concat $ map textToCapital words + where + words = Text.splitOn '_' t +-} + ----------- -- Fixed --