Trouble accessing async object from RestAPI Prefer: respond-async

Post Reply
User avatar
WilliamSmith
Posts: 44
Joined: Tue Dec 13, 2022 8:54 pm
OLAP Product: TM1 / PA / PAx / PAW
Version: TM1 11
Excel Version: 365

Trouble accessing async object from RestAPI Prefer: respond-async

Post by WilliamSmith »

Hi all,

I'm having trouble interacting with the TM1 rest API in the following way. I'm hoping maybe there's some obvious that I'm missing :)

Kick-off TI process

Code: Select all

public async static Task<string> RunProcessWithPolingAsync(TM1SharpConfig tm1, string processName, Dictionary<string, string>? parameters = null)
{
	var client = tm1.GetTM1RestClient();

	client.DefaultRequestHeaders.Add("Prefer", "respond-async");

	var jsonBody = new StringBuilder();

	if (parameters != null)
	{
		jsonBody.Append("{\"Parameters\":[");

		parameters.AsEnumerable().ToList().ForEach(x =>
		{
			jsonBody.Append("{\"Name\":\"" + x.Key + "\", \"Value\":\"" + x.Value + "\"}");
		});

		jsonBody.Append("]}");
	}

	var jsonPayload = new StringContent(jsonBody.ToString(), new MediaTypeWithQualityHeaderValue("application/json"));

	var response = await client.PostAsync(tm1.ServerHTTPSAddress + @"/api/v1/Processes('" + processName + "')/tm1.ExecuteWithReturn", jsonPayload);

	var asyncId = ParseAsyncId(response.Headers.Where(x => x.Key.Equals("Location")).First().Value.First());

	return await GetAsyncStatus(tm1, asyncId);
}
Returns location header, parse out ID

Code: Select all

../_async('OLTJUN4E4XFBAoV1fiSrQsmK9tn1U')
Call to check status always 404s. Doesn't matter how long I wait or how many times I check

Code: Select all

https://server:port/api/v1/_async('OLTJUN4E4XFBAoV1fiSrQsmK9tn1U')
Status Code: 404
Content: {"error":{"code":"278","message":"Resource '/api/v1/_async('OLTJUN4E4XFBAoV1fiSrQsmK9tn1U')' not found"}}
ascheevel
Community Contributor
Posts: 311
Joined: Fri Feb 15, 2013 5:49 pm
OLAP Product: TM1
Version: PA 2.0.9.1
Excel Version: 365
Location: Minneapolis, USA

Re: Trouble accessing async object from RestAPI Prefer: respond-async

Post by ascheevel »

Is it always 404 or just after the first check and you see the job has completed? The async jobs are cleaned up after it has completed and has been polled i.e. if I check on an async job and it's completed it won't be there anymore if I try to check on the same job 5 minutes later.

If you're always seeing 404, it may because you're polling from a separate session id. The async job is session specific, you can't check on the status of some other thread's job, even if it was executing under your ID.
User avatar
WilliamSmith
Posts: 44
Joined: Tue Dec 13, 2022 8:54 pm
OLAP Product: TM1 / PA / PAx / PAW
Version: TM1 11
Excel Version: 365

Re: Trouble accessing async object from RestAPI Prefer: respond-async

Post by WilliamSmith »

"If you're always seeing 404, it may because you're polling from a separate session id. The async job is session specific, you can't check on the status of some other thread's job, even if it was executing under your ID."

Oh, that's a good hint. I was trying to access the async object on a separate http session. This was it.

Thank you so much!
Post Reply