<commit_before>{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE OverloadedStrings         #-}

module ZoomHub.API.Types.NonRESTfulResponse
  ( NonRESTfulResponse
  , mkNonRESTful200
  , mkNonRESTful301
  ) where

import           Data.Aeson                (ToJSON, object, toJSON, (.=))
import qualified Data.Text                 as T
import           Data.Text.Encoding        (decodeUtf8)
import           Network.HTTP.Types.Status (Status, movedPermanently301, ok200,
                                            statusCode, statusMessage)
import           Network.URI               (URI)

data NonRESTfulResponse a = ToJSON a => NonRESTfulResponse
  { nrrStatus           :: Status
  , nrrBodyKey          :: String
  , nrrBody             :: a
  , nrrRedirectLocation :: Maybe URI
  }

mkNonRESTful200 :: ToJSON a => String -> a -> NonRESTfulResponse a
mkNonRESTful200 key body = NonRESTfulResponse
  { nrrStatus = ok200
  , nrrBodyKey = key
  , nrrBody = body
  , nrrRedirectLocation = Nothing
  }

mkNonRESTful301 :: ToJSON a => String -> a -> URI -> NonRESTfulResponse a
mkNonRESTful301 key body redirectLocation = NonRESTfulResponse
  { nrrStatus = movedPermanently301
  , nrrBodyKey = key
  , nrrBody = body
  , nrrRedirectLocation = Just redirectLocation
  }


instance ToJSON a => ToJSON (NonRESTfulResponse a) where
  toJSON r = object
      [ "status" .= statusCode status
      , "statusText" .= decodeUtf8 (statusMessage status)
      , bodyKey .= toJSON (nrrBody r)
      ]
    where
      bodyKey = T.pack (nrrBodyKey r)
      status = nrrStatus r<commit_msg>:art: Add section separator<commit_after>{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE OverloadedStrings         #-}

module ZoomHub.API.Types.NonRESTfulResponse
  ( NonRESTfulResponse
  , mkNonRESTful200
  , mkNonRESTful301
  ) where

import           Data.Aeson                (ToJSON, object, toJSON, (.=))
import qualified Data.Text                 as T
import           Data.Text.Encoding        (decodeUtf8)
import           Network.HTTP.Types.Status (Status, movedPermanently301, ok200,
                                            statusCode, statusMessage)
import           Network.URI               (URI)

data NonRESTfulResponse a = ToJSON a => NonRESTfulResponse
  { nrrStatus           :: Status
  , nrrBodyKey          :: String
  , nrrBody             :: a
  , nrrRedirectLocation :: Maybe URI
  }

mkNonRESTful200 :: ToJSON a => String -> a -> NonRESTfulResponse a
mkNonRESTful200 key body = NonRESTfulResponse
  { nrrStatus = ok200
  , nrrBodyKey = key
  , nrrBody = body
  , nrrRedirectLocation = Nothing
  }

mkNonRESTful301 :: ToJSON a => String -> a -> URI -> NonRESTfulResponse a
mkNonRESTful301 key body redirectLocation = NonRESTfulResponse
  { nrrStatus = movedPermanently301
  , nrrBodyKey = key
  , nrrBody = body
  , nrrRedirectLocation = Just redirectLocation
  }

-- JSON
instance ToJSON a => ToJSON (NonRESTfulResponse a) where
  toJSON r = object
      [ "status" .= statusCode status
      , "statusText" .= decodeUtf8 (statusMessage status)
      , bodyKey .= toJSON (nrrBody r)
      ]
    where
      bodyKey = T.pack (nrrBodyKey r)
      status = nrrStatus r
{'fraction_non_alphanumeric': 0.08346407279573266, 'fraction_numerical': 0.02008158142453718, 'mean_word_length': 2.409625668449198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2129607', 'n_tokens_mistral': 1020, 'n_tokens_neox': 941, 'n_words': 241}