Ends.purs 9.9 KB
Newer Older
1 2 3 4 5
-- | Those things at the end of urls
module Gargantext.Ends
  -- ( )
  where

6
import Data.Argonaut (class DecodeJson, decodeJson, class EncodeJson, (:=), (~>), jsonEmptyObject, (.:))
7 8 9
import Data.Foldable (foldMap)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
10
import Data.Maybe (Maybe(..), maybe, fromMaybe)
11
import Gargantext.Routes as R
12
import Gargantext.Types (ApiVersion, ChartType(..), Limit, NodePath, NodeType(..), Offset, TabType(..), TermSize(..), nodePath, nodeTypePath, showTabType', TermList(MapTerm))
13
import Gargantext.Prelude (class Eq, class Show, identity, show, ($), (<>), bind, pure, (<<<), (==), (/=))
14 15 16 17 18 19 20 21 22 23 24 25 26

-- | A means of generating a url to visit, a destination
class ToUrl conf p where
  toUrl :: conf -> p -> String

url :: forall conf p. ToUrl conf p => conf -> p -> String
url = toUrl

-- | Encapsulates the data we need to talk to a backend server
newtype Backend = Backend
  { name    :: String
  , baseUrl :: String
  , prePath :: String
27 28
  , version :: ApiVersion
  }
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

backend :: ApiVersion -> String -> String -> String -> Backend
backend version prePath baseUrl name = Backend { name, version, prePath, baseUrl }

-- | Creates a backend url from a backend and the path as a string
backendUrl :: Backend -> String -> String
backendUrl (Backend b) path = b.baseUrl <> b.prePath <> show b.version <> "/" <> path

derive instance genericBackend :: Generic Backend _

instance eqBackend :: Eq Backend where
  eq = genericEq

instance showBackend :: Show Backend where
  show (Backend {name}) = name

instance toUrlBackendString :: ToUrl Backend String where
  toUrl = backendUrl

48 49 50 51 52 53 54 55 56 57 58 59
-- JSON instances
instance encodeJsonBackend :: EncodeJson Backend where
  encodeJson (Backend {name, baseUrl, prePath, version})
    =  "name"    := name
    ~> "baseUrl" := baseUrl
    ~> "prePath" := prePath
    ~> "version" := show version
    ~> jsonEmptyObject

instance decodeJsonBackend :: DecodeJson Backend where
  decodeJson json = do
    obj <- decodeJson json
60
    name <- obj .: "name"
61 62 63 64 65
    baseUrl <- obj .: "baseUrl"
    prePath <- obj .: "prePath"
    version <- obj .: "version"
    pure $ Backend {name, baseUrl, prePath, version}

66 67 68 69 70 71 72 73 74 75 76 77
-- | Encapsulates the data needed to construct a url to a frontend
-- | server (either for the app or static content)
newtype Frontend = Frontend
  { name    :: String
  , baseUrl :: String
  , prePath :: String }

derive instance genericFrontend :: Generic Frontend _

instance eqFrontend :: Eq Frontend where
  eq = genericEq

78 79 80
instance toUrlFrontendNodePath :: ToUrl Frontend NodePath where
  toUrl front np = frontendUrl front (nodePath np)

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
-- | Creates a frontend
frontend :: String -> String -> String -> Frontend
frontend baseUrl prePath name = Frontend { name, baseUrl, prePath }

-- | Creates a url from a frontend and the path as a string
frontendUrl :: Frontend -> String -> String
frontendUrl (Frontend f) path = f.baseUrl <> f.prePath <> path

instance showFrontend :: Show Frontend where
  show (Frontend {name}) = name

instance toUrlFrontendString :: ToUrl Frontend String where
  toUrl = frontendUrl

instance toUrlFrontendAppRoute :: ToUrl Frontend R.AppRoute where
  toUrl f r = frontendUrl f (R.appPath r)

-- | The currently selected App and Static configurations
newtype Frontends = Frontends { app :: Frontend, static :: Frontend }

101 102
derive instance eqFrontends :: Eq Frontends

James Laver's avatar
James Laver committed
103 104 105 106 107 108
instance toUrlFrontendsRoutes :: ToUrl Frontends R.AppRoute where
  toUrl f r = appUrl f (R.appPath r)

instance toUrlFrontendsNodePath :: ToUrl Frontends NodePath where
  toUrl (Frontends {app}) np = frontendUrl app (nodePath np)

109 110 111 112 113 114 115 116 117
-- | Creates an app url from a Frontends and the path as a string
appUrl :: Frontends -> String -> String
appUrl (Frontends {app}) = frontendUrl app

-- | Creates a static url from a Frontends and the path as a string
staticUrl :: Frontends -> String -> String
staticUrl (Frontends {static}) = frontendUrl static

sessionPath :: R.SessionRoute -> String
118 119
sessionPath (R.Tab t i)             = sessionPath (R.NodeAPI Node i (showTabType' t))
sessionPath (R.Children n o l s i)  = sessionPath (R.NodeAPI Node i ("children?type=" <> show n <> offsetUrl o <> limitUrl l <> orderUrl s))
120
sessionPath (R.NodeAPI Phylo pId p) = "phyloscape?nodeId=" <> (show $ fromMaybe 0 pId) <> p
121 122 123 124 125 126 127
sessionPath (R.RecomputeNgrams nt nId lId)      = "node/" <> (show nId) <> "/ngrams/recompute?" <> (defaultList lId) <> "&ngramsType=" <> (show nt)
sessionPath (R.RecomputeListChart ChartBar nt nId lId)   = "node/" <> (show nId) <> "/pie?" <> (defaultList lId) <> "&ngramsType=" <> (show nt)
sessionPath (R.RecomputeListChart ChartPie nt nId lId)   = "node/" <> (show nId) <> "/pie?" <> (defaultList lId) <> "&ngramsType=" <> (show nt)
sessionPath (R.RecomputeListChart ChartTree nt nId lId)  = "node/" <> (show nId) <> "/tree?" <> (defaultList lId) <> "&ngramsType=" <> (show nt) <> "&listType=" <> show MapTerm
sessionPath (R.RecomputeListChart Histo nt nId lId)      = "node/" <> (show nId) <> "/chart?" <> (defaultList lId) <> "&ngramsType=" <> (show nt)
sessionPath (R.RecomputeListChart Scatter nt nId lId)    = "node/" <> (show nId) <> "/metrics?" <> (defaultList lId) <> "&ngramsType=" <> (show nt)
sessionPath (R.RecomputeListChart _ nt nId lId)          = "node/" <> (show nId) <> "/recompute-chart?" <> (defaultList lId) <> "&ngramsType=" <> (show nt)
128
sessionPath (R.GraphAPI gId p)      = "graph/" <> (show gId) <> "/" <> p
129
sessionPath (R.GetNgrams opts i)    =
130
  base opts.tabType
131 132 133
     $ "ngrams?ngramsType="
    <> showTabType' opts.tabType
    <> limitUrl opts.limit
134
    <> offset opts.offset
135
    <> orderByUrl opts.orderBy
136
    <> foldMap (\x -> if x /= 0 then "&list=" <> show x else "") opts.listIds
137 138 139
    <> foldMap (\x -> "&listType=" <> show x) opts.termListFilter
    <> foldMap termSizeFilter opts.termSizeFilter
    <> search opts.searchQuery
140
  where
141 142
    base (TabCorpus _) = sessionPath <<< R.NodeAPI Node i
    base _             = sessionPath <<< R.NodeAPI Url_Document i
143 144
    offset Nothing = ""
    offset (Just o) = offsetUrl o
145 146 147 148
    termSizeFilter MonoTerm = "&minTermSize=0&maxTermSize=1"
    termSizeFilter MultiTerm = "&minTermSize=2"
    search "" = ""
    search s = "&search=" <> s
149 150 151 152 153 154
sessionPath (R.GetNgramsTableAll opts i) =
  sessionPath $ R.NodeAPI Node i
     $ "ngrams?ngramsType="
    <> showTabType' opts.tabType
    <> foldMap (\x -> "&list=" <> show x) opts.listIds
    <> limitUrl 100000
155 156 157 158 159
sessionPath (R.GetNgramsTableVersion opts i) =
  sessionPath $ R.NodeAPI Node i
     $ "ngrams/version?ngramsType="
    <> showTabType' opts.tabType
    <> "&list=" <> show opts.listId
160
sessionPath (R.ListDocument lId dId) =
161
  sessionPath $ R.NodeAPI NodeList lId ("document/" <> (show $ fromMaybe 0 dId))
162
sessionPath (R.ListsRoute lId) = "lists/" <> show lId
163
sessionPath (R.PutNgrams t listId termList i) =
164 165 166 167 168 169 170 171
  sessionPath $ R.NodeAPI Node i
      $ "ngrams?ngramsType="
     <> showTabType' t
     <> maybe "" (\x -> "&list=" <> show x) listId
     <> foldMap (\x -> "&listType=" <> show x) termList
sessionPath (R.NodeAPI nt i p) = nodeTypePath nt
                              <> (maybe "" (\i' -> "/" <> show i') i)
                              <> (if p == "" then "" else "/" <> p)
172 173 174 175 176 177 178 179
sessionPath (R.Search {listId, limit, offset, orderBy} Nothing) =
  sessionPath $ R.NodeAPI Corpus Nothing
     $ "search?list_id=" <> show listId
    <> offsetUrl offset
    <> limitUrl limit
    <> orderUrl orderBy
sessionPath (R.Search {listId, limit, offset, orderBy} (Just corpusId)) =
  sessionPath $ R.NodeAPI Corpus (Just corpusId)
180
     $ "search?list_id=" <> show listId
181 182 183
    <> offsetUrl offset
    <> limitUrl limit
    <> orderUrl orderBy
184 185 186 187 188
-- sessionPath (R.Search {listId, limit, offset, orderBy} (Just corpusId)) =
--     "search/" <> (show corpusId) <> "/list/" <> (show listId) <> "?"
--     <> offsetUrl offset
--     <> limitUrl limit
--     <> orderUrl orderBy
189
sessionPath (R.CorpusMetrics { listId, limit, tabType} i) =
190 191 192 193
  sessionPath $ R.NodeAPI Corpus i
     $ "metrics"
    <> "?ngrams=" <> show listId
    <> "&ngramsType=" <> showTabType' tabType
Nicolas Pouillard's avatar
Nicolas Pouillard committed
194
    <> maybe "" limitUrl limit
195
sessionPath (R.CorpusMetricsHash { listId, tabType} i) =
196
  sessionPath $ R.NodeAPI Corpus i
197
     $ "metrics/hash"
198 199
    <> "?ngrams=" <> show listId
    <> "&ngramsType=" <> showTabType' tabType
200
-- TODO fix this url path
201
sessionPath (R.Chart {chartType, limit, listId, tabType} i) =
202 203 204
  sessionPath $ R.NodeAPI Corpus i
     $ show chartType
    <> "?ngramsType=" <> showTabType' tabType
205 206
    <> "&listType=" <> show MapTerm  -- listId
    <> defaultListAddMaybe listId
207 208 209 210
    where
      limitPath = case limit of
        Just li -> "&limit=" <> show li
        Nothing -> ""
Nicolas Pouillard's avatar
Nicolas Pouillard committed
211
    -- <> maybe "" limitUrl limit
212
sessionPath (R.ChartHash { chartType, listId, tabType } i) =
213 214
  sessionPath $ R.NodeAPI Corpus i
     $ show chartType
215
    <> "/hash?ngramsType=" <> showTabType' tabType
216 217
    <> "&listType=" <> show MapTerm -- listId
    <> defaultListAddMaybe listId
218
-- sessionPath (R.NodeAPI (NodeContact s a i) i) = sessionPath $ "annuaire/" <> show a <> "/contact/" <> show i
219 220 221

------- misc routing stuff

222 223 224 225 226 227
defaultList :: Int -> String
defaultList n = if n == 0 then "" else ("list=" <> show n)

defaultListAdd :: Int -> String
defaultListAdd n = "&" <> defaultList n

228 229 230 231
defaultListAddMaybe :: Maybe Int -> String
defaultListAddMaybe Nothing = ""
defaultListAddMaybe (Just l) = "&list=" <> show l

232 233


234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
limitUrl :: Limit -> String
limitUrl l = "&limit=" <> show l

offsetUrl :: Offset -> String
offsetUrl o = "&offset=" <> show o

orderUrl :: forall a. Show a => Maybe a -> String
orderUrl = maybe "" (\x -> "&order=" <> show x)

orderByUrl :: forall a. Show a => Maybe a -> String
orderByUrl = maybe "" (\x -> "&orderBy=" <> show x)

-- nodeTypePath :: NodeType -> Path
-- nodeTypePath = NodeAPI

-- instance toUrlNodeType :: ToUrl NodeType where
--   toUrl ec e nt i = toUrl ec e (NodeAPI nt) i

-- instance toUrlPath :: ToUrl Path where
--   toUrl ec e p i = doUrl base path params
--     where
--       base   = endBaseUrl e ec
--       path   = endPathUrl e ec p i
--       params = ""
------------------------------------------------------------