<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: batch process for http request in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284059#M54875</link>
    <description>&lt;P&gt;I had a question regarding background http requests that&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/982"&gt;@Craige_Hales&lt;/a&gt;&amp;nbsp;answered.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;LI-MESSAGE title="Http Background Request" uid="283304" url="https://community.jmp.com/t5/Discussions/Http-Background-Request/m-p/283304#U283304" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.jmp.com/t5/Uncharted/Can-JMP-run-JMP/ba-p/283748#U283748" target="_blank"&gt;https://community.jmp.com/t5/Uncharted/Can-JMP-run-JMP/ba-p/283748#U283748&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I then took his code and modified it to do something like what you are looking for.&amp;nbsp; In my case I was querying over different dates but you can modify the code to loop over whatever you want.&amp;nbsp; You will probably need to make some adjustments depending on your specific use case.&lt;/P&gt;&lt;P&gt;Hopefully this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
url = "www....";

//Using this to generate different queries
query_keys = Associative Array();
query_keys["key1"] ="Bob";  //Or whatever your query parameters are

//I am looping over different dates
days_back = 20;
day_inc = 2;

start = Date Increment(today(),"day",-days_back);
end = Today();

cur_start = Date Increment(today(),"day",-day_inc);
cur_end = end;
index = 1;
//Here I am making files that contain the url and query key for each http request.  I will use these later.  This is only really needed if you intent to parallelize the http requset among multiple jmp instances.&lt;BR /&gt;//If you don't want to parallelize the queries you can skip the writing to disk and just make a list of your query keys and/or urls.
while(cur_end&amp;gt;start,
	query_key["startdate"] = char(Format(cur_start,"yyyy-mm-dd"));
	query_key["enddate"] = char(Format(cur_end,"yyyy-mm-dd"));
	Save Text File("$temp/url_file"||char(index)||".txt",url);
	Save Text File("$temp/query_keys_file"||char(index)||".txt",As JSON Expr(query_keys));
	index++;
	if(Date Increment(cur_start,"day",-day_inc)&amp;gt;start,
		cur_start = Date Increment(cur_start,"day",-day_inc),
		cur_start = start;
	);
	cur_end = Date Increment(cur_end,"day",-day_inc);
);


index--;
nscripts = index;

njmp = 1; //You can parallelize this as much as you want by launching more jmp instances.  Or if you want to 

// a place to keep up with running instances
rp = [=&amp;gt; ];
rpIndex=0;

For( i = 1, i &amp;lt;= njmp, i += 1, 
	// build a custom JSL for each JMP to execute
	workerFileName = Save Text File(
		"$temp\deleteMe_RunsWhenLaunchedBeCareful" || Char( i ) || ".jsl",
		"\[//!
		deletefile("$temp/sentinel.txt");
		for(i=1,i&amp;lt;=]\"||char(nscripts)||"\[,i++,
			if(Is File("$temp/url_file"||char(i)||".txt"),
				url = Load Text File( "$temp/url_file"||char(i)||".txt" )  ;
				query_key = Parse JSON( Load Text File( "$temp/query_keys_file"||char(i)||".txt" )  );
				Delete File("$temp/url_file"||char(i)||".txt");
				Delete File("$temp/query_keys_file"||char(i)||".txt");&lt;BR /&gt;//Using try so if one request fails I can keep moving through the requests
				Try(
					dig_http = New HTTP Request(URL(url),Method("Get"),Query String(query_key),Timeout(60*60*2))&amp;lt;&amp;lt;Send;&lt;BR /&gt;//My data is coming in as csv so I convert that to jmp.
					file_name = "$temp/data"||char(i)||".csv";
					Save Text File(file_name,dig_http);
					dt = Open(file_name);
					dt &amp;lt;&amp;lt; Save("$temp/data"||char(i)||".jmp");
					Delete File("$temp/data"||char(i)||".csv");
					,&lt;BR /&gt;//Dummy file if try fails
					dt = New Table();
					dt &amp;lt;&amp;lt; Save("$temp/data"||char(i)||".jmp");
				);
			);	
		);
		exit();
		]\"
	);
	// don't launch another copy until the previous copy is running.
	// JMP will become unhappy if the preferences file is busy in another
	// copy of JMP. The sentinel will be deleted when JMP starts
	savetextfile("$temp/sentinel.txt","");
	rp[rpIndex+=1] = Run Program( executable( "jmp" ), options( {workerFileName} ) );
	write("\!n",i," started...");
	// wait for the sentinel to vanish
	while(fileexists("$temp/sentinel.txt"),
		wait(1);// don't burn the CPU while waiting
	);
	write("ok");
);



