function get_stuff(endpoint, options) #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 function get_direct_messages(count::Int; options=Dict()) get_stuff("https://api.twitter.com/1.1/direct_messages.json", setindex!(options, "count", "$count")) end function get_help_configuration(; options=Dict()) get_stuff("https://api.twitter.com/1.1/help/configuration.json", options) end My first refactor. You could make a macro to replace the setindex! call given the name of a variable. This is where I would probably stop (personal preference). However, you could then make a macro that creates a function given a name, an endpoint and a variable that generates the rest. I'm not sure that additional macro would save much space though. On Friday, 14 February 2014 08:34:20 UTC-6, Randy Zwitch wrote: > > 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. >