If you would like to improve performance when calling the Marketo API, you can make concurrent requests. This approach will allow you to get more data in a shorter period of time.
When making an API request, part of the round-trip time between the client and server is the transfer time on the wire. So if we can reduce the transfer time on the wire for the requests in aggregate, we will improve performance.
The sample code below shows you how to do this in Ruby. It uses EventMachine, which is an event-processing library used for making multithreaded requests.
The example below calls the Lead Activities API, and makes two concurrent requests. This approaches eliminates the transfer time from the client to server for the second request. It does this by including the second request at the same time as the first request. The API responses are written to a text file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
require 'em-http-request' require 'json' #Build request URL #Replace AAA-BBB-CCC with your Marketo instance marketo_instance = "https://AAA-BBB-CCC.mktorest.com" endpoint = "/rest/v1/activities.json" #Replace with your access token auth_token = "?access_token=" + "ac756f7a-d54d-41ac-8c3c-f2d2a39ee325:ab" #Specify datetime needed as nextPageToken since_date_time = ["&nextPageToken=A5YMOYZQBOGD2OSYYBYDAQGEMGLBDGDANAABQGRAQWAAKKID", "&nextPageToken=GIYDAOBNGEYS2MBWKQYDAORQGA5DAMBOGAYDAKZQGAYDALBQ"] #Specify activities needed activity_type_ids = "&activityTypeIds=1&activityTypeIds=12" requesturl_a = marketo_instance + endpoint + auth_token + since_date_time.at(0) + activity_type_ids requesturl_b = marketo_instance + endpoint + auth_token + since_date_time.at(1) + activity_type_ids #Make request EventMachine.run do http1 = EventMachine::HttpRequest.new(requesturl_a).get http2 = EventMachine::HttpRequest.new(requesturl_b).get #When API response is received, write response to a text file http1.callback { File.open('response1.txt', 'w') do |t| t.puts http1.response end } http2.callback { File.open('response2.txt', 'w') do |t| t.puts http2.response end } end |