// many queries are probably still queued, wait for them to finish
scripts_left = nscripts;
While( scripts_left,
	scripts_left = 0;
	For( i = 1, i &amp;lt;= nscripts, i++,
		If(Is File("$temp/url_file"||char(i)||".txt"),
			scripts_left += 1; // count the ones still alive
		)
	);
	write("\!n",scripts_left," http requests left");
	wait(1); // don't burn the CPU while waiting
	
);


Write( "\!ngathering results..." );
dt = New table("Final Table");
For( i = 1, i &amp;lt;= nscripts, i += 1, 
	filename="$temp/data"||char(i)||".jmp";
	// do something with the results
		// clean up the temp files
	tmp_dt = open(filename);
	dt &amp;lt;&amp;lt; Concatenate(tmp_dt,Append to First Table);
	Close(tmp_dt);
	deletefile(filename);
	deletefile("$temp\deleteMe_RunsWhenLaunchedBeCareful" || Char( i ) || ".jsl");
	
);
Write( "\!ndone" );

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Aug 2020 14:28:26 GMT</pubDate>
    <dc:creator>Brandon</dc:creator>
    <dc:date>2020-08-04T14:28:26Z</dc:date>
    <item>
      <title>batch process for http request</title>
      <link>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/283963#M54861</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I trying to write a script that sends an http request by putting together an endpoint.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Putting together the request itself is not an issue but in some cases, I need to request some info on more than a 1000 items (test_article) at once.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm limited by the maximum length of the http request to requesting about a 1000 at a time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately, I have not been able to figure out how to put together a loop that would send requests for&amp;nbsp; batches of 1000 test_article and concatenate those as they are received.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any insight would be greatly appreciated&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 23:34:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/283963#M54861</guid>
      <dc:creator>Sburel</dc:creator>
      <dc:date>2023-06-09T23:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: batch process for http request</title>
      <link>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284059#M54875</link>
      <description>&lt;P&gt;I had a question regarding background http requests that&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/982"&gt;@Craige_Hales&lt;/a&gt;&amp;nbsp;answered.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;LI-MESSAGE title="Http Background Request" uid="283304" url="https://community.jmp.com/t5/Discussions/Http-Background-Request/m-p/283304#U283304" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.jmp.com/t5/Uncharted/Can-JMP-run-JMP/ba-p/283748#U283748" target="_blank"&gt;https://community.jmp.com/t5/Uncharted/Can-JMP-run-JMP/ba-p/283748#U283748&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I then took his code and modified it to do something like what you are looking for.&amp;nbsp; In my case I was querying over different dates but you can modify the code to loop over whatever you want.&amp;nbsp; You will probably need to make some adjustments depending on your specific use case.&lt;/P&gt;&lt;P&gt;Hopefully this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
url = "www....";

//Using this to generate different queries
query_keys = Associative Array();
query_keys["key1"] ="Bob";  //Or whatever your query parameters are

//I am looping over different dates
days_back = 20;
day_inc = 2;

start = Date Increment(today(),"day",-days_back);
end = Today();

cur_start = Date Increment(today(),"day",-day_inc);
cur_end = end;
index = 1;
//Here I am making files that contain the url and query key for each http request.  I will use these later.  This is only really needed if you intent to parallelize the http requset among multiple jmp instances.&lt;BR /&gt;//If you don't want to parallelize the queries you can skip the writing to disk and just make a list of your query keys and/or urls.
while(cur_end&amp;gt;start,
	query_key["startdate"] = char(Format(cur_start,"yyyy-mm-dd"));
	query_key["enddate"] = char(Format(cur_end,"yyyy-mm-dd"));
	Save Text File("$temp/url_file"||char(index)||".txt",url);
	Save Text File("$temp/query_keys_file"||char(index)||".txt",As JSON Expr(query_keys));
	index++;
	if(Date Increment(cur_start,"day",-day_inc)&amp;gt;start,
		cur_start = Date Increment(cur_start,"day",-day_inc),
		cur_start = start;
	);
	cur_end = Date Increment(cur_end,"day",-day_inc);
);


