Types for getting data from Blue Alliance API

I am trying to get data off the the Blue Alliance API, and I have succeeded but I want to make the way I am getting it re-useable for all of the data you can get. How I initially set it up is the code below (and I am using a slightly different method that removes the while for links that don’t need a page number).

IEnumerator GetInformation<T>(List<T> list, string prePageLink, string postPageLink)
{
    list.Clear();

    string result = "";
    int index = 0;

    while (result != "[]\n")  
    {
        using (UnityWebRequest request = UnityWebRequest.Get($"{baseURL}{prePageLink}{index}{postPageLink}"))
        {
           request.SetRequestHeader(authKeyName, authKeyValue);

            yield return request.SendWebRequest();

            if (request.result == UnityWebRequest.Result.ConnectionError)
            {
                Debug.Log(request.error);
                yield break;
            }
            else
            {
                result = request.downloadHandler.text;
                if (result == "[]\n") yield break;

                result = ConvertToUseableJson(result);
                if (forced)
                {
                    var jsond = JsonHelper.FromJson<SimpleTeam>(result);
                    Debug.Log($"Forced, adding {jsond} to a {jsond.GetType()} list");
                    simpleYearlyTeamList.AddRange(jsond);
                }
                else
                {
                    var jsond = JsonHelper.FromJson<T>(result);
                    Debug.Log($"Not forced, adding {jsond} to a {jsond.GetType()} list");
                    list.AddRange(jsond);
                }
                index++;
            }
        }
    }
}

That last part of the code that uses the var jsond is the part that is going wrong. The first option which is under the if (forced) that specifically says the type I am using is <SimpleTeam> works perfectly fine, as it adds the information to the intended list in the correct way. The part where I use <T> on the other hand does not work. The debug logs (other than the Forced turning into Not forced) result in the exact same thing, and the list I am inputting as a method argument is the same simpleYearlyTeamList I am using in the forced area, and I am using the same link I am getting data from for both when testing so the JsonHelper.FromJson<T> should also be using SimpleTeam, which does seem to be what the Debug logs represent unless I am misunderstanding something. The issue is, although the data gets added to the list in the if (forced), it does not get added to the list in the else. I have attempted to do some research about this, but have not come up with anything that really helped me, and this is also my first time working with using <T> so I don’t really know how to go about fixing this issue.

In the (forced) case you are adding the data to “simpleYearlyTeamList,” and in the (!forced) case, you are adding it to “list.” Is this what you intended to do? The code you’ve included doesn’t appear to do anything with the contents of “list.” They will be discarded when the list is destructed when your function returns.

Yes I did. The variable list is of type List<T> which is used to allow me to input any list when I am calling that function. In the case of !forced, (at least when I was testing this) I was inputting simpleYearlyTeamList. This works as List<T> is a class, and classes are passed as references, instead of just creating a new temporary variable/function variable (unsure of actual name) as in the case where you are using data types. This means that when I add to list it will add to the list I input when I called the function, meaning that in both cases it would have been adding to the simpleYearlyTeamList.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.