2 Module : Gargantext.API.Routes
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# LANGUAGE ConstraintKinds #-}
16 {-# LANGUAGE TemplateHaskell #-}
17 {-# LANGUAGE TypeOperators #-}
18 {-# LANGUAGE KindSignatures #-}
19 {-# LANGUAGE ScopedTypeVariables #-}
20 {-# LANGUAGE TypeFamilies #-}
21 {-# LANGUAGE UndecidableInstances #-}
23 ---------------------------------------------------------------------
24 module Gargantext.API.Routes
26 ---------------------------------------------------------------------
27 import Control.Concurrent (threadDelay)
28 import Data.Text (Text)
30 import Gargantext.API.Admin.Auth (AuthRequest, AuthResponse, AuthenticatedUser(..), withAccess, PathId(..))
31 import Gargantext.API.Admin.FrontEnd (FrontEndAPI)
32 import Gargantext.API.Prelude
33 import Gargantext.API.Count (CountAPI, count, Query)
34 import Gargantext.API.Ngrams (TableNgramsApi, apiNgramsTableDoc)
35 import Gargantext.API.Node
36 import Gargantext.API.Search (SearchPairsAPI, searchPairs)
37 import Gargantext.Core.Types.Individu (User(..))
38 import Gargantext.Database.Query.Table.Node.Contact (HyperdataContact)
39 import Gargantext.Database.Admin.Types.Node
40 import Gargantext.Database.Admin.Types.Node (NodeId, CorpusId, AnnuaireId)
41 import Gargantext.Prelude
42 import Gargantext.Viz.Graph.API
44 import Servant.Auth as SA
45 import Servant.Auth.Swagger ()
46 import Servant.Job.Async
47 import Servant.Swagger.UI
48 import qualified Gargantext.API.Node.Corpus.Annuaire as Annuaire
49 import qualified Gargantext.API.Node.Corpus.Export as Export
50 import qualified Gargantext.API.Node.Corpus.New as New
51 import qualified Gargantext.API.Ngrams.List as List
55 type GargAPI = "api" :> Summary "API " :> GargAPIVersion
56 -- | TODO :<|> Summary "Latest API" :> GargAPI'
59 type GargAPIVersion = "v1.0"
60 :> Summary "Garg API Version "
63 type GargVersion = "version"
64 :> Summary "Backend version"
69 "auth" :> Summary "AUTH API"
70 :> ReqBody '[JSON] AuthRequest
71 :> Post '[JSON] AuthResponse
73 -- TODO-ACCESS here we want to request a particular header for
74 -- auth and capabilities.
78 type GargPrivateAPI = SA.Auth '[SA.JWT, SA.Cookie] AuthenticatedUser :> GargPrivateAPI'
82 = "user" :> Summary "First user endpoint"
84 :<|> "nodes" :> Summary "Nodes endpoint"
85 :> ReqBody '[JSON] [NodeId] :> NodesAPI
87 type GargPrivateAPI' =
91 :<|> "node" :> Summary "Node endpoint"
92 :> Capture "node_id" NodeId
93 :> NodeAPI HyperdataAny
96 :<|> "corpus" :> Summary "Corpus endpoint"
97 :> Capture "corpus_id" CorpusId
98 :> NodeAPI HyperdataCorpus
100 :<|> "corpus" :> Summary "Corpus endpoint"
101 :> Capture "node1_id" NodeId
103 :> Capture "node2_id" NodeId
104 :> NodeNodeAPI HyperdataAny
106 :<|> "corpus" :> Capture "node_id" CorpusId
110 :<|> "annuaire" :> Summary "Annuaire endpoint"
111 :> Capture "annuaire_id" AnnuaireId
112 :> NodeAPI HyperdataAnnuaire
114 :<|> "annuaire" :> Summary "Contact endpoint"
115 :> Capture "annuaire_id" NodeId
117 :> Capture "contact_id" NodeId
118 :> NodeNodeAPI HyperdataContact
121 :<|> "document" :> Summary "Document endpoint"
122 :> Capture "doc_id" DocId
123 :> "ngrams" :> TableNgramsApi
125 -- :<|> "counts" :> Stream GET NewLineFraming '[JSON] Count :> CountAPI
127 :<|> "count" :> Summary "Count endpoint"
128 :> ReqBody '[JSON] Query
131 -- Corpus endpoint --> TODO rename s/search/filter/g
132 :<|> "search" :> Capture "corpus" NodeId
135 -- TODO move to NodeAPI?
136 :<|> "graph" :> Summary "Graph endpoint"
137 :> Capture "graph_id" NodeId
140 -- TODO move to NodeAPI?
142 :<|> "tree" :> Summary "Tree endpoint"
143 :> Capture "tree_id" NodeId
148 :<|> New.AddWithQuery
150 -- :<|> "annuaire" :> Annuaire.AddWithForm
151 -- :<|> New.AddWithFile
152 -- :<|> "scraper" :> WithCallbacks ScraperAPI
153 -- :<|> "new" :> New.Api
155 :<|> "lists" :> Summary "List export API"
156 :> Capture "listId" ListId
159 :<|> "wait" :> Summary "Wait test"
161 :> WaitAPI -- Get '[JSON] Int
167 -- :<|> "list" :> Capture "node_id" Int :> NodeAPI
168 -- :<|> "ngrams" :> Capture "node_id" Int :> NodeAPI
169 -- :<|> "auth" :> Capture "node_id" Int :> NodeAPI
170 ---------------------------------------------------------------------
172 type API = SwaggerAPI
176 -- | API for serving @swagger.json@
177 type SwaggerAPI = SwaggerSchemaUI "swagger-ui" "swagger.json"
179 -- | API for serving main operational routes of @gargantext.org@
185 -- :<|> "list" :> Capture "node_id" Int :> NodeAPI
186 -- :<|> "ngrams" :> Capture "node_id" Int :> NodeAPI
187 -- :<|> "auth" :> Capture "node_id" Int :> NodeAPI
188 ---------------------------------------------------------------------
190 ---------------------------------------------------------------------
191 -- | Server declarations
193 -- TODO-SECURITY admin only: withAdmin
194 -- Question: How do we mark admins?
195 serverGargAdminAPI :: GargServer GargAdminAPI
196 serverGargAdminAPI = roots
200 serverPrivateGargAPI' :: AuthenticatedUser -> GargServer GargPrivateAPI'
201 serverPrivateGargAPI' (AuthenticatedUser (NodeId uid))
203 :<|> nodeAPI (Proxy :: Proxy HyperdataAny) uid
204 :<|> nodeAPI (Proxy :: Proxy HyperdataCorpus) uid
205 :<|> nodeNodeAPI (Proxy :: Proxy HyperdataAny) uid
206 :<|> Export.getCorpus -- uid
207 :<|> nodeAPI (Proxy :: Proxy HyperdataAnnuaire) uid
208 :<|> nodeNodeAPI (Proxy :: Proxy HyperdataContact) uid
210 :<|> withAccess (Proxy :: Proxy TableNgramsApi) Proxy uid
211 <$> PathNode <*> apiNgramsTableDoc
213 :<|> count -- TODO: undefined
215 :<|> withAccess (Proxy :: Proxy SearchPairsAPI) Proxy uid
216 <$> PathNode <*> searchPairs -- TODO: move elsewhere
218 :<|> withAccess (Proxy :: Proxy GraphAPI) Proxy uid
219 <$> PathNode <*> graphAPI uid -- TODO: mock
221 :<|> withAccess (Proxy :: Proxy TreeAPI) Proxy uid
222 <$> PathNode <*> treeAPI
224 :<|> addCorpusWithForm (UserDBId uid)
225 :<|> addCorpusWithQuery (RootId (NodeId uid))
227 -- :<|> addAnnuaireWithForm
228 -- :<|> New.api uid -- TODO-SECURITY
229 -- :<|> New.info uid -- TODO-SECURITY
234 ----------------------------------------------------------------------
236 type WaitAPI = Get '[JSON] Text
238 waitAPI :: Int -> GargServer WaitAPI
241 m = (10 :: Int) ^ (6 :: Int)
242 _ <- liftBase $ threadDelay ( m * n)
243 pure $ "Waited: " <> (cs $ show n)
244 ----------------------------------------
247 addCorpusWithQuery :: User -> GargServer New.AddWithQuery
248 addCorpusWithQuery user cid =
250 JobFunction (\q log ->
253 printDebug "addToCorpusWithQuery" x
255 in New.addToCorpusWithQuery user cid q log'
259 addWithFile :: GargServer New.AddWithFile
260 addWithFile cid i f =
262 JobFunction (\_i log -> New.addToCorpusWithFile cid i f (liftBase . log))
265 addCorpusWithForm :: User -> GargServer New.AddWithForm
266 addCorpusWithForm user cid =
268 JobFunction (\i log ->
271 printDebug "addToCorpusWithForm" x
273 in New.addToCorpusWithForm user cid i log')
275 addAnnuaireWithForm :: GargServer Annuaire.AddWithForm
276 addAnnuaireWithForm cid =
278 JobFunction (\i log -> Annuaire.addToAnnuaireWithForm cid i (liftBase . log))