Skip to content
This repository was archived by the owner on Jun 24, 2019. It is now read-only.

Commit 7de04df

Browse files
committed
Don't decode/parse the response on the main thread.
Instead, offload the work to the background thread. Closes #10.
1 parent 459e037 commit 7de04df

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

Assets/PolyToolkit/Internal/api_clients/poly_client/PolyClient.cs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ namespace PolyToolkitInternal.api_clients.poly_client {
2626
/// Parses the response of a List Assets request from Poly into a PolyListResult.
2727
/// </summary>
2828
public class ParseAssetsBackgroundWork : BackgroundWork {
29-
private string response;
29+
private byte[] response;
3030
private PolyStatus status;
3131
private Action<PolyStatus, PolyListAssetsResult> callback;
3232
private PolyListAssetsResult polyListAssetsResult;
33-
public ParseAssetsBackgroundWork(string response, Action<PolyStatus, PolyListAssetsResult> callback) {
33+
public ParseAssetsBackgroundWork(byte[] response, Action<PolyStatus, PolyListAssetsResult> callback) {
3434
this.response = response;
3535
this.callback = callback;
3636
}
3737
public void BackgroundWork() {
38-
status = PolyClient.ParseReturnedAssets(response, out polyListAssetsResult);
38+
JObject result;
39+
status = PolyClient.ParseResponse(response, out result);
40+
if (status.ok) {
41+
status = PolyClient.ParseReturnedAssets(Encoding.UTF8.GetString(response), out polyListAssetsResult);
42+
}
3943
}
4044
public void PostWork() {
4145
callback(status, polyListAssetsResult);
@@ -45,16 +49,20 @@ public void PostWork() {
4549
/// Parses an asset from Poly into a PolyAsset.
4650
/// </summary>
4751
public class ParseAssetBackgroundWork : BackgroundWork {
48-
private string response;
52+
private byte[] response;
4953
private Action<PolyStatus,PolyAsset> callback;
5054
private PolyStatus status;
5155
private PolyAsset polyAsset;
52-
public ParseAssetBackgroundWork(string response, Action<PolyStatus,PolyAsset> callback) {
56+
public ParseAssetBackgroundWork(byte[] response, Action<PolyStatus,PolyAsset> callback) {
5357
this.response = response;
5458
this.callback = callback;
5559
}
5660
public void BackgroundWork() {
57-
status = PolyClient.ParseAsset(JObject.Parse(response), out polyAsset);
61+
JObject result;
62+
status = PolyClient.ParseResponse(response, out result);
63+
if (status.ok) {
64+
status = PolyClient.ParseAsset(result, out polyAsset);
65+
}
5866
}
5967
public void PostWork() {
6068
callback(status, polyAsset);
@@ -386,37 +394,12 @@ public void SendRequest(PolyRequest request, Action<PolyStatus, PolyListAssetsRe
386394
});
387395
}
388396
} else {
389-
string text = Encoding.UTF8.GetString(response);
390-
PolyStatus responseStatus = CheckResponseForError(text);
391-
if (!responseStatus.ok) {
392-
callback(responseStatus, null);
393-
return;
394-
}
395397
PolyMainInternal.Instance.DoBackgroundWork(new ParseAssetsBackgroundWork(
396-
text, callback));
398+
response, callback));
397399
}
398400
}, maxCacheAge);
399401
}
400402

401-
/// <summary>
402-
/// Verify if the response can be parsed as json and, if so, that it contains no error token.
403-
/// If either conditions are false return a PolyStatusError with relevant information.
404-
/// </summary>
405-
private PolyStatus CheckResponseForError(string response) {
406-
JObject results = new JObject();
407-
try {
408-
results = JObject.Parse(response);
409-
} catch (Exception ex) {
410-
return PolyStatus.Error("Failed to parse Poly API response, encountered exception: {0}", ex.Message);
411-
}
412-
IJEnumerable<JToken> error = results["error"].AsJEnumerable();
413-
if (error == null) {
414-
return PolyStatus.Success();
415-
}
416-
return PolyStatus.Error("{0}: {1}", error["code"] != null ? error["code"].ToString() : "(no error code)",
417-
error["message"] != null ? error["message"].ToString() : "(no error message)");
418-
}
419-
420403
/// <summary>
421404
/// Fetch a specific asset.
422405
/// </summary>
@@ -450,13 +433,7 @@ public void GetAsset(string assetId, Action<PolyStatus,PolyAsset> callback, bool
450433
});
451434
}
452435
} else {
453-
string text = Encoding.UTF8.GetString(response);
454-
PolyStatus responseStatus = CheckResponseForError(text);
455-
if (!responseStatus.ok) {
456-
callback(responseStatus, null);
457-
return;
458-
}
459-
PolyMainInternal.Instance.DoBackgroundWork(new ParseAssetBackgroundWork(text,
436+
PolyMainInternal.Instance.DoBackgroundWork(new ParseAssetBackgroundWork(response,
460437
callback));
461438
}
462439
}, DEFAULT_QUERY_CACHE_MAX_AGE_MILLIS);
@@ -475,5 +452,23 @@ public UnityWebRequest GetRequest(string path, string contentType) {
475452
}
476453
return request;
477454
}
455+
456+
public static PolyStatus ParseResponse(byte[] response, out JObject result) {
457+
try {
458+
result = JObject.Parse(Encoding.UTF8.GetString(response));
459+
JToken errorToken = result["error"];
460+
if (errorToken != null) {
461+
IJEnumerable<JToken> error = errorToken.AsJEnumerable();
462+
return PolyStatus.Error("{0}: {1}", error["code"] != null ? error["code"].ToString() : "(no error code)",
463+
error["message"] != null ? error["message"].ToString() : "(no error message)");
464+
} else {
465+
return PolyStatus.Success();
466+
}
467+
} catch (Exception ex) {
468+
result = null;
469+
return PolyStatus.Error("Failed to parse Poly API response, encountered exception: {0}", ex.Message);
470+
}
471+
472+
}
478473
}
479474
}

0 commit comments

Comments
 (0)