on master help sign in
Use the ‘http-api-data’ package to render things

Should make using the library *a lot* more pleasant.

commit1df79c77c7ac305f3b884860e5bd4231cc5f93ea authormrkkrp <markkarpov@opmbx.org> dateFri 4 Nov 2016, 02:51 (10 years ago) parent0741f4f
2 files changed+19−11
Network/HTTP/Req.hs+18−11 req.cabal+1
Network/HTTP/Req.hs+18−11
@@ -195,6 +195,7 @@ import Data.Typeable (Typeable)
195195import GHC.Generics
196196import GHC.TypeLits
197197import System.IO.Unsafe (unsafePerformIO)
198+import Web.HttpApiData (ToHttpApiData (..))
198199import qualified Blaze.ByteString.Builder as BB
199200import qualified Data.Aeson as A
200201import qualified Data.Binary.Builder as R
@@ -501,6 +502,9 @@ instance HttpMethod method => RequestComponent (Womb "method" method) where
501502-- > https "httpbin.org" /: "foo" /: "bar/baz"
502503-- > -- https://httpbin.org/foo/bar%2Fbaz
503504--
505+-- > https "httpbin.org" /: "bytes" /: (10 :: Int)
506+-- > -- https://httpbin.org/bytes/10
507+--
504508-- > https "юникод.рф"
505509-- > -- https://%D1%8E%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4.%D1%80%D1%84
506510
@@ -520,11 +524,12 @@ http = Url Http . pure
520524https :: Text -> Url 'Https
521525https = Url Https . pure
522526
523−-- | Grow given 'Url' appending a single path segment to it.
527+-- | Grow given 'Url' appending a single path segment to it. Note that the
528+-- path segment can be of any type that is an instance of 'ToHttpApiData'.
524529
525530infixl 5 /:
526−(/:) :: Url scheme -> Text -> Url scheme
527−Url secure path /: segment = Url secure (NE.cons segment path)
531+(/:) :: ToHttpApiData a => Url scheme -> a -> Url scheme
532+Url secure path /: segment = Url secure (NE.cons (toUrlPiece segment) path)
528533
529534-- | The 'parseUrlHttp' function provides an alternative method to get 'Url'
530535-- (possibly with some 'Option's) from a 'ByteString'. This is useful when
@@ -670,7 +675,8 @@ newtype FormUrlEncodedParam = FormUrlEncodedParam [(Text, Maybe Text)]
670675 deriving (Semigroup, Monoid)
671676
672677instance QueryParam FormUrlEncodedParam where
673− queryParam name mvalue = FormUrlEncodedParam [(name, mvalue)]
678+ queryParam name mvalue =
679+ FormUrlEncodedParam [(name, toQueryParam <$> mvalue)]
674680
675681-- | A type class for things that can be interpreted as HTTP
676682-- 'L.RequestBody'.
@@ -771,7 +777,7 @@ instance RequestComponent (Option scheme) where
771777-- $query-parameters
772778--
773779-- This section describes a polymorphic interface that can be used to
774−-- construct query parameters (of type 'Option') and form url encoded bodies
780+-- construct query parameters (of type 'Option') and form URL-encoded bodies
775781-- (of type 'FormUrlEncodedParam').
776782
777783-- | This operator builds a query parameter that will be included in URL of
@@ -783,7 +789,7 @@ instance RequestComponent (Option scheme) where
783789-- > name =: value = queryParam name (pure value)
784790
785791infix 7 =:
786−(=:) :: QueryParam a => Text -> Text -> a
792+(=:) :: (QueryParam param, ToHttpApiData a) => Text -> a -> param
787793name =: value = queryParam name (pure value)
788794
789795-- | Construct a flag, that is, valueless query parameter. For example, in
@@ -795,25 +801,26 @@ name =: value = queryParam name (pure value)
795801--
796802-- > queryFlag name = queryParam name Nothing
797803
798−queryFlag :: QueryParam a => Text -> a
799−queryFlag name = queryParam name Nothing
804+queryFlag :: QueryParam param => Text -> param
805+queryFlag name = queryParam name (Nothing :: Maybe ())
800806
801807-- | A type class for query-parameter-like things. The reason to have
802808-- overloaded 'queryParam' is to be able to use it as an 'Option' and as a
803809-- 'FormUrlEncodedParam' when constructing form URL encoded request bodies.
804810-- Having the same syntax for these cases seems natural and user-friendly.
805811
806−class QueryParam a where
812+class QueryParam param where
807813
808814 -- | Create a query parameter with given name and value. If value is
809815 -- 'Nothing', it won't be included at all (i.e. you create a flag this
810816 -- way). It's recommended to use @('=:')@ and 'queryFlag' instead of this
811817 -- method, because they are easier to read.
812818
813− queryParam :: Text -> Maybe Text -> a
819+ queryParam :: ToHttpApiData a => Text -> Maybe a -> param
814820
815821instance QueryParam (Option scheme) where
816− queryParam name mvalue = withQueryParams ((:) (name, mvalue))
822+ queryParam name mvalue =
823+ withQueryParams ((:) (name, toQueryParam <$> mvalue))
817824
818825----------------------------------------------------------------------------
819826-- Request — Optional parameters — Headers
req.cabal+1
@@ -64,6 +64,7 @@ library
6464 , case-insensitive >= 0.2 && < 1.3
6565 , connection >= 0.2.2 && < 0.3
6666 , data-default-class
67+ , http-api-data >= 0.2 && < 0.4
6768 , http-client >= 0.5 && < 0.6
6869 , http-client-tls >= 0.3.2 && < 0.4
6970 , http-types >= 0.8 && < 10.0