<commit_before>{-# LANGUAGE OverloadedStrings #-}

module ZoomHub.Rackspace.CloudFiles where

import Data.Aeson as Aeson

import qualified Control.Monad.IO.Class as IO
import qualified Data.ByteString.Lazy as LBS
import qualified Network.HTTP.Conduit as HTTP


-- Types
data Credentials = Credentials
  { username :: String
  , apiKey :: String
  } deriving (Eq, Show)

instance Aeson.ToJSON Credentials where
    toJSON (Credentials username apiKey) =
      Aeson.object ["auth" .=
        Aeson.object ["RAX-KSKEY:apiKeyCredentials" .=
          Aeson.object ["username" .= username, "apiKey" .= apiKey]
        ]
      ]

tokenURL :: String
tokenURL = "https://identity.api.rackspacecloud.com/v2.0/tokens"

getJSON :: String -> -- ^ username
           String -> -- ^ API key
           IO LBS.ByteString
getJSON username apiKey = do
  req <- IO.liftIO $ HTTP.parseUrl tokenURL
  let credentials = Aeson.encode $ Credentials username apiKey
  let req' = req {
              HTTP.method = "POST"
            , HTTP.requestBody = HTTP.RequestBodyLBS credentials
            , HTTP.requestHeaders = [("Content-Type", "application/json")]
            }
  manager <- IO.liftIO $ HTTP.newManager HTTP.tlsManagerSettings
  res <- HTTP.httpLbs req' manager
  return $ HTTP.responseBody res<commit_msg>Implement Rackspace `getToken`<commit_after>{-# LANGUAGE OverloadedStrings #-}

module ZoomHub.Rackspace.CloudFiles where

import Control.Applicative as Applicative
import Data.Aeson as Aeson

import qualified Control.Monad.IO.Class as IO
import qualified Data.ByteString.Lazy as LBS
import qualified Network.HTTP.Conduit as HTTP


-- Types
data Credentials = Credentials
  { username :: String
  , apiKey :: String
  } deriving (Eq, Show)

instance Aeson.ToJSON Credentials where
    toJSON (Credentials username apiKey) =
      Aeson.object ["auth" .=
        Aeson.object ["RAX-KSKEY:apiKeyCredentials" .=
          Aeson.object ["username" .= username, "apiKey" .= apiKey]
        ]
      ]

data Token = Token String deriving (Eq, Show)

instance Aeson.FromJSON Token where
    parseJSON (Object o) =
      Token <$> ((o .: "access") >>= (.: "token") >>= (.: "id"))
    parseJSON _ = empty


-- API
tokenURL :: String
tokenURL = "https://identity.api.rackspacecloud.com/v2.0/tokens"

getToken :: Credentials -> IO (Maybe Token)
getToken credentials = do
  req <- IO.liftIO $ HTTP.parseUrl tokenURL
  let credentialsBS = Aeson.encode $ credentials
  let req' = req {
              HTTP.method = "POST"
            , HTTP.requestBody = HTTP.RequestBodyLBS credentialsBS
            , HTTP.requestHeaders = [("Content-Type", "application/json")]
            }
  manager <- IO.liftIO $ HTTP.newManager HTTP.tlsManagerSettings
  res <- HTTP.httpLbs req' manager
  let maybeToken = decode $ HTTP.responseBody res
  return maybeToken
{'fraction_non_alphanumeric': 0.11071807569862045, 'fraction_numerical': 0.0014149274849663955, 'mean_word_length': 3.0170454545454546, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '794311', 'n_tokens_mistral': 879, 'n_tokens_neox': 841, 'n_words': 227}