index--;
nscripts = index;

njmp = 1; //You can parallelize this as much as you want by launching more jmp instances.  Or if you want to 

// a place to keep up with running instances
rp = [=&amp;gt; ];
rpIndex=0;

For( i = 1, i &amp;lt;= njmp, i += 1, 
	// build a custom JSL for each JMP to execute
	workerFileName = Save Text File(
		"$temp\deleteMe_RunsWhenLaunchedBeCareful" || Char( i ) || ".jsl",
		"\[//!
		deletefile("$temp/sentinel.txt");
		for(i=1,i&amp;lt;=]\"||char(nscripts)||"\[,i++,
			if(Is File("$temp/url_file"||char(i)||".txt"),
				url = Load Text File( "$temp/url_file"||char(i)||".txt" )  ;
				query_key = Parse JSON( Load Text File( "$temp/query_keys_file"||char(i)||".txt" )  );
				Delete File("$temp/url_file"||char(i)||".txt");
				Delete File("$temp/query_keys_file"||char(i)||".txt");&lt;BR /&gt;//Using try so if one request fails I can keep moving through the requests
				Try(
					dig_http = New HTTP Request(URL(url),Method("Get"),Query String(query_key),Timeout(60*60*2))&amp;lt;&amp;lt;Send;&lt;BR /&gt;//My data is coming in as csv so I convert that to jmp.
					file_name = "$temp/data"||char(i)||".csv";
					Save Text File(file_name,dig_http);
					dt = Open(file_name);
					dt &amp;lt;&amp;lt; Save("$temp/data"||char(i)||".jmp");
					Delete File("$temp/data"||char(i)||".csv");
					,&lt;BR /&gt;//Dummy file if try fails
					dt = New Table();
					dt &amp;lt;&amp;lt; Save("$temp/data"||char(i)||".jmp");
				);
			);	
		);
		exit();
		]\"
	);
	// don't launch another copy until the previous copy is running.
	// JMP will become unhappy if the preferences file is busy in another
	// copy of JMP. The sentinel will be deleted when JMP starts
	savetextfile("$temp/sentinel.txt","");
	rp[rpIndex+=1] = Run Program( executable( "jmp" ), options( {workerFileName} ) );
	write("\!n",i," started...");
	// wait for the sentinel to vanish
	while(fileexists("$temp/sentinel.txt"),
		wait(1);// don't burn the CPU while waiting
	);
	write("ok");
);



// many queries are probably still queued, wait for them to finish
scripts_left = nscripts;
While( scripts_left,
	scripts_left = 0;
	For( i = 1, i &amp;lt;= nscripts, i++,
		If(Is File("$temp/url_file"||char(i)||".txt"),
			scripts_left += 1; // count the ones still alive
		)
	);
	write("\!n",scripts_left," http requests left");
	wait(1); // don't burn the CPU while waiting
	
);


Write( "\!ngathering results..." );
dt = New table("Final Table");
For( i = 1, i &amp;lt;= nscripts, i += 1, 
	filename="$temp/data"||char(i)||".jmp";
	// do something with the results
		// clean up the temp files
	tmp_dt = open(filename);
	dt &amp;lt;&amp;lt; Concatenate(tmp_dt,Append to First Table);
	Close(tmp_dt);
	deletefile(filename);
	deletefile("$temp\deleteMe_RunsWhenLaunchedBeCareful" || Char( i ) || ".jsl");
	
);
Write( "\!ndone" );

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Aug 2020 14:28:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284059#M54875</guid>
      <dc:creator>Brandon</dc:creator>
      <dc:date>2020-08-04T14:28:26Z</dc:date>
    </item>
    <item>
      <title>Re: batch process for http request</title>
      <link>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284164#M54885</link>
      <description>&lt;P&gt;Here is an approach that I use. It uses a single instance of JMP. The code combines the HTTP results into a single JSON object which is later converted to a data table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;list = {};
