]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Count.hs
[API][AUTH] if authorized then id of tree is given (with token).
[gargantext.git] / src / Gargantext / API / Count.hs
1 {-|
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
8 Portability : POSIX
9
10 Count API part of Gargantext.
11 -}
12
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 {-# LANGUAGE DeriveGeneric #-}
20 {-# LANGUAGE DeriveAnyClass #-}
21 {-# LANGUAGE OverloadedStrings #-}
22
23 module Gargantext.API.Count
24 where
25
26
27 import GHC.Generics (Generic)
28 import Prelude (Bounded, Enum, minBound, maxBound)
29
30 import Data.Aeson hiding (Error)
31 import Data.Aeson.TH (deriveJSON)
32 import Data.Eq (Eq())
33 import Data.Either
34 import Data.List (repeat, permutations)
35 import Data.Swagger
36 import Data.Text (Text, pack)
37
38 import Servant
39 import Test.QuickCheck.Arbitrary
40 import Test.QuickCheck (elements)
41 -- import Control.Applicative ((<*>))
42
43 import Gargantext.Prelude
44 import Gargantext.Core.Utils.Prefix (unPrefix)
45
46 -----------------------------------------------------------------------
47 type CountAPI = Post '[JSON] Counts
48
49 -----------------------------------------------------------------------
50 data Scraper = Pubmed | Hal | IsTex | Isidore
51 deriving (Eq, Show, Generic, Enum, Bounded)
52
53 scrapers :: [Scraper]
54 scrapers = [minBound..maxBound]
55
56 instance FromJSON Scraper
57 instance ToJSON Scraper
58
59 instance Arbitrary Scraper where
60 arbitrary = elements scrapers
61
62 instance ToSchema Scraper
63
64 -----------------------------------------------------------------------
65 data QueryBool = QueryBool Text
66 deriving (Eq, Show, Generic)
67
68 queries :: [QueryBool]
69 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
70
71 instance Arbitrary QueryBool where
72 arbitrary = elements queries
73
74 instance FromJSON QueryBool
75 instance ToJSON QueryBool
76
77 instance ToSchema QueryBool
78 -----------------------------------------------------------------------
79
80 data Query = Query { query_query :: QueryBool
81 , query_name :: Maybe [Scraper]
82 }
83 deriving (Eq, Show, Generic)
84 instance FromJSON Query
85 instance ToJSON Query
86
87 instance Arbitrary Query where
88 arbitrary = elements [ Query q (Just n)
89 | q <- queries
90 , n <- take 10 $ permutations scrapers
91 ]
92
93 instance ToSchema Query
94 -----------------------------------------------------------------------
95 type Code = Integer
96 type Error = Text
97 type Errors = [Error]
98
99 -----------------------------------------------------------------------
100 data Message = Message Code Errors
101 deriving (Eq, Show, Generic)
102
103 toMessage :: [(Code, Errors)] -> [Message]
104 toMessage = map (\(c,err) -> Message c err)
105
106 messages :: [Message]
107 messages = toMessage $ [ (400, ["Ill formed query "])
108 , (300, ["API connexion error "])
109 , (300, ["Internal Gargantext Error "])
110 ] <> take 10 ( repeat (200, [""]))
111
112 instance Arbitrary Message where
113 arbitrary = elements messages
114
115 instance FromJSON Message
116 instance ToJSON Message
117
118 instance ToSchema Message
119 -----------------------------------------------------------------------
120 data Counts = Counts { results :: [Either Message Count]
121 } deriving (Eq, Show, Generic)
122
123
124 instance FromJSON Counts
125 instance ToJSON Counts
126
127 instance Arbitrary Counts where
128 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
129 , Right (Count IsTex (Just 150))
130 , Right (Count Hal (Just 150))
131 ]
132 ]
133
134 instance ToSchema Counts
135
136 -----------------------------------------------------------------------
137 data Count = Count { count_name :: Scraper
138 , count_count :: Maybe Int
139 }
140 deriving (Eq, Show, Generic)
141
142 $(deriveJSON (unPrefix "count_") ''Count)
143
144 instance ToSchema Count
145 --instance Arbitrary Count where
146 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
147
148 -----------------------------------------------------------------------
149 count :: Query -> Handler Counts
150 count _ = undefined