In each of our wrapper functions we are calling the http-get
method with a hard coded string for the API URL.
(http-get credentials (str "https://www.reddit.com/.json?limit=" limit "&t=" (name time))
Here are some different approaches that we could take
- Custom function for each URL
(http-get credentials (str (url/subreddit "aww") "?limit=" limit "&t=" (name time))
- Decorative functions
(-> reddit
(subreddit "awww')
(limit 1)
(time :hour))
The reddit function creates the base url and each function then decorates on top of it
With this approach we might not need to create custom url for each endpoint, instead we just need a small set of decorators.
The con is that it will be difficult to manage invalid URLs, not sure how to handle that.
- Create a custom DLS like hiccup
[:reddit
[:subreddit "aww"]
[:limit 1]
[:time :hour]]
Works out same as the decorative model but this allows for easy algorithmic url creation. This is very suitable of the url is to be created at run time. Might be a lot of fun to build as well.
The reason I am suggesting this change is that in all of our wrapper functions the only thing that is different is the URL for the API and creating a custom wrapper function for each endpoint might not be scalable.
With functions that generate url we can change the client to look something like this
(defn reddit-client [cred url]
(-> (http-get cred (build-url url))
(process-response))
And the usage would be something like this
(reddit-client
cred
[:reddit
[:subreddit "aww"]
[:limit 1]
[:time :hour]])
- In the decorative model supporting new URL's will be easier and much more reuse-able.
- URL's can be stored since they are merely data allowing for more flexibility
- Makes the process a little more pure
What are your thoughts on it?