Example #1
0
    // ============================

    // coroutine: save the user profile
    IEnumerator saveUser(string username, User user)
    {
        saving = true;
        string oldName = user.Name;

        user.Name = username; // update user

        // do the request
        var headers = new Dictionary <string, string>();

        headers["Content-Type"] = "application/json";
        WWW www = new WWW(WebCs.UsersUrl(), FileCs.DefaultEncoding.GetBytes(user.ToJson()), headers);

        yield return(www);

        // check result
        saving = false;
        if (www.error != null)
        {
            user.Name = oldName; // revert
            Debug.Log(www.error);
            StatusText.text = "Error saving user : "******"Saved.";
        }
    }
Example #2
0
 // retrieve metas from the server
 private List <ImageMetas> threadDownloadMetas(int nbr)
 {
     try
     {
         using (WebClient metasWebClient = new WebClient())
         {
             metasWebClient.Encoding = Encoding.UTF8;
             metasWebClient.Headers["Content-Type"] = "application/json";
             string jsonString = metasWebClient.UploadString(WebCs.ImageSuggestionsUrl(nbr),
                                                             User.CurrentUser.TagsVectorAsJson());
             return(ImageMetas.FromJsonArray(jsonString));
         }
     }
     catch (Exception e)
     {
         // TODO: handle error properly
         Debug.Log("error downloading metas " + e);
         Debug.Log(User.CurrentUser.TagsVectorAsJson());
         return(new List <ImageMetas>());
     }
 }
        private void getAndDownload(string id)
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers["Content-Type"]  = "application/json";
                webClient.DownloadStringCompleted += (o, e) =>
                {
                    if (e.Error != null)
                    {
                        UnityEngine.Debug.Log("error downloading image " + id);
                    }
                    else
                    {
                        var metas = ImageMetas.FromJson(e.Result);
                        var uri   = new Uri(metas.Url.Replace("https://", "http://"));
                        webClient.DownloadFileAsync(uri, Path.Combine(path, id + "." + metas.Format));
                    }
                };

                webClient.DownloadStringAsync(new Uri(WebCs.ImageDetailsUrl(id)));
            }
        }
    // =================================================

    // coroutine: load the list of profiles from the server
    IEnumerator getUsersList(Action <string> complete)
    {
        WWW www = new WWW(WebCs.UsersUrl("all"));

        yield return(www);

        if (www.error != null)
        {
            complete(www.error);
        }
        else
        {
            try
            {
                usernames = JsonConvert.DeserializeObject <List <Username> >(www.text);
                complete(null);
            }
            catch (Exception e)
            {
                complete(e.Message);
            }
        }
    }
    // =================================================

    // coroutine: get the user profile from its id
    IEnumerator loadUser(string username, Action <User, string> complete)
    {
        WWW www = new WWW(WebCs.UsersUrl(username));

        yield return(www);

        if (www.error != null)
        {
            complete(null, www.error);
        }
        else
        {
            try
            {
                var user = User.FromJson(www.text);
                complete(user, null);
            }
            catch (Exception e)
            {
                Debug.Log(e);
                complete(null, e.Message);
            }
        }
    }