2 Module : Gargantext.API.Count
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Count API part of Gargantext.
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE DeriveGeneric #-}
18 {-# LANGUAGE DeriveAnyClass #-}
20 module Gargantext.API.Count
23 import Gargantext.Prelude
25 import Prelude (Bounded, Enum, minBound, maxBound)
27 import Data.Text (Text, pack)
29 import GHC.Generics (Generic)
30 import Data.Aeson hiding (Error)
31 import Test.QuickCheck.Arbitrary
32 import Test.QuickCheck (elements)
33 import Data.List (repeat,permutations)
34 -----------------------------------------------------------------------
35 type CountAPI = Post '[JSON] [Count]
37 -----------------------------------------------------------------------
38 data Scraper = Pubmed | Hal | IsTex | Isidore
39 deriving (Eq, Show, Generic, Enum, Bounded)
42 scrapers = [minBound..maxBound]
44 instance FromJSON Scraper
45 instance ToJSON Scraper
47 instance Arbitrary Scraper where
48 arbitrary = elements scrapers
50 -----------------------------------------------------------------------
51 -----------------------------------------------------------------------
53 data QueryBool = QueryBool Text
54 deriving (Eq, Show, Generic)
56 queries :: [QueryBool]
57 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
59 instance Arbitrary QueryBool where
60 arbitrary = elements queries
62 instance FromJSON QueryBool
63 instance ToJSON QueryBool
67 data Query = Query { query_query :: QueryBool
68 , query_name :: Maybe [Scraper]
70 deriving (Eq, Show, Generic)
71 instance FromJSON Query
73 instance Arbitrary Query where
74 arbitrary = elements [ Query q (Just n)
76 , n <- take 10 $ permutations scrapers
79 -----------------------------------------------------------------------
80 -----------------------------------------------------------------------
81 data ErrorMessage = ErrorMessage Text
82 deriving (Eq, Show, Generic)
84 errorMessages :: [ErrorMessage]
85 errorMessages = map (\m -> ErrorMessage (pack m)) $ [ "Ill formed query "
86 , "API connexion error "
87 , "Internal Gargantext Error "
88 , "Connexion to Gargantext Error"
89 -- , "Token has expired "
90 ] <> take 100 ( repeat ("No Error"))
92 instance Arbitrary ErrorMessage where
93 arbitrary = elements errorMessages
95 instance FromJSON ErrorMessage
96 instance ToJSON ErrorMessage
98 -----------------------------------------------------------------------
99 data Error = Error { error_message :: ErrorMessage
102 deriving (Eq, Show, Generic)
103 instance FromJSON Error
104 instance ToJSON Error
107 errorCodes = [200,300,400,500]
110 errors = [ Error m c | m <- errorMessages
114 instance Arbitrary Error where
115 arbitrary = elements errors
117 -----------------------------------------------------------------------
118 -----------------------------------------------------------------------
119 data Count = Count { count_name :: Scraper
120 , count_count :: Maybe Int
121 , count_errors :: Maybe [Error]
123 deriving (Eq, Show, Generic)
125 instance FromJSON Count
126 instance ToJSON Count
128 instance Arbitrary Count where
129 arbitrary = elements [ Count n (Just c) (Just [e]) | n <- scrapers
135 -----------------------------------------------------------------------
136 count :: Query -> Handler [Count]