I'm trying to write a Haskell module that defines functions for a remote XML-RPC API using the library haxr. Here's how haxr's documentation suggests you define a Haskell function that calls examples.add
on the server at url
:
add :: String -> Int -> Int -> IO Int
add url = remote url "examples.add"
called like this:
server = "http://localhost/~bjorn/cgi-bin/simple_server"
add server x y
This seems ok to me if I had one or two XML-RPC methods (I wouldn't need a seperate module then). However, the duplication of server
in the code is a problem since I have close to 100 functions. I can't define server
in the module, like this:
someRemote :: Remote
someRemote = remote "http://example.com/XMLRPC"
add :: Int -> Int -> IO Int
add = someRemote "examples.add"
since the URL can't be hard-coded if it is to be flexible for the code that uses it. I also can't define the someRemote
as a parameter of the functions, as it has the same duplication issue.
Haxr's examples provide no clues on how to solve this.
I usually write programs in imperative OOP languages (i.e. Java, Python). If I were using those languages, I would define a class with an constructor that takes server
, and all the functions using the object instances' server
variable, rather than asking the calling code for it.
I've looked for an equivalent to this in Haskell, but I don't seem to know the right keywords to find it. Type classes don't seem to be the answer. I could write a higher order function that returns the partially applied functions, but unpacking those would be even uglier.