<commit_before>{-# LANGUAGE OverloadedStrings #-}
module BT.Routing(route) where

import Data.Map
import BT.EndPoints
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import Data.ByteString.Lazy.Char8 (pack)
import Network.Wai (Request)
import BT.Types (PersistentConns)
import Text.JSON (toJSObject, encode)

jstostring :: (Request -> PersistentConns -> IO [(String, String)]) -> Request -> PersistentConns -> IO BL.ByteString
jstostring obj r p= do
    resp <- obj r p
    return . pack . encode . toJSObject $ resp

router :: Map B.ByteString (Request -> PersistentConns -> IO BL.ByteString)
router = Data.Map.fromList $ [
        ("/register", jstostring BT.EndPoints.register),
        ("/balance", jstostring BT.EndPoints.getBalance),
        ("/withdraw", jstostring BT.EndPoints.sendBTC),
        ("/deposit", jstostring BT.EndPoints.deposit),
        ("/request", jstostring BT.EndPoints.createPayment),
        ("/mine", BT.EndPoints.mine),
        ("/", BT.EndPoints.mine),
        ("/pay", jstostring BT.EndPoints.makePayment)]

route :: B.ByteString -> Request -> PersistentConns -> IO BL.ByteString
route path info conns = case Data.Map.lookup path router of
                Nothing -> return "{\"error\":\"No Such Method\"}"
                Just a -> do
                    putStrLn $ "Handling" ++ (show path)
                    resp <- a info conns
                    putStrLn $ "Handled" ++ (show resp)
                    return resp
<commit_msg>use aeson instead of json for encoding<commit_after>{-# LANGUAGE OverloadedStrings #-}
module BT.Routing(route) where

import Data.Map
import BT.EndPoints
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import Network.Wai (Request)
import BT.Types (PersistentConns)
import Data.Aeson.Encode (encode)

jstostring :: (Request -> PersistentConns -> IO [(String, String)]) -> Request -> PersistentConns -> IO BL.ByteString
jstostring obj r p= do
    resp <- obj r p
    return . encode . Data.Map.fromList $ resp

router :: Map B.ByteString (Request -> PersistentConns -> IO BL.ByteString)
router = Data.Map.fromList $ [
        ("/register",   jstostring BT.EndPoints.register),
        ("/balance",    jstostring BT.EndPoints.getBalance),
        ("/withdraw",   jstostring BT.EndPoints.sendBTC),
        ("/deposit",    jstostring BT.EndPoints.deposit),
        ("/request",    jstostring BT.EndPoints.createPayment),
        ("/mine",       BT.EndPoints.mine),
        ("/",           BT.EndPoints.mine),
        ("/pay",        jstostring BT.EndPoints.makePayment)]

route :: B.ByteString -> Request -> PersistentConns -> IO BL.ByteString
route path info conns = case Data.Map.lookup path router of
                Nothing -> return "{\"error\":\"No Such Method\"}"
                Just a -> do
                    putStrLn $ "Handling" ++ (show path)
                    resp <- a info conns
                    putStrLn $ "Handled" ++ (show resp)
                    return resp

{'fraction_non_alphanumeric': 0.1259946949602122, 'fraction_numerical': 0.0003315649867374005, 'mean_word_length': 2.516317016317016, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 27, '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': '3474913', 'n_tokens_mistral': 919, 'n_tokens_neox': 884, 'n_words': 249}