module Network.Lastfm.Request
(
Request, R, Ready, Sign, Format(..)
, api, post, get, json, xml, APIKey, apiKey, SessionKey, sessionKey
, Token, token, Callback, callback
, Artist, artist, artists, Album, album, MBID, mbid
, Country, country, Autocorrect, autocorrect
, Event, event, Status(..), status
, From, from, To, to, Group, group
, Language, language, Distance, distance
, Longitude, longitude, Latitude, latitude, Location, location
, Start, start, End, end, Festivals, festivalsonly
, StartTimestamp, startTimestamp, EndTimestamp, endTimestamp
, Metro, metro, Tag, tags, tag, Track, track, Timestamp , timestamp, Fingerprint, fingerprint
, AlbumArtist, albumArtist, Duration, duration, TrackNumber, trackNumber
, Playlist, playlist, Title, title, Description, description
, ChosenByUser, chosenByUser, Context, context, StreamId, streamId
, RecentTracks, recentTracks
, Recipient, recipient, Username, username, User, user, Password, password
, Public, public, Message, message, Page, page, Limit, limit
, TaggingType, taggingType, UseRecs, useRecs, Venue, venue, VenueName, venueName
, Discovery, discovery, RTP, rtp, BuyLinks, buyLinks, Multiplier(..), multiplier
, Bitrate(..), bitrate, Name, name, Station, station
, Targeted, comparison, Scrobble, LibraryAlbum, LibraryArtist
) where
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
class Argument a where
add :: Text -> a -> Request f b
add k v = wrap $ \r@R { _query = q } -> r { _query = M.insert k (toText v) q }
toText :: a -> Text
instance Argument Text where
toText = id
instance Argument Bool where
toText b = if b then "1" else "0"
instance Argument Int64 where
toText = T.pack . show
instance Argument a => Argument [a] where
toText = T.intercalate "," . map toText
data APIKey
data SessionKey
data Token
data Callback
api :: Text -> Request f a
api = add "method"
get :: Request f a
get = wrap $ \r -> r { _method = "GET" }
post :: Request f a
post = wrap $ \r -> r { _method = "POST" }
json :: Request JSON a
json = wrap id
xml :: Request XML a
xml = wrap id
apiKey :: Text -> Request f APIKey
apiKey = add "api_key"
sessionKey :: Text -> Request f SessionKey
sessionKey = add "sk"
token :: Text -> Request f Token
token = add "token"
callback :: Text -> Request f Callback
callback = add "cb"
data Artist
data Album
data MBID
data Country
data Language
data Tag
data Autocorrect
data Page
data Limit
data Message
data Public
data Recipient
data Username
data User
data Password
data Status = Attending | Maybe | NotAttending
data Event
data Festivals
data Longitude
data Latitude
data Location
data Distance
data Metro
data Start
data End
data StartTimestamp
data EndTimestamp
data From
data To
data Playlist
data Title
data Description
data Track
data Timestamp
data Fingerprint
data AlbumArtist
data Context
data StreamId
data Duration
data TrackNumber
data ChosenByUser
data TaggingType
data RecentTracks
data UseRecs
data Scrobble
data Group
data Venue
data VenueName
data Multiplier = M1 | M2
data Bitrate = B64 | B128
data Name
data Station
data Discovery
data RTP
data BuyLinks
data LibraryAlbum
data LibraryArtist
artist :: Text -> Request f Artist
artist = add "artist"
artists :: [Text] -> Request f [Artist]
artists = add "artists"
album :: Text -> Request f Album
album = add "album"
mbid :: Text -> Request f MBID
mbid = add "mbid"
country :: Text -> Request f Country
country = add "country"
language :: Text -> Request f Language
language = add "lang"
tags :: [Text] -> Request f [Tag]
tags = add "tags"
tag :: Text -> Request f Tag
tag = add "tag"
autocorrect :: Bool -> Request f Autocorrect
autocorrect = add "tags"
page :: Int64 -> Request f Page
page = add "page"
limit :: Int64 -> Request f Limit
limit = add "limit"
message :: Text -> Request f Message
message = add "message"
public :: Bool -> Request f Public
public = add "public"
recipient :: Text -> Request f Recipient
recipient = add "recipient"
username :: Text -> Request f Username
username = add "username"
user :: Text -> Request f User
user = add "user"
password :: Text -> Request f Password
password = add "password"
status :: Status -> Request f Status
status = add "status" . T.pack . \s -> case s of
Attending -> "0"
Maybe -> "1"
_ -> "2"
event :: Int64 -> Request f Event
event = add "event"
festivalsonly :: Bool -> Request f Festivals
festivalsonly = add "festivalsonly"
longitude :: Text -> Request f Longitude
longitude = add "longitude"
latitude :: Text -> Request f Latitude
latitude = add "latitude"
location :: Text -> Request f Location
location = add "location"
distance :: Int64 -> Request f Distance
distance = add "distance"
venue :: Int64 -> Request f Venue
venue = add "venue"
venueName :: Text -> Request f VenueName
venueName = add "venue"
metro :: Text -> Request f Metro
metro = add "metro"
start :: Int64 -> Request f Start
start = add "start"
end :: Int64 -> Request f End
end = add "end"
startTimestamp :: Int64 -> Request f StartTimestamp
startTimestamp = add "startTimestamp"
endTimestamp :: Int64 -> Request f EndTimestamp
endTimestamp = add "endTimestamp"
from :: Int64 -> Request f From
from = add "from"
to :: Int64 -> Request f To
to = add "to"
track :: Text -> Request f Track
track = add "track"
timestamp :: Int64 -> Request f Timestamp
timestamp = add "timestamp"
playlist :: Int64 -> Request f Playlist
playlist = add "playlistID"
title :: Text -> Request f Title
title = add "title"
description :: Text -> Request f Description
description = add "description"
fingerprint :: Int64 -> Request f Fingerprint
fingerprint = add "fingerprintid"
albumArtist :: Text -> Request f AlbumArtist
albumArtist = add "albumArtist"
context :: Text -> Request f Context
context = add "context"
streamId :: Int64 -> Request f StreamId
streamId = add "streamId"
duration :: Int64 -> Request f Duration
duration = add "duration"
trackNumber :: Int64 -> Request f TrackNumber
trackNumber = add "trackNumber"
chosenByUser :: Bool -> Request f ChosenByUser
chosenByUser = add "chosenByUser"
taggingType :: Text -> Request f TaggingType
taggingType = add "taggingtype"
recentTracks :: Bool -> Request f RecentTracks
recentTracks = add "recentTracks"
useRecs :: Bool -> Request f UseRecs
useRecs = add "useRecs"
group :: Text -> Request f Group
group = add "group"
multiplier :: Multiplier -> Request f Multiplier
multiplier m = case m of
M1 -> add "speed_multiplier" (T.pack "1.0")
M2 -> add "speed_multiplier" (T.pack "2.0")
bitrate :: Bitrate -> Request f Bitrate
bitrate b = case b of
B64 -> add "bitrate" (64 :: Int64)
B128 -> add "bitrate" (128 :: Int64)
name :: Text -> Request f Name
name = add "name"
station :: Text -> Request f Station
station = add "station"
discovery :: Bool -> Request f Discovery
discovery = add "discovery"
rtp :: Bool -> Request f RTP
rtp = add "rtp"
buyLinks :: Bool -> Request f BuyLinks
buyLinks = add "buyLinks"
class Targeted a where
target :: Request f a -> Text
instance Targeted [Artist] where
target _ = "artists"
instance Targeted User where
target _ = "user"
comparison :: Targeted a => Int64 -> Request f a -> Request f a
comparison n t = let z = target t in
add ("type" <> toText n) z <*> add ("value" <> toText n) (_query (unwrap t (R mempty mempty mempty)) M.! z)