<commit_before>module APIBuilder.Routes
  ( URLFragment
  , URLParam
  , Route(..)
  , HTTPMethod(..)
  , routeURL
  , (=.) ) where

import Control.Arrow ((***))
import Data.Maybe (mapMaybe)
import Data.Monoid ((<>))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Network.HTTP.Base as HTTP (urlEncodeVars)

type URLFragment = Text
type URLParam = (Text, Maybe Text)

(=.) :: Text -> Maybe Text -> (Text, Maybe Text)
(=.) = (,)

data Route = Route { fragments :: [URLFragment]
                   , urlParams :: [URLParam]
                   , httpMethod :: HTTPMethod }
  deriving (Show, Read, Eq)

data HTTPMethod = GET | POST
  deriving (Show, Read, Eq)

routeURL :: Text -> Route -> Text
routeURL baseURL (Route fs ps _) =
  let path = T.intercalate "/" fs
  in baseURL <> "/" <> path <> "/?" <> buildParams ps

buildParams :: [URLParam] -> Text
buildParams = T.pack . HTTP.urlEncodeVars . map (T.unpack *** T.unpack) . mapMaybe collectParams

collectParams :: URLParam -> Maybe (Text, Text)
collectParams (a, Just x) = Just (a,x)
collectParams (_, Nothing) = Nothing<commit_msg>fixed sometimes adding a trailing slash to urls that didnt want one<commit_after>module APIBuilder.Routes
  ( URLFragment
  , URLParam
  , Route(..)
  , HTTPMethod(..)
  , routeURL
  , (=.) ) where

import Control.Arrow ((***))
import Data.Maybe (mapMaybe)
import Data.Monoid ((<>))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Network.HTTP.Base as HTTP (urlEncodeVars)

type URLFragment = Text
type URLParam = (Text, Maybe Text)

(=.) :: Text -> Maybe Text -> (Text, Maybe Text)
(=.) = (,)

data Route = Route { fragments :: [URLFragment]
                   , urlParams :: [URLParam]
                   , httpMethod :: HTTPMethod }
  deriving (Show, Read, Eq)

data HTTPMethod = GET | POST
  deriving (Show, Read, Eq)

routeURL :: Text -> Route -> Text
routeURL baseURL (Route fs ps _) =
  let path = T.intercalate "/" fs
  in baseURL <> "/" <> path <> pathParamsSep fs <> buildParams ps

pathParamsSep :: [URLFragment] -> Text
pathParamsSep [] = "?"
pathParamsSep xs = if T.isInfixOf "." (last xs) then "?" else "/?"

buildParams :: [URLParam] -> Text
buildParams = T.pack . HTTP.urlEncodeVars . map (T.unpack *** T.unpack) . mapMaybe collectParams

collectParams :: URLParam -> Maybe (Text, Text)
collectParams (a, Just x) = Just (a,x)
collectParams (_, Nothing) = Nothing
{'fraction_non_alphanumeric': 0.14688796680497926, 'fraction_numerical': 0.0, 'mean_word_length': 3.351985559566787, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4859099', 'n_tokens_mistral': 776, 'n_tokens_neox': 762, 'n_words': 238}