# Fetch Data from sheet

To fetch data from the sheet I create a class and push that to the zenject. Here is the class:

```csharp
/// <summary>
/// Class to download list of events from the sheet
/// </summary>
public class FetchEventData : IFetchData<List<EventsData>>
{
    /// <summary>
    /// Event data loaded from the url
    /// </summary>
    [SerializeField]
    private List<EventsData> _eventsData = new List<EventsData>();

    /// <summary>
    /// downloaded data
    /// </summary>
    public List<EventsData> _downloadedData
    {
        get => _eventsData;
    }

    /// <summary>
    /// Fetch data from the given url
    /// </summary>
    /// <param name="url">url which is used to download</param>
    /// <param name="OnProgress">Raise when downloading is in progress</param>
    /// <param name="OnCompleted">Raise when Completed or Fail</param>
    /// <returns></returns>
    public IEnumerator FetchData(string url, Action<float, string> OnProgress, Action<bool, List<EventsData>> OnCompleted)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
        {
            // send request
            webRequest.SendWebRequest();

            // when data is loading
            while(!webRequest.isDone)
            {
                OnProgress?.Invoke(webRequest.downloadProgress, "Events Data");
                yield return null;
            }

            switch (webRequest.result)
            {
                case UnityWebRequest.Result.ConnectionError:
                    OnCompleted?.Invoke(false, null);
                    Debug.LogError("Error: " + webRequest.error);
                    break;
                case UnityWebRequest.Result.DataProcessingError:
                    OnCompleted?.Invoke(false, null);
                    Debug.LogError("Error: " + webRequest.error);
                    break;
                case UnityWebRequest.Result.ProtocolError:
                    OnCompleted?.Invoke(false, null);
                    Debug.LogError("Error: " + webRequest.error);
                    break;
                case UnityWebRequest.Result.Success:
                    Debug.Log("Received: " + webRequest.downloadHandler.text);
                    // Parse data using Newtonsoft
                    _eventsData = JsonConvert.DeserializeObject<List<EventsData>>(webRequest.downloadHandler.text);
                    OnCompleted?.Invoke(true, _eventsData);
                    break;
            }
        }
    }
}
```

In this class, I've employed a straightforward approach where I use a URL to make a *'Get Request'*. This allows me to retrieve all classes and parse them into a list using *'Newtonsoft JSON'*. You'll notice that this class implements the *'IFetchData'* interface. I've registered this class with Zenject to declare its dependency, ensuring it's provided whenever needed.

```csharp
Container.Bind<IFetchData<List<EventsData>>>().To<FetchEventData>().AsSingle();
```

Based on the events passed into the *'IEnumerator'*, I manage the display of the loading process and its progression.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://muhammad-taha-bin-farooq.gitbook.io/sheet-linker/fetch-in-unity/ugui/fetch-data-from-sheet.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
