Skip to content

Commit

Permalink
Modernize Nix configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
supki committed Jun 10, 2015
1 parent 750da9b commit bfd66e7
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 34 deletions.
21 changes: 2 additions & 19 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,2 @@
{ haskellPackages ? (import <nixpkgs> {}).haskellPackages }:

haskellPackages.cabal.mkDerivation (self: rec {
pname = "liblastfm";
version = "0.5.1";
src = builtins.filterSource (_: type: type != "unknown") ./.;
buildDepends = with haskellPackages; [
aeson cereal cryptoApi httpClient httpClientTls networkUri
profunctors pureMD5 semigroups text xmlConduit
];
testDepends = with haskellPackages; buildDepends ++ [
hspec hspecExpectationsLens HUnit lens lensAeson xmlHtmlConduitLens
];
meta = {
description = "Lastfm API interface";
license = self.stdenv.lib.licenses.mit;
platforms = self.ghc.meta.platforms;
};
})
{ nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7101" }:
nixpkgs.pkgs.haskell.packages.${compiler}.callPackage ./liblastfm.nix {}
2 changes: 1 addition & 1 deletion example/recommendations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ main = withConnection $ \conn -> do
mapM_ Text.putStrLn (concat r)

-- Construct signed query
query :: Connection -> Request JSON (APIKey -> SessionKey -> Sign) -> IO (Either LastfmError Value)
query :: Connection -> Request 'JSON (APIKey -> SessionKey -> Sign) -> IO (Either LastfmError Value)
query conn r = lastfm conn $ sign secret (r <*> ak <*> sk <* json)
where
ak = apiKey "__YOUR_API_KEY__"
Expand Down
2 changes: 1 addition & 1 deletion example/sort-friends.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ forConcurrently = flip mapConcurrently
pretty :: [(Text, Score)] -> IO ()
pretty = mapM_ (\(n,s) -> Text.putStrLn $ n <> ": " <> s) . take 5 . sortBy (flip compare `on` snd)

query :: Connection -> Request JSON (APIKey -> Ready) -> IO (Either LastfmError Value)
query :: Connection -> Request 'JSON (APIKey -> Ready) -> IO (Either LastfmError Value)
query conn r = lastfm conn (r <*> apiKey "234fc6e0f41f6ef99b7bd62ebaf8d318" <* json)

target :: Text
Expand Down
24 changes: 24 additions & 0 deletions liblastfm.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ mkDerivation, aeson, base, bytestring, cereal, containers
, crypto-api, hspec, hspec-expectations-lens, http-client
, http-client-tls, HUnit, lens, lens-aeson, network-uri
, profunctors, pureMD5, semigroups, stdenv, text, xml-conduit
, xml-html-conduit-lens
}:
mkDerivation {
pname = "liblastfm";
version = "0.5.1";
src = ./.;
buildDepends = [
aeson base bytestring cereal containers crypto-api http-client
http-client-tls network-uri profunctors pureMD5 semigroups text
xml-conduit
];
testDepends = [
aeson base bytestring cereal containers crypto-api hspec
hspec-expectations-lens http-client http-client-tls HUnit lens
lens-aeson network-uri profunctors pureMD5 text xml-conduit
xml-html-conduit-lens
];
description = "Lastfm API interface";
license = stdenv.lib.licenses.mit;
}
17 changes: 17 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7101" }: let
inherit (nixpkgs) pkgs;
ghc = pkgs.haskell.packages.${compiler}.ghcWithPackages( ps: with ps; [
hdevtools doctest
]);
cabal-install = pkgs.haskell.packages.${compiler}.cabal-install;
pkg = (import ./default.nix { inherit nixpkgs compiler; });
in
pkgs.stdenv.mkDerivation rec {
name = pkg.pname;
buildInputs = [ ghc cabal-install ] ++ pkg.env.buildInputs;
shellHook = ''
${pkg.env.shellHook}
export IN_WHICH_NIX_SHELL=${name}
cabal configure --package-db=$NIX_GHC_LIBDIR/package.conf.d
'';
}
13 changes: 6 additions & 7 deletions src/Network/Lastfm/Request.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ module Network.Lastfm.Request
, Targeted, comparison, Scrobble, LibraryAlbum, LibraryArtist
) where

import Control.Applicative
import Data.Int (Int64)
import Data.Monoid ((<>), mempty)

import Control.Applicative
import Data.Int (Int64)
import Data.Monoid ((<>), mempty)
import qualified Data.Map.Strict as M
import Data.Text (Text)
import qualified Data.Text as T

import Network.Lastfm.Internal
import Network.Lastfm.Internal


class Argument a where
Expand Down Expand Up @@ -96,15 +95,15 @@ post = wrap $ \r -> r { _method = "POST" }
--
-- This is a little helper. It's actually enough
-- to specialize Format manually
json :: Request JSON a
json :: Request 'JSON a
json = wrap id
{-# INLINE json #-}

-- | Change API response format to XML
--
-- This is a little helper. It's actually enough
-- to specialize Format manually
xml :: Request XML a
xml :: Request 'XML a
xml = wrap id
{-# INLINE xml #-}

Expand Down
4 changes: 2 additions & 2 deletions src/Network/Lastfm/Response.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ class Supported f r | f -> r, r -> f where
parseResponseBody :: Lazy.ByteString -> Maybe r
parseResponseEncodedError :: r -> Maybe LastfmError

instance Supported JSON Value where
instance Supported 'JSON Value where
prepareRequest r = r { _query = M.singleton "format" "json" `M.union` _query r }
parseResponseBody = decode
parseResponseEncodedError = parseMaybe $ \(Object o) -> do
code <- o .: "error"
msg <- o .: "message"
return (LastfmEncodedError code msg)

instance Supported XML Document where
instance Supported 'XML Document where
prepareRequest = id
parseResponseBody = either (const Nothing) Just . parseLBS def
parseResponseEncodedError doc = case fromDocument doc of
Expand Down
8 changes: 4 additions & 4 deletions test/api/SpecHelper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ shouldHaveResponse q l = do
Just _ -> return ()
Nothing -> assertFailure (printf "Query failed on %s" (show r))

shouldHaveJson :: Request JSON Ready -> Fold Value a -> Expectation
shouldHaveJson :: Request 'JSON Ready -> Fold Value a -> Expectation
shouldHaveJson = shouldHaveResponse

shouldHaveXml :: Request XML Ready -> Fold Document a -> Expectation
shouldHaveXml :: Request 'XML Ready -> Fold Document a -> Expectation
shouldHaveXml = shouldHaveResponse

-- | Check success stuff for POST requests
shouldHaveJson_ :: Request JSON Ready -> Expectation
shouldHaveJson_ :: Request 'JSON Ready -> Expectation
shouldHaveJson_ l = shouldHaveResponse l (key "status".only "ok")

shouldHaveXml_ :: Request XML Ready -> Expectation
shouldHaveXml_ :: Request 'XML Ready -> Expectation
shouldHaveXml_ l = shouldHaveResponse l (root.attributed (ix "status".only "ok"))

-- | Make a request using public API key
Expand Down

0 comments on commit bfd66e7

Please sign in to comment.