BiasedBit.MinusEngine.MinusApi.SaveGallery C# (CSharp) Метод

SaveGallery() публичный Метод

Saves a gallery and makes it publicly accessible.
public SaveGallery ( String cookieHeader, String name, String galleryEditorId, String key, String items ) : void
cookieHeader String
name String Desired name for the gallery.
galleryEditorId String Gallery editor ID (obtained when created).
key String Editor key for the gallery (obtained when created).
items String /// The order in which the items will be displayed in the gallery. /// /// If you fail to include items that were uploaded to this gallery, those items will be /// discarded by the server. ///
Результат void
        public void SaveGallery(String cookieHeader, String name, String galleryEditorId, String key, String[] items)
        {
            // Get a pre-configured web client
            CookieAwareWebClient client = this.CreateAndSetupWebClient(cookieHeader);

            string jsonItems;

            // build the item list (the order in which the items will be shown)
            if (items != null && items.Count() > 0)
            {
                jsonItems = JsonConvert.SerializeObject(items);
            }
            else
            {
                jsonItems = "[]";
            }

            // Add the post data - must be as a string because WebClient doesn't do UrlEncode on all the
            // characters it's supposed to do. If I do UrlEncode() before submitting the webclient will
            // also perform url encoding on stuff that's already url encoded.
            StringBuilder data = new StringBuilder();
            data.Append("name=").Append(name)
            .Append("&id=").Append(galleryEditorId)
            .Append("&key=").Append(key)
            .Append("&items=").Append(UrlEncode(jsonItems));

            client.Headers["Content-Type"] = "application/x-www-form-urlencoded";

            // register the completion/error listener
            client.UploadStringCompleted += delegate(object sender, UploadStringCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    Debug.WriteLine("SaveGallery operation failed: " + e.Error.Message);
                    this.TriggerUploadItemFailed(e.Error);
                    #if !WINDOWS_PHONE
                        client.Dispose();
                    #endif
                    return;
                }

                Debug.WriteLine("SaveGallery operation successful.");
                this.TriggerSaveGalleryComplete();
                #if !WINDOWS_PHONE
                    client.Dispose();
                #endif
            };

            // submit as an asynchronous task
            try
            {
                ThreadPool.QueueUserWorkItem((object state) =>
                {
                    try
                    {
                        client.UploadStringAsync(SAVE_GALLERY_URL, "POST", data.ToString());
                    }
                    catch (WebException e)
                    {
                        Debug.WriteLine("Failed to access SaveGallery API: " + e.Message);
                        this.TriggerSaveGalleryFailed(e);
                        #if !WINDOWS_PHONE
                            client.Dispose();
                        #endif
                    }
                });
            }
            catch (Exception e)
            {
                Debug.WriteLine("Failed to submit task to thread pool: " + e.Message);
                this.TriggerSaveGalleryFailed(e);
                #if !WINDOWS_PHONE
                    client.Dispose();
                #endif
            }
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Tests the full scope of methods in the API.
        /// This method creates a gallery, uploads a couple of items, saves them and then retrieves them.
        /// Make sure you change the values of the items in the "items" array to match actually valid files or this
        /// will fail.
        /// </summary>
        private static void TestAll()
        {
            // The call that triggers the program is the near the end of this method
            // (the rest is pretty much setup to react to events)

            // create the API
            MinusApi api = new MinusApi(API_KEY);

            // Prepare the items to be uploaded
            String[] items =
            {
                @"C:\Users\bruno\Desktop\clown.png",
                @"C:\Users\bruno\Desktop\small.png"
            };
            IList<String> uploadedItems = new List<String>(items.Length);

            // create a couple of things we're going to need between requests
            CreateGalleryResult galleryCreated = null;

            // set up the listeners for CREATE
            api.CreateGalleryFailed += delegate(MinusApi sender, Exception e)
            {
                // don't do anything else...
                Console.WriteLine("Failed to create gallery..." + e.Message);
            };
            api.CreateGalleryComplete += delegate(MinusApi sender, CreateGalleryResult result)
            {
                // gallery created, trigger upload of the first file
                galleryCreated = result;
                Console.WriteLine("Gallery created! " + result);
                Thread.Sleep(1000);
                Console.WriteLine("Uploading files...");
                api.UploadItem(result.EditorId, result.Key, items[0]);
            };

            // set up the listeners for UPLOAD
            api.UploadItemFailed += delegate(MinusApi sender, Exception e)
            {
                // don't do anything else...
                Console.WriteLine("Upload failed: " + e.Message);
            };
            api.UploadItemComplete += delegate(MinusApi sender, UploadItemResult result)
            {
                // upload complete, either trigger another upload or save the gallery if all files have been uploaded
                Console.WriteLine("Upload successful: " + result);
                uploadedItems.Add(result.Id);
                if (uploadedItems.Count == items.Length)
                {
                    // if all the elements are uploaded, then save the gallery
                    Console.WriteLine("All uploads complete, saving gallery...");
                    api.SaveGallery("testGallery", galleryCreated.EditorId, galleryCreated.Key, uploadedItems.ToArray());
                }
                else
                {
                    // otherwise just keep uploading
                    Console.WriteLine("Uploading item " + (uploadedItems.Count + 1));
                    api.UploadItem(galleryCreated.EditorId, galleryCreated.Key, items[uploadedItems.Count]);
                }
            };

            // set up the listeners for SAVE
            api.SaveGalleryFailed += delegate(MinusApi sender, Exception e)
            {
                Console.WriteLine("Failed to save gallery... " + e.Message);
            };
            api.SaveGalleryComplete += delegate(MinusApi sender)
            {
                // The extra "m" is appended because minus uses the first character to determine the type of data
                // you're accessing (image, gallery, etc) and route you accordingly.
                Console.WriteLine("Gallery saved! You can now access it at http://min.us/m" + galleryCreated.ReaderId);
                api.SignIn("123test123", "123test123");
            };

            //set up listeners for SignIn
            api.SignInFailed += delegate(MinusApi sender, Exception e)
            {
                Console.WriteLine("Failed to Sign In... " + e.Message);
            };
            api.SignInComplete += delegate(MinusApi sender, SignInResult result)
            {
                Console.WriteLine("Signed In: " + result.Success);
            };

            // this is the call that actually triggers the whole program
            api.CreateGallery();
        }