haskell wreq param list

208 Views Asked by At

In the excellent wreq Haskell library it is easy to add one or more query parameters to an URL:

opts = defaults & param "key" .~ ["value"]

however what I'm struggling to do is adding a list of parameters at a time:

params = [("key1", "value1"), ("key2", "value2"), ("key3", "value3")]

I know that there is function params but I could not find any example on how to use it.

1

There are 1 best solutions below

2
On BEST ANSWER

Both param <key> and params are lenses:

param  :: Text -> Lens' Options [Text] 
params ::         Lens' Options [(Text, Text)]

Without going too much into details, you can think of a lens focusing something, e.g. param "foo" focuses on some [Text] in Options that belong to the parameter foo (*). You can then change/query/manipulate those values with the correct function (see the lens package).

You've already used (.~) to replace the current values, and you can use it again with params:

default & params .~ [("key1", "value1"), ("key2", "value2"), ("key3", "value3")]

You can think of (.~) in this context as

(.~) :: Lens' a b -> b -> a -> a
-- concrete:
(.~) :: Lens' Options [(Text, Text)] -> [(Text, Text)] -> Options -> Options

(*) That's not 100% true, since lenses allow you to do all kinds of stuff, but good enough for this context.