diff --git a/package.yaml b/package.yaml index 7a5befb2e83891c4fd39e0efcaa4a97b2403e983..9f2b10685c04ef48a1ca6a537bf49b01d9f2407b 100644 --- a/package.yaml +++ b/package.yaml @@ -38,6 +38,7 @@ library: exposed-modules: - Gargantext - Gargantext.API + - Gargantext.API.HashedResponse - Gargantext.API.Node - Gargantext.API.Admin.Settings - Gargantext.API.Prelude diff --git a/src/Gargantext/API/HashedResponse.hs b/src/Gargantext/API/HashedResponse.hs new file mode 100644 index 0000000000000000000000000000000000000000..b5d05ae8b3a5a41516b8163958e1cbbc92439e76 --- /dev/null +++ b/src/Gargantext/API/HashedResponse.hs @@ -0,0 +1,19 @@ +module Gargantext.API.HashedResponse where + +import Data.Aeson +import Data.Swagger +import qualified Data.Digest.Pure.MD5 as DPMD5 +import GHC.Generics (Generic) +import Protolude + +data HashedResponse a = HashedResponse { md5 :: Text, value :: a } + deriving (Generic) + +instance ToSchema a => ToSchema (HashedResponse a) +instance ToJSON a => ToJSON (HashedResponse a) where + toJSON = genericToJSON defaultOptions + +constructHashedResponse :: ToJSON a => a -> HashedResponse a +constructHashedResponse v = HashedResponse { md5 = md5', value = v } + where + md5' = show $ DPMD5.md5 $ encode v \ No newline at end of file diff --git a/src/Gargantext/API/Metrics.hs b/src/Gargantext/API/Metrics.hs index 698784811425e545b9c4493cdcaf7489b49c485f..cb4b79b5020a46a78e096d62912377edfa3f1180 100644 --- a/src/Gargantext/API/Metrics.hs +++ b/src/Gargantext/API/Metrics.hs @@ -19,15 +19,12 @@ module Gargantext.API.Metrics where import Control.Lens -import Data.Aeson -import qualified Data.Digest.Pure.MD5 as DPMD5 -import Data.Swagger import Data.Time (UTCTime) -import GHC.Generics (Generic) import Protolude import Servant import qualified Data.Map as Map +import Gargantext.API.HashedResponse import Gargantext.API.Ngrams import Gargantext.API.Ngrams.NTree import Gargantext.API.Prelude (GargServer) @@ -46,18 +43,6 @@ import Gargantext.Text.Metrics (Scored(..)) import Gargantext.Viz.Chart import Gargantext.Viz.Types -data HashedResponse a = HashedResponse { md5 :: Text, value :: a } - deriving (Generic) - -instance ToSchema a => ToSchema (HashedResponse a) -instance ToJSON a => ToJSON (HashedResponse a) where - toJSON = genericToJSON defaultOptions - -constructHashedResponse :: ToJSON a => a -> HashedResponse a -constructHashedResponse chart = HashedResponse { md5 = md5', value = chart } - where - md5' = show $ DPMD5.md5 $ encode chart - ------------------------------------------------------------- -- | Scatter metrics API type ScatterAPI = Summary "SepGen IncExc metrics" diff --git a/src/Gargantext/API/Ngrams.hs b/src/Gargantext/API/Ngrams.hs index c4a2b8891a2e50786f96dfc115b0415d011d75d7..45a5ab4e17c8e2aa577e72bd08b241f8d551ec6f 100644 --- a/src/Gargantext/API/Ngrams.hs +++ b/src/Gargantext/API/Ngrams.hs @@ -148,7 +148,7 @@ data TabType = Docs | Trash | MoreFav | MoreTrash deriving (Generic, Enum, Bounded, Show) instance FromHttpApiData TabType - where + where parseUrlPiece "Docs" = pure Docs parseUrlPiece "Trash" = pure Trash parseUrlPiece "MoreFav" = pure MoreFav diff --git a/src/Gargantext/API/Table.hs b/src/Gargantext/API/Table.hs index be2f5ff8fab3e4fb8c4fc8e461f8e6b8929a1117..a3ec6d8c7617d23ffadcaec308b468faf55146de 100644 --- a/src/Gargantext/API/Table.hs +++ b/src/Gargantext/API/Table.hs @@ -36,7 +36,13 @@ import Data.Maybe import Data.Swagger import Data.Text (Text()) import GHC.Generics (Generic) +import Servant +import Test.QuickCheck (elements) +import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary) + +import Gargantext.API.HashedResponse import Gargantext.API.Ngrams (TabType(..)) +import Gargantext.API.Prelude (GargServer) import Gargantext.Core.Types (Offset, Limit, TableResult(..)) import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger) import Gargantext.Database.Query.Facet (FacetDoc , runViewDocuments, OrderBy(..), runViewAuthorsDoc) @@ -45,15 +51,19 @@ import Gargantext.Database.Action.Search import Gargantext.Database.Admin.Types.Node import Gargantext.Database.Prelude -- (Cmd, CmdM) import Gargantext.Prelude -import Servant -import Test.QuickCheck (elements) -import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary) ------------------------------------------------------------------------ -type TableApi = Summary " Table API" +type TableApi = Summary "Table API" + :> QueryParam "tabType" TabType + :> Get '[JSON] (HashedResponse FacetTableResult) + :<|> Summary "Table API (POST)" :> ReqBody '[JSON] TableQuery :> Post '[JSON] FacetTableResult + :<|> "md5" :> + Summary "Table md5" + :> QueryParam "tabType" TabType + :> Get '[JSON] Text data TableQuery = TableQuery { tq_offset :: Int @@ -74,13 +84,30 @@ instance Arbitrary TableQuery where arbitrary = elements [TableQuery 0 10 DateAsc Docs "electrodes"] -tableApi :: NodeId -> TableQuery -> Cmd err FacetTableResult -tableApi cId (TableQuery o l order ft "") = getTable cId (Just ft) (Just o) (Just l) (Just order) -tableApi cId (TableQuery o l order ft q) = case ft of +tableApi :: NodeId -> GargServer TableApi +tableApi id' = getTableApi id' + :<|> postTableApi id' + :<|> getTableMd5Api id' + + +getTableApi :: NodeId -> Maybe TabType -> Cmd err (HashedResponse FacetTableResult) +getTableApi cId tabType = do + t <- getTable cId tabType Nothing Nothing Nothing + pure $ constructHashedResponse t + + +postTableApi :: NodeId -> TableQuery -> Cmd err FacetTableResult +postTableApi cId (TableQuery o l order ft "") = getTable cId (Just ft) (Just o) (Just l) (Just order) +postTableApi cId (TableQuery o l order ft q) = case ft of Docs -> searchInCorpus' cId False [q] (Just o) (Just l) (Just order) Trash -> searchInCorpus' cId True [q] (Just o) (Just l) (Just order) x -> panic $ "not implemented in tableApi " <> (cs $ show x) +getTableMd5Api :: NodeId -> Maybe TabType -> Cmd err Text +getTableMd5Api cId tabType = do + HashedResponse { md5 = md5' } <- getTableApi cId tabType + pure md5' + searchInCorpus' :: CorpusId -> Bool -> [Text]