/// <summary> /// Recursive method that polls the activity status until it completes. /// </summary> /// <param name="client">NField Connection Client (this)</param> /// <param name="activityId">The id of the activity to wait for.</param> /// <param name="fieldNameResult">The name of the result field</param> /// <returns>The <see cref="BackgroundActivityStatus" /> id.</returns> internal static Task <T> GetActivityResultAsync <T>(this INfieldConnectionClient client, string activityId, string fieldNameResult) { return(client.Client.GetAsync(client.BackgroundActivityUrl(activityId)) .ContinueWith(response => response.Result.Content.ReadAsStringAsync()) .Unwrap() .ContinueWith(content => { var obj = JObject.Parse(content.Result); var status = obj["Status"].Value <int>(); switch (status) { case 0: // pending case 1: // started Thread.Sleep(millisecondsTimeout: 200); return client.GetActivityResultAsync <T>(activityId, fieldNameResult); case 2: // succeeded var tcs = new TaskCompletionSource <T>(); tcs.SetResult(obj[fieldNameResult].Value <T>()); return tcs.Task; case 3: // failed default: throw new NfieldErrorException("Action did not complete successfully"); } }) .Unwrap()); }
public void InitializeNfieldConnection(INfieldConnectionClient connection) { ConnectionClient = connection; }
private static Uri BackgroundActivityUrl(this INfieldConnectionClient client, string activityId) { return(new Uri(client.NfieldServerUri, $"BackgroundActivities/{activityId}/")); }