Remove extras from Spotify

This commit is contained in:
patrick brisbin 2018-01-27 11:55:37 -05:00
parent e8dc2ec0ec
commit 6b3c6af895

View File

@ -9,84 +9,33 @@ module Yesod.Auth.OAuth2.Spotify
import Yesod.Auth.OAuth2.Prelude import Yesod.Auth.OAuth2.Prelude
import Data.Maybe newtype User = User Text
import qualified Data.Text as T
data SpotifyUserImage = SpotifyUserImage instance FromJSON User where
{ spotifyUserImageHeight :: Maybe Int parseJSON = withObject "User" $ \o -> User
, spotifyUserImageWidth :: Maybe Int <$> o .: "id"
, spotifyUserImageUrl :: Text
}
instance FromJSON SpotifyUserImage where pluginName :: Text
parseJSON = withObject "SpotifyUserImage" $ \v -> SpotifyUserImage pluginName = "spotify"
<$> v .:? "height"
<*> v .:? "width"
<*> v .: "url"
data SpotifyUser = SpotifyUser oauth2Spotify :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
{ spotifyUserId :: Text oauth2Spotify scopes clientId clientSecret =
, spotifyUserHref :: Text authOAuth2 pluginName oauth2 $ \manager token -> do
, spotifyUserUri :: Text (User userId, userResponseJSON) <-
, spotifyUserDisplayName :: Maybe Text authGetProfile pluginName manager token "https://api.spotify.com/v1/me"
, spotifyUserProduct :: Maybe Text
, spotifyUserCountry :: Maybe Text
, spotifyUserEmail :: Maybe Text
, spotifyUserImages :: Maybe [SpotifyUserImage]
}
instance FromJSON SpotifyUser where pure Creds
parseJSON = withObject "SpotifyUser" $ \v -> SpotifyUser { credsPlugin = pluginName
<$> v .: "id" , credsIdent = userId
<*> v .: "href" , credsExtra = setExtra token userResponseJSON
<*> v .: "uri" }
<*> v .:? "display_name" where
<*> v .:? "product" oauth2 = OAuth2
<*> v .:? "country"
<*> v .:? "email"
<*> v .:? "images"
oauth2Spotify :: YesodAuth m
=> Text -- ^ Client ID
-> Text -- ^ Client Secret
-> [Text] -- ^ Scopes
-> AuthPlugin m
oauth2Spotify clientId clientSecret scope = authOAuth2 "spotify"
OAuth2
{ oauthClientId = clientId { oauthClientId = clientId
, oauthClientSecret = clientSecret , oauthClientSecret = clientSecret
, oauthOAuthorizeEndpoint = "https://accounts.spotify.com/authorize" `withQuery` , oauthOAuthorizeEndpoint = "https://accounts.spotify.com/authorize" `withQuery`
[ ("scope", encodeUtf8 $ T.intercalate " " scope) [ scopeParam " " scopes
] ]
, oauthAccessTokenEndpoint = "https://accounts.spotify.com/api/token" , oauthAccessTokenEndpoint = "https://accounts.spotify.com/api/token"
, oauthCallback = Nothing , oauthCallback = Nothing
} }
$ fromProfileURL "spotify" "https://api.spotify.com/v1/me" toCreds
toCreds :: SpotifyUser -> Creds m
toCreds user = Creds
{ credsPlugin = "spotify"
, credsIdent = spotifyUserId user
, credsExtra = mapMaybe getExtra extrasTemplate
}
where
userImage :: Maybe SpotifyUserImage
userImage = spotifyUserImages user >>= listToMaybe
userImagePart :: (SpotifyUserImage -> Maybe a) -> Maybe a
userImagePart getter = userImage >>= getter
extrasTemplate = [ ("href", Just $ spotifyUserHref user)
, ("uri", Just $ spotifyUserUri user)
, ("display_name", spotifyUserDisplayName user)
, ("product", spotifyUserProduct user)
, ("country", spotifyUserCountry user)
, ("email", spotifyUserEmail user)
, ("image_url", spotifyUserImageUrl <$> userImage)
, ("image_height", T.pack . show <$> userImagePart spotifyUserImageHeight)
, ("image_width", T.pack . show <$> userImagePart spotifyUserImageWidth)
]
getExtra :: (Text, Maybe Text) -> Maybe (Text, Text)
getExtra (key, val) = fmap ((,) key) val