Unity REST API Integration

This covers integrating and using our REST API in the Unity editor. All the files covered here are part of the Genvid Unity Package and are automatically installed when running the py unity.py build command.

  1. How to use the REST API.
  2. Genvid window usage.

How to use the REST API

As long as you include the dll files in your project, you will be able call the GenvidRESTCSharp API without any issues. In this section, we cover step-by-step how to make proper calls to the API.

BastionClustersAPI

  1. Create a new Genvid.Consul.Client.
    private static Genvid.Consul.Client _client = null;

    private static Genvid.Consul.Client Consul
    {
        get
        {
            if (_client == null)
            {
                _client = new Genvid.Consul.Client();
            }
            return _client;
        }
    }
  1. Get the service bastion-api from the Consul client created in the last step.
  2. Perform a GetEnumerator on the service and then get the Current on the service.
  3. Create the string: http:// + the current service ServiceAddress + : + the current service ServicePort + /v1.
    private static string BastionUrl
    {
        get
        {
            var iservices = Consul.health.service("bastion-api");
            var services = iservices.GetEnumerator();
            if (!services.MoveNext())
            {
                projectAvailable = false;
                return "no project loaded";
            }
            else
            {
                projectAvailable = true;
            }
            var service = services.Current;
            return string.Format("http://{0}:{1}/v1", service.Service.Address, service.Service.Port);
        }
    }
  1. Create a new Genvid.Api.BastionClustersApi object using the string you created as a parameter.
  2. You can access the available clusters with the Genvid.Api.BastionClustersApi object. You need to do a getClusters() on the Genvid.Api.BastionClustersApi object which returns a list of Genvid.Model.Bastion.ClusterOperator.
                var tempBastion = new Genvid.Api.BastionClustersApi(BastionUrl);
                listClusterOperator = tempBastion.getClusters();
                listClusterOperator.Sort(delegate (Genvid.Model.Bastion.ClusterOperator elementCompare1, Genvid.Model.Bastion.ClusterOperator elementCompare2)
                {
                    return elementCompare1.id.CompareTo(elementCompare2.id);
                });
  1. Each Genvid.Model.Bastion.ClusterOperator in the list contains the same type of structure:

    ClusterOperator

    A Cluster from the Bastion-API.

    Object Properties:
     
    • id (string) – The cluster ID
    • instanceID (string) – The terraform instance ID
    • source (string) – The terraform source location
    • category (string) – The cluster category
    JSON Example
    {
        "instanceID": "fvTzRSWIqVgISAZONqyA",
        "source": "SefmKXEfXKciHItzfHLK",
        "id": "GslOmnPCGBigRZSoXrRr",
        "category": "eZgdTVXArBRiCOpGGCPs"
    }
    

HealthAPI

  1. Create a new Genvid.Consul.Client.
    private static Genvid.Consul.Client _client = null;

    private static Genvid.Consul.Client Consul
    {
        get
        {
            if (_client == null)
            {
                _client = new Genvid.Consul.Client();
            }
            return _client;
        }
    }
  1. Get the service bastion-api from the Consul client you created in the previous step.
  2. Perform a GetEnumerator on the service and then get the Current on the service.
  3. Create the string: http:// + the current service ServiceAddress + : + the current service ServicePort + /v1/proxy/ + theClusterName + /cluster-api/v1/.
    private string ClusterUrl
    {
        get
        {
            var iservices = Consul.health.service("bastion-api");
            var services = iservices.GetEnumerator();
            if (!services.MoveNext())
            {
                projectAvailable = false;
                return "no project loaded";
            }
            else
            {
                projectAvailable = true;
            }
            var service = services.Current;
            return string.Format("http://{0}:{1}/v1/proxy/{2}/cluster-api/v1", service.Service.Address, service.Service.Port, optionsCluster[currentSelectionCluster]);
        }
    }
  1. Create a new Genvid.Api.HealthApi object using the string you created as a parameter.
  2. With the Genvid.Api.HealthApi object, you can access the health service of your choice. In our case, we want to access the SDK Health check by passing the service name as a parameter to getServiceHealth.
                    var healthApi = new Genvid.Api.HealthApi(stringCluster);
                    HealthCheckList = healthApi.getServiceHealth("SDK Health check");
  1. Afterwards, you can use the Genvid.Api.HealthApi object list to access data from it. In this case, you need the first element of the list.

    If you want a better format for the Output string, you can deserialize it as a Genvid.Model.Cluster.HealthCheckOutput. Before attempting to deserialize the Output string, make sure you set the object Status to passing.

                    if (HealthCheckList.Count > 0)
                    {
                        if (HealthCheckList[healthCheckSelection].Status == "passing")
                        {
                            currentHealthCheckOutput = Newtonsoft.Json.JsonConvert.DeserializeObject<Genvid.Model.Cluster.HealthCheckOutput>(HealthCheckList[healthCheckSelection].Output);
                        }
                        else
                        {
                            currentHealthCheckOutput = null;
                        }
                    }
                    else
                    {
                        currentHealthCheckOutput = null;
                    }

