- Home /
 
Recursive Coroutine-starts with RestSharp stops at the first callback which starts another coroutine of the same
I have been using UnityWebRequest for REST calls through Unity but for a certain project I had to choose another option and started testing RestSharp for Unity (https://github.com/llamazoo/RestSharp-for-unity3d).
The application contains a lot of API calls, therefore I have made a single static class with a static IEnumerator to send the requests and perform a callback to the monobehaviour class which started the coroutine.
There are some methods which Starts the API request coroutine, get the response and performs a callback which again Starts another coroutine of the same IEnumerator. All this works well with UnityWebRequests.
I have tried to do the same with RestSharp, adding a yield to the Async Execute of the API request. The first API call (let's say to endpoint-1) works well, receives the response and performs the callback with data from endpoint-1 as parameter. The callback method formats the data, and calls the same RestSharp IEnumarator with new data to endpoint-2. Here, the static method breaks out of the method as well as the Coroutine. (Which allows only single API call with a response per moment)
The following is my static class
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using RestSharp;
 using System.Net;
 
 public static class RestSharpUtil
 {
     public static IEnumerator request_routine_POST(string url,string json, System.Action<string> callback, System.Action<string> failCallback=null){
         Debug.Log("Request :  "+json);
 
 
         RestClient w = new RestClient(url);
         RestRequest request = new RestRequest(Method.POST);
 
 
 
         request.AddHeader("Content-Type", "application/json");
         request.AddHeader("Accept", "application/json");
 
         request.Parameters.Clear();
 
         request.AddParameter("application/json", json, ParameterType.RequestBody);
 
         yield return w.ExecuteAsync(request,(response,AsyncHandler)=> {
 
             if(response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.Accepted){
                 Debug.Log("Success!-> " + response.Content);
 
                 callback(response.Content);
                 Debug.Log("After Request");
 
             }else{
 
                 LoadingPanel.ShowError("Something went wrong, Try again!");
 
 
             }
         });
 
         Debug.Log("Action Complete");
     }
 }
 
               And the following are my methods (main one and the callback)
 public void test(){
 
        string customerdata= "json1";
 
        StartCoroutine(RestSharpUtil.request_routine_POST(endpoint-1, customerdata, OnResponse));
 
  }
 
 void OnResponse(string res){
 
         string otherdata = "some processed data with res";
 
         StartCoroutine(RestSharpUtil.request_routine_POST(endpoint-2, otherdata, OnResponse2));
 
 }
 
               OnResponse2 is just a blank method to show the content. However, performing the Coroutine in OnResponse prints the earlier request, not the new one.
My Result is,
 Request : json1
 Action Complete
 Success -> response
 Success -> response
 
               It never prints the "Action Complete" or "After Request". What am I doing wrong here?
Your answer