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
local.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 local.py run-script 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 local.py run-script 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 local.py run-script 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("cluster-api");
var services = iservices.GetEnumerator();
if (!services.MoveNext())
{
throw new System.Exception("Service cluster-api not found!");
}
var service = services.Current;
return string.Format("http://{0}:{1}/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.jobs.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("cluster-api");
var services = iservices.GetEnumerator();
if (!services.MoveNext())
{
throw new System.Exception("Service cluster-api not found!");
}
var service = services.Current;
return string.Format("http://{0}:{1}/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.links.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);
}
}