See SDK Health Check for more information about the structure contents for the health check.

JobsAPI

  1. Create a new Genvid.Consul.Client.
    private static Genvid.Consul.Client _client = null;

    private static Genvid.Consul.Client Consul
    {
        get
        {
            if (_client == null)
            {
                _client = new Genvid.Consul.Client();
            }
            return _client;
        }
    }
  1. Get the service bastion-api you created in the previous step from the Consul client.
  2. Perform a GetEnumerator on the service and then get the Current on the service.
  3. Create the string: http:// + the current service ServiceAddress + : + the current service ServicePort + /v1/proxy/ + theClusterName + /cluster-api/v1/.
    private string ClusterUrl
    {
        get
        {
            var iservices = Consul.health.service("bastion-api");
            var services = iservices.GetEnumerator();
            if (!services.MoveNext())
            {
                projectAvailable = false;
                return "no project loaded";
            }
            else
            {
                projectAvailable = true;
            }
            var service = services.Current;
            return string.Format("http://{0}:{1}/v1/proxy/{2}/cluster-api/v1", service.Service.Address, service.Service.Port, optionsCluster[currentSelectionCluster]);
        }
    }
  1. Create a new Genvid.Api.JobsApi object using the string you created as a parameter.
  2. Use the Genvid.Api.JobsApi object to perform the available tasks startJob(properJobName) or stopJob(properJobName).
    public void connectToWebJobs(int typeExecution, string jobName)
    {
        var api = new Genvid.Api.JobsApi(ClusterUrl);

        if (typeExecution == startJob)
        {
            api.startJob(jobName);
            UnityEngine.Debug.Log(jobName + " is started !");
        }
        else if (typeExecution == stopJob)
        {
            api.stopJob(jobName);
            UnityEngine.Debug.Log(jobName + " is stopped !");
        }
    }

LinksAPI

  1. Create a new Genvid.Consul.Client.
    private static Genvid.Consul.Client _client = null;

    private static Genvid.Consul.Client Consul
    {
        get
        {
            if (_client == null)
            {
                _client = new Genvid.Consul.Client();
            }
            return _client;
        }
    }
  1. Get the service bastion-api From the Consul client you created in the previous step.
  2. Perform a GetEnumerator on the service and then get the Current on the service.
  3. Create the string: http:// + the current service ServiceAddress + : + the current service ServicePort + /v1/proxy/ + theClusterName + /cluster-api/v1/.
    private string ClusterUrl
    {
        get
        {
            var iservices = Consul.health.service("bastion-api");
            var services = iservices.GetEnumerator();
            if (!services.MoveNext())
            {
                projectAvailable = false;
                return "no project loaded";
            }
            else
            {
                projectAvailable = true;
            }
            var service = services.Current;
            return string.Format("http://{0}:{1}/v1/proxy/{2}/cluster-api/v1", service.Service.Address, service.Service.Port, optionsCluster[currentSelectionCluster]);
        }
    }
  1. Create a new Genvid.Api.JobsApi object using the string you created as a parameter.
  2. Use the Genvid.Api.LinksApi object to perform a getLinks with the proper category and JobName as parameters.
  3. Perform an Application.OpenUrl on the .href for each object of the list returned.
    public void openLink(string name, string url)
    {
        UnityEngine.Debug.Log(string.Format("Opening {0} at {1}", name, url));
        Application.OpenURL(url);
    }

