libstackexchange 0.2.1.0 → 0.3.0.0
raw patch · 4 files changed
+35/−43 lines, 4 filesdep +profunctorsdep −aeson-lensdep −lensPVP ok
version bump matches the API change (PVP)
Dependencies added: profunctors
Dependencies removed: aeson-lens, lens
API changes (from Hackage documentation)
- Network.StackExchange.Response: se :: (Functor f, Isomorphic k) => k (SE a -> f (SE a)) (Maybe Value -> f (Maybe Value))
+ Network.StackExchange.Response: se :: (Functor f, Profunctor p) => p (SE a) (f (SE t)) -> p (Maybe Value) (f (Maybe Value))
Files
- libstackexchange.cabal +2/−3
- src/Network/StackExchange/API.hs +7/−7
- src/Network/StackExchange/Request.hs +19/−27
- src/Network/StackExchange/Response.hs +7/−6
libstackexchange.cabal view
@@ -1,5 +1,5 @@ name: libstackexchange-version: 0.2.1.0+version: 0.3.0.0 synopsis: StackExchange API interface description: Provides interface for StackExchange v2.1 API homepage: https://github.com/supki/libstackexchange@@ -19,9 +19,8 @@ attoparsec, text, aeson,- aeson-lens, http-conduit,- lens >= 3.0.4+ profunctors hs-source-dirs: src exposed-modules: Network.StackExchange Network.StackExchange.API
src/Network/StackExchange/API.hs view
@@ -81,14 +81,12 @@ , writePermissions, meWritePermissions ) where -import Control.Applicative ((<$>)) import Data.Monoid ((<>)) import Control.Exception (throw)-import Control.Lens ((^.))-import Data.Aeson (Value)+import Data.Aeson ((.:), Value) import qualified Data.Aeson as A-import qualified Data.Aeson.Lens as L+import qualified Data.Aeson.Types as A import qualified Data.Attoparsec.Lazy as AP import Data.ByteString.Lazy (ByteString) import Data.Text.Lazy (Text)@@ -100,8 +98,10 @@ import Network.StackExchange.Request -- $setup--- >>> import Control.Lens ((^.),(^..), from, to, traverse)--- >>> import Data.Maybe (catMaybes, isJust)+-- >>> import Control.Applicative+-- >>> import Control.Lens+-- >>> import qualified Data.Aeson.Lens as L+-- >>> import Data.Maybe (catMaybes, isJust) -- >>> let pagesize = 10 :: Int -- >>> let checkLengthM f = ((== pagesize) . length) `fmap` f -- >>> let k = key "Lhg6xe5d5BvNK*C0S8jijA(("@@ -1335,4 +1335,4 @@ items ∷ Maybe Value → Maybe [SE a]-items s = map SE <$> (s ^. L.key "items")+items s = fmap (map SE) . A.parseMaybe (\o -> A.parseJSON o >>= (.: "items")) =<< s
src/Network/StackExchange/Request.hs view
@@ -4,7 +4,6 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE ViewPatterns #-} -- | StackExchange API request manipulation routines@@ -26,7 +25,6 @@ import Prelude hiding (filter) import Unsafe.Coerce (unsafeCoerce) -import Control.Lens hiding (query) import Data.ByteString.Lazy (ByteString) import Data.Aeson (FromJSON) import Data.Aeson.Types (Value)@@ -99,16 +97,6 @@ } -makeLensesFor- [ ("_host", "__host")- , ("_path", "__path")- , ("_method", "__method")- , ("_query", "__query")- , ("_parse", "__parse")- ]- ''R-- type Request a n r = Dual (Endo (R a n r)) @@ -128,7 +116,7 @@ -- -- Primarily used in Auth, not intended for usage by library user host ∷ Text → Request a n r-host p = wrap $ __host .~ p+host h = wrap $ \r -> r { _host = h } {-# INLINE host #-} @@ -136,7 +124,7 @@ -- -- Primarily used in API call wrappers, not intended for usage by library user path ∷ Text → Request a n r-path p = wrap $ __path .~ p+path p = wrap $ \r -> r { _path = p } {-# INLINE path #-} @@ -144,7 +132,7 @@ -- -- Primarily used in API call wrappers, not intended for usage by library user method ∷ Text → Request a n r-method m = wrap $ __method .~ m+method m = wrap $ \r -> r { _method = m } {-# INLINE method #-} @@ -152,7 +140,7 @@ -- -- Primarily used in API call wrappers, not intended for usage by library user parse ∷ (ByteString → r) → Request a n r-parse f = wrap $ __parse ?~ f+parse f = wrap $ \r -> r { _parse = Just f } {-# INLINE parse #-} @@ -164,37 +152,37 @@ -- -- Takes a list of (key, value) parameters such as @[(\"order\", \"asc\"), (\"sort\", \"rank\")]@ query ∷ [(Text, Text)] → Request a n r-query q = wrap $ __query %~ (M.fromList q <>)+query q = wrap $ __query (M.fromList q <>) {-# INLINE query #-} -- | Convert token requiring Request into ready one token ∷ Text → Request RequireToken n r → Request Ready n r-token t = unsafeCoerce . mappend (wrap (__query %~ M.insert "access_token" t))+token t = unsafeCoerce . mappend (wrap (__query (M.insert "access_token" t))) {-# INLINE token #-} -- | Request defining only App key key ∷ Text → Request a n r-key s = wrap $ __query %~ M.insert "key" s+key k = wrap $ __query (M.insert "key" k) {-# INLINE key #-} -- | Request defining only API call site query parameter site ∷ Text → Request a n r-site s = wrap $ __query %~ M.insert "site" s+site s = wrap $ __query (M.insert "site" s) {-# INLINE site #-} -- | Request defining only API call filter query parameter filter ∷ Text → Request a n r-filter f = wrap $ __query %~ M.insert "filter" f+filter f = wrap $ __query (M.insert "filter" f) {-# INLINE filter #-} -- | Request defining only API call state query parameter state ∷ Text → Request a n r-state s = wrap $ __query %~ M.insert "state" s+state s = wrap $ __query (M.insert "state" s) {-# INLINE state #-} @@ -204,7 +192,7 @@ -- | Request defining only API call scope query parameter scope ∷ [Scope] → Request a n r-scope ss = wrap $ __query %~ (M.insert "scope" $ scopie ss)+scope ss = wrap $ __query (M.insert "scope" $ scopie ss) where scopie xs = T.intercalate "," . flip map xs $ \case ReadInbox → "read_inbox"@@ -217,7 +205,7 @@ -- -- Primarily used in Authentication API call wrappers, not intended for usage by library user client ∷ Int → Request a n r-client (toLazyText . decimal → c) = wrap $ __query %~ M.insert "client_id" c+client (toLazyText . decimal → c) = wrap $ __query (M.insert "client_id" c) {-# INLINE client #-} @@ -225,7 +213,7 @@ -- -- Primarily used in Authentication API call wrappers, not intended for usage by library user redirectURI ∷ Text → Request a n r-redirectURI r = wrap $ __query %~ M.insert "redirect_uri" r+redirectURI r = wrap $ __query (M.insert "redirect_uri" r) {-# INLINE redirectURI #-} @@ -233,7 +221,7 @@ -- -- Primarily used in Authentication API call wrappers, not intended for usage by library user secret ∷ Text → Request a n r-secret c = wrap $ __query %~ M.insert "client_secret" c+secret c = wrap $ __query (M.insert "client_secret" c) {-# INLINE secret #-} @@ -241,7 +229,7 @@ -- -- Primarily used in Authentication API call wrappers, not intended for usage by library user code ∷ Text → Request a n r-code c = wrap $ __query %~ M.insert "code" c+code c = wrap $ __query (M.insert "code" c) {-# INLINE code #-} @@ -261,3 +249,7 @@ T.unpack $ mconcat [_host, "/", _path, "?", argie _query] where argie = T.intercalate "&" . M.foldrWithKey (\k v m → T.concat [k, "=", v] : m) mempty+++__query :: (Map Text Text -> Map Text Text) -> R a n r -> R a n r+__query f = \r -> r { _query = f (_query r) }
src/Network/StackExchange/Response.hs view
@@ -16,7 +16,7 @@ import Data.Maybe (fromMaybe) import Data.Typeable (Typeable) -import Control.Lens+import Data.Profunctor import Data.Aeson (Value(..)) import Data.ByteString.Lazy (ByteString, toStrict) import Data.Default (Default(..))@@ -49,9 +49,10 @@ -- | Isomorphism for the ease of interaction with aeson-lens-se ∷ (Functor f, Isomorphic k) ⇒ k (SE a → f (SE a)) (Maybe Value → f (Maybe Value))-se = iso to' from'+se :: (Functor f, Profunctor p) => p (SE a) (f (SE t)) -> p (Maybe Value) (f (Maybe Value))+se = dimap sa (fmap bt) where- to' = SE . fromMaybe Null- from' (SE Null) = Nothing- from' (SE x) = Just x+ sa = SE . fromMaybe Null++ bt (SE Null) = Nothing+ bt (SE x) = Just x