Routes.hs 3.6 KB
Newer Older
1 2
{-# LANGUAGE OverloadedStrings    #-}
{-# LANGUAGE TypeOperators #-}
3
{-# LANGUAGE TypeApplications #-}
4
{-# LANGUAGE ViewPatterns #-}
5 6 7 8 9


module Test.API.Routes where

import Fmt (Builder, (+|), (|+))
10
import Gargantext.API.Admin.Auth.Types (AuthRequest, AuthResponse, Token)
11
import Gargantext.API.Ngrams.Types ( NgramsTable, NgramsTablePatch, OrderBy, TabType, Versioned, VersionedWithCount )
12
import Gargantext.API.Routes.Named
13 14
import Gargantext.API.Routes.Named.Table
import Gargantext.Core.Types (ListId, NodeId)
15 16 17 18
import Gargantext.Core.Types.Main (ListType)
import Gargantext.Core.Types.Query (Limit, MaxSize, MinSize, Offset)
import Gargantext.Prelude
import Network.Wai.Handler.Warp (Port)
19 20
import Servant.Client (ClientM)
import Servant.Client.Generic ( genericClient, AsClientT )
21 22 23 24 25 26
import Gargantext.API.Types () -- MimeUnrender instances
import Gargantext.API.Errors
import Gargantext.API.Routes.Named.Private hiding (tableNgramsAPI)
import Gargantext.API.Routes.Named.Node
import qualified Servant.Auth.Client as S
import qualified Data.Text.Encoding as TE
27 28 29 30 31 32 33 34 35 36

-- This is for requests made by http.client directly to hand-crafted URLs    
curApi :: Builder
curApi = "v1.0"

mkUrl :: Port -> Builder -> ByteString
mkUrl _port urlPiece =
  "/api/" +| curApi |+ urlPiece


37 38 39 40 41
-- | The client for the full API. It also serves as a \"proof\" that our
-- whole API has all the required instances to be used in a client.
clientRoutes :: API (AsClientT ClientM)
clientRoutes = genericClient

42 43 44

-- This is for Servant.Client requests
auth_api :: AuthRequest -> ClientM AuthResponse
45 46 47 48 49 50 51 52
auth_api = clientRoutes & apiWithCustomErrorScheme
                        & ($ GES_new)
                        & backendAPI
                        & backendAPI'
                        & mkBackEndAPI
                        & gargAPIVersion
                        & gargAuthAPI
                        & authEp
53

54 55 56
table_ngrams_get_api :: Token
                     -> NodeId
                     -> TabType
57
                     -> ListId
58 59 60 61 62 63 64 65
                     -> Limit
                     -> Maybe Offset
                     -> Maybe ListType
                     -> Maybe MinSize
                     -> Maybe MaxSize
                     -> Maybe OrderBy
                     -> Maybe Text
                     -> ClientM (VersionedWithCount NgramsTable)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
table_ngrams_get_api (toServantToken -> token) nodeId =
  clientRoutes & apiWithCustomErrorScheme
               & ($ GES_new)
               & backendAPI
               & backendAPI'
               & mkBackEndAPI
               & gargAPIVersion
               & gargPrivateAPI
               & mkPrivateAPI
               & ($ token)
               & nodeEp
               & nodeEndpointAPI
               & ($ nodeId)
               & tableNgramsAPI
               & tableNgramsGetAPI
               & getNgramsTableEp

toServantToken :: Token -> S.Token
toServantToken = S.Token . TE.encodeUtf8
85

86 87 88
table_ngrams_put_api :: Token
                     -> NodeId
                     -> TabType
89
                     -> ListId
90 91
                     -> Versioned NgramsTablePatch
                     -> ClientM (Versioned NgramsTablePatch)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
table_ngrams_put_api (toServantToken -> token) nodeId =
  clientRoutes & apiWithCustomErrorScheme
               & ($ GES_new)
               & backendAPI
               & backendAPI'
               & mkBackEndAPI
               & gargAPIVersion
               & gargPrivateAPI
               & mkPrivateAPI
               & ($ token)
               & nodeEp
               & nodeEndpointAPI
               & ($ nodeId)
               & tableNgramsAPI
               & tableNgramsPutAPI
               & putNgramsTableEp