Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Commit 5b35b74

Browse files
committed
ARgorithm Execution and StateSet Model implemented
1 parent c6e97a5 commit 5b35b74

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

Assets/Scripts/ARgorithmAPI/APIClient.cs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ The ConnectionResponse.status can have following values
5959
webRequest.downloadHandler.text
6060
);
6161
serverEndpoint = url;
62-
Debug.Log(serverEndpoint);
6362
if (response.auth == "ENABLED")
6463
{
6564
callback(new ConnectionResponse {
@@ -209,7 +208,6 @@ Returns list of ARgorithms
209208

210209
if(webRequest.isDone){
211210
string value = "{\"items\":" + webRequest.downloadHandler.text + "}";
212-
Debug.Log(value);
213211
ARgorithmCollection response = JsonConvert.DeserializeObject<ARgorithmCollection>(value);
214212
callback(response);
215213
}
@@ -264,39 +262,42 @@ The LoginResponse.status can have following values
264262
}
265263
}
266264

267-
/*
268-
BELOW CODE IS ATTEMPT AT RUNNING ARGOTIHMS
269-
IGNORE FOR NOW , WILL IMPLEMENT LATER
270-
*/
271-
272-
// public IEnumerator run(ExecutionRequest exec,System.Action<string> callback){
273-
// string body = JsonUtility.ToJson(exec);
274-
// Debug.Log(body);
275-
// using(UnityWebRequest webRequest = UnityWebRequest.Post(serverEndpoint+"/argorithms/run",body)){
276-
// webRequest.SetRequestHeader("authorization","Bearer "+token);
277-
// webRequest.SetRequestHeader("Content-Type","application/json");
278-
// yield return webRequest.SendWebRequest();
265+
public IEnumerator run(ExecutionRequest exec,System.Action<ExecutionResponse> callback){
266+
string body = JsonConvert.SerializeObject(exec);
267+
268+
using(UnityWebRequest webRequest = new UnityWebRequest(serverEndpoint+"/argorithms/run","POST")){
269+
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(body);
270+
webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
271+
webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
272+
webRequest.SetRequestHeader("authorization","Bearer "+token);
273+
webRequest.SetRequestHeader("Content-Type","application/json");
274+
yield return webRequest.SendWebRequest();
279275

280-
// if(webRequest.isNetworkError){
281-
// callback("FAILURE");
282-
// }
276+
if(webRequest.isNetworkError){
277+
callback(new ExecutionResponse{
278+
status="FAILURE",
279+
data={}
280+
});
281+
}
283282

284-
// if (webRequest.isDone){
285-
// switch (webRequest.responseCode)
286-
// {
287-
// case 200:
288-
// callback(webRequest.downloadHandler.text);
289-
// break;
283+
if (webRequest.isDone){
284+
switch (webRequest.responseCode)
285+
{
286+
case 200:
287+
ExecutionResponse res = JsonConvert.DeserializeObject<ExecutionResponse>(webRequest.downloadHandler.text);
288+
callback(res);
289+
break;
290290

291-
// default:
292-
// Debug.Log(webRequest.responseCode);
293-
// Debug.Log(webRequest.downloadHandler.text);
294-
// callback("TRY_AGAIN");
295-
// break;
296-
// }
297-
// }
298-
// }
299-
// }
291+
default:
292+
callback(new ExecutionResponse{
293+
status="FAILURE",
294+
data={}
295+
});
296+
break;
297+
}
298+
}
299+
}
300+
}
300301

301302
}
302303

Assets/Scripts/ARgorithmAPI/Models/Models.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using UnityEngine;
5+
using Newtonsoft.Json.Linq;
56

67
namespace ARgorithmAPI.Models{
78
/*
@@ -73,8 +74,8 @@ public class ARgorithm{
7374
public string description;
7475
public string function;
7576
public string filename;
76-
public object parameters;
77-
public object example;
77+
public JObject parameters;
78+
public JObject example;
7879
}
7980

8081
[Serializable]
@@ -85,4 +86,23 @@ public class ARgorithmCollection{
8586
public ARgorithm[] items;
8687
}
8788

89+
[Serializable]
90+
public class ExecutionRequest{
91+
public string argorithmID;
92+
public JObject parameters;
93+
}
94+
95+
[Serializable]
96+
public class State{
97+
public string state_type;
98+
public JObject state_def;
99+
public string comments;
100+
}
101+
102+
[Serializable]
103+
public class ExecutionResponse{
104+
public string status;
105+
public List<State> data;
106+
}
107+
88108
}

0 commit comments

Comments
 (0)