Cluster Api integration¶
This section will cover how to integrate and use the cluster Api in the Unity editor.
Files needed for Cluster Api¶
Three files are currently required to be integrated into your project to fully use our Cluster Api without issue. If you want to run the current sample, you don’t need to copy the files in the sample. By using unity.py, you can build the project and run it without having to manually copy any file.
GenvidRESTCSharp.dll¶
This is the file created by Genvid and it is used to make all the calls related to RestSharp.
This file needs to be placed inside the Plugins folder of your
project. The command script of the project handles this for you
when you run py unity.py build
.
As a developer, you are free to make any call from this dll directly, though we recommend using the C# script created to interact with it.
Newtonsoft.Json.dll¶
This is a file needed for the GenvidRESTCSharp.dll.
This file needs to be placed inside the Plugins
folder of your
project. The command script of the project handles this for you when
you run py unity.py build
.
RestSharp.dll¶
This is a file needed for the GenvidRESTCSharp.dll.
This file needs to be placed inside the Plugins
folder of your
project. The command script of the project handles this for you when
you run py unity.py build
.
Optional file - GenvidProject.cs¶
This is a file that is used to generate the menu items. When integrated
in your project, simply go into the menu Genvid
and select
Generate menu items
to create the menu based on the Jobs and Links
available in your project.
This file needs to be placed inside the Assets
folder of your
project.
How to use the Cluster Api¶
As long as the dll are included in your project, you will be able to do the proper call to the GenvidRESTCSharp without any issue. In this section, we will cover step by step how to make proper call to the API:
JobsApi¶
- Create a new
Genvid.Consult.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;
}
}
- From the consul client created in the last step, get the service
cluster-api
. - Perform a
GetEnumerator
on the service and then get theCurrent
on the service. - Assemble into a string:
http://
+ the current service ServiceAddress +:
+ the current service ServicePort +/v1
private static string ClusterUrl
{
get
{
var iservices = Consul.catalog.service("bastion-api");
var services = iservices.GetEnumerator();
if (!services.MoveNext())
{
throw new System.Exception("Service bastion-api not found!");
}
var service = services.Current;
return string.Format("http://{0}:{1}/v1/cluster/local/v1", service.ServiceAddress, service.ServicePort);
}
}
- Use the string created in the last step to create a new
Genvid.jobs.JobsApi
object with the string as a parameter. - Use the
Genvid.jobs.JobsApi
object to perform any task available:startJob(properJobName)
orstopJob(properJobName)
.
public static void connectToWebJobs(int typeExecution, string jobName)
{
var api = new Genvid.Api.JobsApi(ClusterUrl);
if (typeExecution == 0)
{
api.startJob(jobName);
UnityEngine.Debug.Log(jobName + " is started !");
}
else if (typeExecution == 1)
{
api.stopJob(jobName);
UnityEngine.Debug.Log(jobName + " is stopped !");
}
}
LinksApi¶
- Create a new
Genvid.Consult.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;
}
}
- From the consul client created in the last step, get the service
cluster-api
. - Perform a
GetEnumerator
on the service and then get theCurrent
on the service. - Assemble into a string:
http://
+ the current service ServiceAddress +:
+ the current service ServicePort +/v1
private static string ClusterUrl
{
get
{
var iservices = Consul.catalog.service("bastion-api");
var services = iservices.GetEnumerator();
if (!services.MoveNext())
{
throw new System.Exception("Service bastion-api not found!");
}
var service = services.Current;
return string.Format("http://{0}:{1}/v1/cluster/local/v1", service.ServiceAddress, service.ServicePort);
}
}
- Use the string created in the last step to create a new
Genvid.jobs.LinksApi
object with the string as a parameter. - Use the
Genvid.jobs.LinksApi
object to perform agetLinks
with the proper category and properJobName as parameter. - Perform an
Application.OpenUrl
on the.href
for each object of the list returned.
public static void openLink(string name, string category = "")
{
var api = new Genvid.Api.LinksApi(ClusterUrl);
var links = api.getLinks(category, name);
foreach (var link in links)
{
UnityEngine.Debug.Log(string.Format("Opening {0} at {1}", link.name, link.href));
Application.OpenURL(link.href);
}
}
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 improve the user experience with more features also. Your feedback on this process is welcome.
A Genvid window is now available in the Window dropdown menu. This menu is available only if the
files from Cluster-API are integrated into your project along GenvidWindow.cs
and
GenvidProject.cs
as indicated at the top of this page.
To be able to see the content in this window, your Genvid Bastion must be set up, and the unity project loaded like explain previously. This will give you some direct access to the different websites available, as well as the ability to control some of the jobs running in the local cluster.