Add GetRaw, PatchRaw, Delete methods to ApiClient.cs
This commit is contained in:
1 parent
c980e1b6d5
commit
94b20f1347
1 file changed
+67
-37
@@ -53,20 +53,35 @@ namespace CommanderAILab.Services
|
||||
return StartCoroutine(PostRawCoroutine(path, json, onSuccess, onError));
|
||||
}
|
||||
|
||||
public Coroutine GetRaw(string path, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return StartCoroutine(GetRawCoroutine(path, onSuccess, onError));
|
||||
}
|
||||
|
||||
public Coroutine PatchRaw(string path, string json, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return StartCoroutine(PatchRawCoroutine(path, json, onSuccess, onError));
|
||||
}
|
||||
|
||||
public Coroutine Delete(string path, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return StartCoroutine(DeleteCoroutine(path, onSuccess, onError));
|
||||
}
|
||||
|
||||
// -- Coroutine implementations --------------------------------------
|
||||
|
||||
private IEnumerator GetCoroutine<T>(string path, Action<T> onSuccess, Action<string> onError)
|
||||
{
|
||||
var url = $"{baseUrl}{path}";
|
||||
using var request = UnityWebRequest.Get(url);
|
||||
request.timeout = (int)timeoutSeconds;
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.LogWarning($"[ApiClient] GET {path} failed: {request.error}");
|
||||
onError?.Invoke(request.error);
|
||||
yield break;
|
||||
}
|
||||
|
||||
var obj = JsonUtility.FromJson<T>(request.downloadHandler.text);
|
||||
onSuccess?.Invoke(obj);
|
||||
}
|
||||
@@ -81,14 +96,12 @@ namespace CommanderAILab.Services
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
request.timeout = (int)timeoutSeconds;
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.LogWarning($"[ApiClient] POST {path} failed: {request.error}");
|
||||
onError?.Invoke(request.error);
|
||||
yield break;
|
||||
}
|
||||
|
||||
var obj = JsonUtility.FromJson<TRes>(request.downloadHandler.text);
|
||||
onSuccess?.Invoke(obj);
|
||||
}
|
||||
@@ -102,14 +115,63 @@ namespace CommanderAILab.Services
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
request.timeout = (int)timeoutSeconds;
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.LogWarning($"[ApiClient] POST {path} failed: {request.error}");
|
||||
onError?.Invoke(request.error);
|
||||
yield break;
|
||||
}
|
||||
onSuccess?.Invoke(request.downloadHandler.text);
|
||||
}
|
||||
|
||||
private IEnumerator PatchRawCoroutine(string path, string json, Action<string> onSuccess, Action<string> onError)
|
||||
{
|
||||
var url = $"{baseUrl}{path}";
|
||||
using var request = new UnityWebRequest(url, "PATCH");
|
||||
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
request.timeout = (int)timeoutSeconds;
|
||||
yield return request.SendWebRequest();
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.LogWarning($"[ApiClient] PATCH {path} failed: {request.error}");
|
||||
onError?.Invoke(request.error);
|
||||
yield break;
|
||||
}
|
||||
onSuccess?.Invoke(request.downloadHandler.text);
|
||||
}
|
||||
|
||||
private IEnumerator DeleteCoroutine(string path, Action<string> onSuccess, Action<string> onError)
|
||||
{
|
||||
var url = $"{baseUrl}{path}";
|
||||
using var request = UnityWebRequest.Delete(url);
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.timeout = (int)timeoutSeconds;
|
||||
yield return request.SendWebRequest();
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.LogWarning($"[ApiClient] DELETE {path} failed: {request.error}");
|
||||
onError?.Invoke(request.error);
|
||||
yield break;
|
||||
}
|
||||
onSuccess?.Invoke(request.downloadHandler.text);
|
||||
}
|
||||
|
||||
// -- Raw GET helper (returns JSON string) ---------------------------
|
||||
|
||||
private IEnumerator GetRawCoroutine(string path, Action<string> onSuccess, Action<string> onError)
|
||||
{
|
||||
var url = $"{baseUrl}{path}";
|
||||
using var request = UnityWebRequest.Get(url);
|
||||
request.timeout = (int)timeoutSeconds;
|
||||
yield return request.SendWebRequest();
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.LogWarning($"[ApiClient] GET {path} failed: {request.error}");
|
||||
onError?.Invoke(request.error);
|
||||
yield break;
|
||||
}
|
||||
onSuccess?.Invoke(request.downloadHandler.text);
|
||||
}
|
||||
|
||||
@@ -129,7 +191,6 @@ namespace CommanderAILab.Services
|
||||
}
|
||||
|
||||
// -- Collection -----------------------------------------------------
|
||||
// GET /api/collection
|
||||
|
||||
public Coroutine GetCollection(Action<string> onSuccess, Action<string> onError = null, string search = null)
|
||||
{
|
||||
@@ -140,41 +201,35 @@ namespace CommanderAILab.Services
|
||||
}
|
||||
|
||||
// -- Decks ----------------------------------------------------------
|
||||
// GET /api/decks
|
||||
|
||||
public Coroutine GetDecks(Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return StartCoroutine(GetRawCoroutine("/api/decks", onSuccess, onError));
|
||||
}
|
||||
|
||||
// POST /api/decks
|
||||
public Coroutine CreateDeck(string deckJson, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return PostRaw("/api/decks", deckJson, onSuccess, onError);
|
||||
}
|
||||
|
||||
// -- Simulator ------------------------------------------------------
|
||||
// POST /api/lab/start
|
||||
|
||||
public Coroutine StartSim(string configJson, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return PostRaw("/api/lab/start", configJson, onSuccess, onError);
|
||||
}
|
||||
|
||||
// GET /api/lab/status/{id}
|
||||
public Coroutine GetSimStatus(string simId, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return StartCoroutine(GetRawCoroutine($"/api/lab/status/{simId}", onSuccess, onError));
|
||||
}
|
||||
|
||||
// GET /api/lab/results/{id}
|
||||
public Coroutine GetSimResults(string simId, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return StartCoroutine(GetRawCoroutine($"/api/lab/results/{simId}", onSuccess, onError));
|
||||
}
|
||||
|
||||
// -- Coach ----------------------------------------------------------
|
||||
// POST /api/coach/chat
|
||||
|
||||
public Coroutine CoachChat(string messageJson, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
@@ -182,7 +237,6 @@ namespace CommanderAILab.Services
|
||||
}
|
||||
|
||||
// -- Deck Generator -------------------------------------------------
|
||||
// POST /api/deckgen/v3
|
||||
|
||||
public Coroutine GenerateDeck(string requestJson, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
@@ -190,21 +244,18 @@ namespace CommanderAILab.Services
|
||||
}
|
||||
|
||||
// -- Meta -----------------------------------------------------------
|
||||
// GET /api/lab/meta/commanders
|
||||
|
||||
public Coroutine GetCommanders(Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return StartCoroutine(GetRawCoroutine("/api/lab/meta/commanders", onSuccess, onError));
|
||||
}
|
||||
|
||||
// GET /api/lab/precons
|
||||
public Coroutine GetPrecons(Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
return StartCoroutine(GetRawCoroutine("/api/lab/precons", onSuccess, onError));
|
||||
}
|
||||
|
||||
// -- Scanner --------------------------------------------------------
|
||||
// POST /api/scanner/scan
|
||||
|
||||
public Coroutine ScanCard(byte[] imageBytes, Action<string> onSuccess, Action<string> onError = null)
|
||||
{
|
||||
@@ -219,33 +270,12 @@ namespace CommanderAILab.Services
|
||||
using var request = UnityWebRequest.Post(url, form);
|
||||
request.timeout = (int)timeoutSeconds;
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.LogWarning($"[ApiClient] POST /api/scanner/scan failed: {request.error}");
|
||||
onError?.Invoke(request.error);
|
||||
yield break;
|
||||
}
|
||||
|
||||
onSuccess?.Invoke(request.downloadHandler.text);
|
||||
}
|
||||
|
||||
// -- Raw GET helper (returns JSON string) ---------------------------
|
||||
|
||||
private IEnumerator GetRawCoroutine(string path, Action<string> onSuccess, Action<string> onError)
|
||||
{
|
||||
var url = $"{baseUrl}{path}";
|
||||
using var request = UnityWebRequest.Get(url);
|
||||
request.timeout = (int)timeoutSeconds;
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
Debug.LogWarning($"[ApiClient] GET {path} failed: {request.error}");
|
||||
onError?.Invoke(request.error);
|
||||
yield break;
|
||||
}
|
||||
|
||||
onSuccess?.Invoke(request.downloadHandler.text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user