SettingsAPI

  1. Create a new Genvid.Consul.Client.
    private static Genvid.Consul.Client _client = null;

    private static Genvid.Consul.Client Consul
    {
        get
        {
            if (_client == null)
            {
                _client = new Genvid.Consul.Client();
            }
            return _client;
        }
    }
  1. Get the service bastion-api from the Consul client you created in the previous step.
  2. Perform a GetEnumerator on the service and then get the Current on the service.
  3. Create the string: http:// + the current service ServiceAddress + : + the current service ServicePort + /v1/proxy/ + theClusterName + /cluster-api/v1/.
    private string ClusterUrl
    {
        get
        {
            var iservices = Consul.health.service("bastion-api");
            var services = iservices.GetEnumerator();
            if (!services.MoveNext())
            {
                projectAvailable = false;
                return "no project loaded";
            }
            else
            {
                projectAvailable = true;
            }
            var service = services.Current;
            return string.Format("http://{0}:{1}/v1/proxy/{2}/cluster-api/v1", service.Service.Address, service.Service.Port, optionsCluster[currentSelectionCluster]);
        }
    }
  1. Create a new Genvid.Api.JobsApi object using the string you created as a parameter.
  2. Use the Genvid.Api.SettingsApi object to perform a getSettings which returns a Genvid.Model.Cluster.Settings object.
    private void loadSettings()
    {
        var tempSettings = new Genvid.Api.SettingsApi(ClusterUrl).getSettings();
        var tempSettingsProperties = tempSettings.GetType().GetProperties();

        if (oldSettingsLoaded == null || compareSettings(tempSettingsProperties, tempSettings, oldSettingsToSendLoaded, oldSettingsLoaded) == false)
        {
            oldSettingsLoaded = tempSettings;
            oldSettingsToSendLoaded = oldSettingsLoaded.GetType().GetProperties();
            var tempSettingsNew = new Genvid.Api.SettingsApi(ClusterUrl).getSettings();
            var tempSettingsPropertiesNew = tempSettingsNew.GetType().GetProperties();
            newSettings = tempSettingsNew;
            settingsToSend = tempSettingsPropertiesNew;
        }
    }
  1. You can also use the Genvid.Api.SettingsApi object to perform a setSettings with a Genvid.Model.Cluster.Settings object as a parameter to save your settings.
    public void saveSettings(Genvid.Model.Cluster.Settings newSettings)
    {
        var api = new Genvid.Api.SettingsApi(ClusterUrl);

        api.setSettings(newSettings);
    }

Genvid window usage

Warning

This feature is currently in beta. The window is functional, but we expect to make some changes in the future to add new features and improve the user experience. We welcome your feedback on the process.

A Genvid window is now available in the Window dropdown menu. This menu is only available if you integrated files from Cluster-API into your project along with GenvidWindow.cs as indicated at the top of this page.

To see the content in this window, your Genvid Bastion must be set up and the Unity project loaded as explained previously. This gives you some direct access to the different websites available, as well as the ability to control some jobs running in your local cluster.

Genvid Window Unity

Fig. 64 The Genvid Window for REST API inside the Unity Editor.

The following sections give more details on each section of the window and their functions.

Clusters

We display all the clusters currently available for the project in a combo list. < Select a Cluster > is the default selection. The number of clusters available is displayed beside the foldout. Make sure to select the proper cluster when using the SDK with this window.

Health check

In this section, we describe the content of the Genvid.Model.Cluster.HealthCheckOutput.State from the service SDK Health check. The contents include:

  1. ComposeConnected is the Compose connection status.
  2. NatsConnected is the NATS connection status.
  3. NumStreams is the number of streams connected.

This section only appears when the game is running, otherwise it displays the error message No project currently running. At the top of this section, there is a summary of the number of streams active and if both Compose and NATS are connected.

Jobs

A foldout lists all the jobs available for your project along a Start All button. Opening the foldout displays a button labeled On/Off. The button is green when the job is activated and red when deactivated.

Click on the button to start or stop a job. All the tasks associated with the jobs are listed in the foldout when a job is active.

Logs

This section only include a button Open Link and upon clicking on it, the logs page is displayed.

Settings

All the settings associated with the project are listed within different foldouts. Beside the foldout, we have the info.name, the encode.output.width, the encode.output.height and the audio status encode.input.silent. You can modify the settings by changing their value either via a textbox or checkbox.

Note: You can’t write characters in a field that only contains numbers.

There are two buttons available at the bottom of the page:

  • Reload settings reloads the settings (useful for reverting
    incorrect settings).
  • Save Settings saves changes to the settings made in this
    window.

See The Genvid Settings for more information about each setting.