IEnumerator GetProducts(int classId, int page, string sku, GameObject spinner, CallbackWithProducts callback) { string url = ConstructProductInfoUrlWith(classId, page, sku); using (WWW www = new WWW(url, null, headers)) { // Cancel WWW if it exceeds timeout float timeSinceDownloadStarted = -1f; bool requestCancelled = false; // Enable the loading spinner if (spinner != null) { spinner.SetActive(true); } while (!www.isDone) { timeSinceDownloadStarted += 1f; if (timeSinceDownloadStarted > Constants.TIMEOUT) { Debug.LogWarning("Download timeout!"); requestCancelled = true; break; } Debug.Log("Still downloading... " + www.progress); yield return(new WaitForSeconds(1f)); } // Once download is complete // If request was cancelled due to a timeout if (requestCancelled) { Debug.LogWarning("Request cancelled"); StopCoroutine("GetProducts"); // Disable the loading spinner if (spinner != null) { spinner.SetActive(false); } if (callback != null) { callback(null); } // Done } else { yield return(www); // Disable the loading spinner if (spinner != null) { spinner.SetActive(false); } // If there was an error with the request if (www.error != null) { Debug.LogWarning("WWW download had an error:" + www.error); if (callback != null) { callback(null); } // Done } else { string response = www.text; Debug.Log("Response - " + response); // Wayfair API is not returning HTTP status as yet, // so if the response was just a string that starts with Error, there was some error if (response.StartsWith("\"Error")) { Debug.LogWarning("Got error - " + response); if (callback != null) { callback(null); } // Done } else { // Try to parse the JSON response try { JsonData responseJson = JsonMapper.ToObject(response); // If response contains an array, its probably a list of products if (responseJson.IsArray) { List <Product> products = JsonMapper.ToObject <List <Product> > (response); Debug.Log("Got page - " + products); if (callback != null) { callback(products); } } // Otherwise its a single product else if (responseJson.IsObject) { Product product = JsonMapper.ToObject <Product> (response); Debug.Log("Got product - " + product); if (callback != null) { List <Product> products = new List <Product> (); products.Add(product); callback(products); } } // Done } catch (Exception e) { Debug.LogWarning("JSON Parsing error - " + e.Message); if (callback != null) { callback(null); } // Done } } } } } // memory is freed from the web stream (www.Dispose() gets called implicitly) }
/// <summary> /// Loads the image from URL /// </summary> /// <returns>The image from URL.</returns> /// /// <param name="url">URL - The URL of the image</param> /// <param name="image">Image - Unity Image to display the image</param> // public static IEnumerator LoadImageFromURL(string url, Image image) { // // Start a download of the given image URL // WWW www = new WWW(url); // // // Wait for download to complete // yield return www; // // yield return www; // if (www.error != null) { // Debug.LogWarning("Image download had an error:" + www.error); // } // else { // // Assign texture // Sprite sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0.5f, 0.5f)); // image.sprite = sprite; // } // } /// <summary> /// Gets products from the Wayfair 3D Model API. /// </summary> /// <param name="classId">Class identifier - See documentation on what ID to use for the different classes</param> /// <param name="page">Page - Page to request</param> /// <param name="sku">Sku - Individual SKU if you're looking for a single product</param> /// <param name="callback">Callback - Called when product information is ready</param> public void GetProducts(int classId, int page, string sku, CallbackWithProducts callback) { StartCoroutine(GetProducts(classId, page, sku, loadingIndicator, callback)); }