I've been working on making a package for the Twitter API. To build methods, I've been copying code between functions, so that every function nearly has the same pattern:
#No default argument function get_help_configuration(; options = Dict()) endpoint = "https://api.twitter.com/1.1/help/configuration.json" #URI encode values for all keys in Dict encodeURI(options) #Build query string query_str = Requests.format_query_str(options) #Build oauth_header oauth_header_val = oauthheader("GET", endpoint, options) return Requests.get(URI("$(endpoint)?$query_str"); headers = {"Content-Type" => "application/x-www-form-urlencoded", "Authorization" => oauth_header_val, "Connection" => "close", "Accept" => "*/*"}) end #One default argument function get_direct_messages(count::Int; options = Dict()) endpoint = "https://api.twitter.com/1.1/direct_messages.json" #Add status into options Dict options["count"] = "$count" #URI encode values for all keys in Dict encodeURI(options) #Build query string query_str = Requests.format_query_str(options) #Build oauth_header oauth_header_val = oauthheader("GET", endpoint, options) return Requests.get(URI("$(endpoint)?$query_str"); headers = {"Content-Type" => "application/x-www-form-urlencoded", "Authorization" => oauth_header_val, "Connection" => "close", "Accept" => "*/*"}) end The only difference between the two function calls is whether I've specified an argument for the most common option. Each function call has either of these two patterns (which other than the first argument, are the same themselves). What's the best way to avoid repetition: a macro, a function, something else? The only inputs are endpoint (URI) and the first argument (values like count::Int, screen_name::String), which needs to be substituted into the area where I add the key to the Dict.