For( i = 1, i &amp;lt;= number_of_iterations, i++, 
        request = HTTP Request(
            Url( url ),
            Method( "GET" ),
            Headers( request_headers ),
            QueryString( query ),
            Password( access_token ),
            Timeout( 360 )
        );
		
        data = request &amp;lt;&amp;lt; Send;
        If( request &amp;lt;&amp;lt; IsSuccess,
            response = Parse JSON( data );
            If( AndMZ( Is Associative Array( response ), Contains( response, "records" ), N Items( response["records"] ) &amp;gt; 0 ),
                list ||= response["records"]
            );
        ,
            Wait( 0.25 )
        );
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Aug 2020 20:51:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284164#M54885</guid>
      <dc:creator>Ryan_Gilmore</dc:creator>
      <dc:date>2020-08-04T20:51:46Z</dc:date>
    </item>
    <item>
      <title>Re: batch process for http request</title>
      <link>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284166#M54886</link>
      <description>Thanks a lot! I'll try to adapt your approach to my issue.</description>
      <pubDate>Tue, 04 Aug 2020 20:56:32 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284166#M54886</guid>
      <dc:creator>Sburel</dc:creator>
      <dc:date>2020-08-04T20:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: batch process for http request</title>
      <link>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284169#M54888</link>
      <description>&lt;P&gt;I realized I've left out the scripts I was starting from my initial inquiry. For the sake of clarity, here's the starting point script I would like to find a way to breakdown into batches when the number of items exceeds 1000.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;		Names Default To Here( 1 );

		dt = Data Table( "test_article" ); // This is a summary data table only contains 1 column of interest 'test_article' - I want to retrieve some info for each of the unique items in that column 
		//If the number of items exceeds 1000, I'll need to break down the query in batches of 1000 or less to construct the http request
		//Right if the list is 1000 or less, I'm golden, if it's greater, the request will fail
		dt:test_article &amp;lt;&amp;lt; set data type( "Numeric" );
		dt &amp;lt;&amp;lt; Transpose( columns( :test_article ), Output Table( "Transpose of test_article" ) );
		dt = Data Table( "Transpose of test_article" );
		Col_List = dt &amp;lt;&amp;lt; Get Column Names( "String" );
		For( i = 1, i &amp;lt;= N Items( Col_List ), i++,
			If( Contains( Col_List[i], "Row" ),
				Column( Col_List[i] ) &amp;lt;&amp;lt; Set Selected( 1 )
			)
		);
		dt &amp;lt;&amp;lt; Combine Columns(
			delimiter( "," ),
			selected Columns only( 1 ),
			Selected Columns are Indicator Columns( 0 ),
			Column Name( "test_article" )
		);
		nc = N Cols( Data Table( "Transpose of test_article" ) );
		Show( nc );
		rowNum = 1;
		col = Column( Data Table( "Transpose of test_article" ), "test_article" );
		tableVal = col[rowNum];

				adr0 = "http:/xyz:1234/oligos/more-oligo-info-intended?ids=";
				address = adr0 || tableval;
		

			request = New HTTP Request( URL( address ), Method( "GET" ), Timeout( 240 ) );
			json1 = request &amp;lt;&amp;lt; Send;
			dt = JSON To Data Table( json1 );
			Current Data Table() &amp;lt;&amp;lt; set name( "test_article_info" );
			Data Table( "test_article" ) &amp;lt;&amp;lt; close window;
			Data Table( "Transpose of test_article" ) &amp;lt;&amp;lt; close window;
		// I need to build a for loop that will send 1 request after the next and progressively concatenate those file together&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Aug 2020 21:12:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284169#M54888</guid>
      <dc:creator>Sburel</dc:creator>
      <dc:date>2020-08-04T21:12:39Z</dc:date>
    </item>
    <item>
      <title>Re: batch process for http request</title>
      <link>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284170#M54889</link>
      <description>Hi Ryan,&lt;BR /&gt;&lt;BR /&gt;Thanks for the answer. I'll see if I can adapt your suggestion to the problem I've listed below.&lt;BR /&gt;&lt;BR /&gt;Best,&lt;BR /&gt;&lt;BR /&gt;Sebastien</description>
      <pubDate>Tue, 04 Aug 2020 21:13:45 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/batch-process-for-http-request/m-p/284170#M54889</guid>
      <dc:creator>Sburel</dc:creator>
      <dc:date>2020-08-04T21:13:45Z</dc:date>
    </item>
  </channel>
</rss>

