Browse Source

New Util file to load an API key from the environment/file system

tags/v0.2.2.0
Peter J. Jones 6 years ago
parent
commit
3c7271bfb2
3 changed files with 85 additions and 4 deletions
  1. 0
    2
      Network/API/TheMovieDB/Types.hs
  2. 81
    0
      Network/API/TheMovieDB/Util.hs
  3. 4
    2
      themoviedb.cabal

+ 0
- 2
Network/API/TheMovieDB/Types.hs View File

@@ -1,5 +1,3 @@
{-# LANGUAGE OverloadedStrings #-}

{-

This file is part of the Haskell package themoviedb. It is subject to

+ 81
- 0
Network/API/TheMovieDB/Util.hs View File

@@ -0,0 +1,81 @@
{-

This file is part of the Haskell package themoviedb. It is subject to
the license terms in the LICENSE file found in the top-level directory
of this distribution and at git://pmade.com/themoviedb/LICENSE. No
part of themoviedb package, including this file, may be copied,
modified, propagated, or distributed except according to the terms
contained in the LICENSE file.

-}
module Network.API.TheMovieDB.Util
( loadKey
, loadConfig
) where

import Control.Monad (liftM, msum)
import Data.Char (isSpace)
import Network.API.TheMovieDB.HTTP
import Network.API.TheMovieDB.Types
import System.Environment (getEnv)
import System.IO.Error (isDoesNotExistError)
import System.Posix.Files
import System.Posix.User

-- Fetch the value of an environment variable without an exception.
getEnvMaybe :: String -> IO (Maybe String)
getEnvMaybe n = env `catch` err
where env = liftM Just $ getEnv n
err e = if isDoesNotExistError e
then return Nothing
else ioError e

-- Expand @~@ at the front of a file path.
expandFile :: FilePath -> IO FilePath
expandFile ('~':rest) = do userID <- getEffectiveUserID
entry <- getUserEntryForID userID
return $ homeDirectory entry ++ rest
expandFile dir = return dir

-- Return the contents of a file if it exists, otherwise Nothing.
readFileMaybe :: FilePath -> IO (Maybe String)
readFileMaybe n = do realName <- expandFile n
exists <- fileExist realName
if exists
then liftM Just (readFirstLine realName)
else return Nothing
where readFirstLine = liftM skipSpace . readFile
skipSpace = filter $ not . isSpace


-- FIXME: does sequence interfere with what I'm trying to do with
-- msum? That is, return the first non-Nothing value.
--
-- | Fetch an API 'Key' from the first of:
--
-- * @TMDB_KEY@ environment variable
--
-- * @XDG_CONFIG_HOME/tmdbkey@ file (where XDG_CONFIG_HOME is
-- an environment variable)
--
-- * @~\/.config\/tmdbkey@ file
--
-- * @~/.tmdbkey@ file
--
-- If the key can't be loaded from any of those places the result
-- will be Nothing.
loadKey :: IO (Maybe Key)
loadKey = liftM msum . sequence $ [env, xdgConfig, config, home]
where env = getEnvMaybe "TMDB_KEY"
config = readFileMaybe "~/.config/tmdbkey"
home = readFileMaybe "~/.tmdbkey"
xdgConfig = do dir <- getEnvMaybe "XDG_CONFIG_HOME"
case dir of
Nothing -> return Nothing
Just d -> readFileMaybe (d ++ "/tmdbkey")

-- | Uses 'loadKey' to fetch an API 'Key' and wrap it into a default
-- 'Config' using 'mkConfig'.
loadConfig :: IO (Maybe Config)
loadConfig = do key <- loadKey
return $ maybe Nothing (Just . mkConfig) key

+ 4
- 2
themoviedb.cabal View File

@@ -17,7 +17,8 @@ build-type: Simple
cabal-version: >=1.8

library
exposed-modules: Network.API.TheMovieDB
exposed-modules: Network.API.TheMovieDB,
Network.API.TheMovieDB.Util
other-modules: Network.API.TheMovieDB.Fetch,
Network.API.TheMovieDB.Search,
Network.API.TheMovieDB.HTTP,
@@ -36,7 +37,8 @@ library
network ==2.3.*,
bytestring ==0.9.*,
text ==0.11.*,
aeson ==0.6.*
aeson ==0.6.*,
unix ==2.4.*

source-repository head
type: git

Loading…
Cancel
Save