C# Examples

Samples: Overall Flow

Code

C# script to create Background Burn of colorful bag, get results of said Background Burn, select a mask for the Background Burn, delete the Background Burn, then get a list of all Background Burns. The implementation of each method, and responses printed are detailed after the following code snippet.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace BurnerApiDemo
{
        class MainClass
        {
                private const string ApiUrl = "https://api.bonanza.com/api/background_burns";
                private const string ApiKey = "your_key_here";
                private const string UserId = "your_user_id_here";

                public static void Main(string[] args)
                {
                        using (var client = new HttpClient()) {
                                var createResponse = CreateBurn(client).Result;
                                Console.WriteLine(createResponse);

                                dynamic burnInfo = JObject.Parse(createResponse);
                                var burnId = burnInfo ["id"].ToString();
                                        
                                var showResponse = ShowBurn(client, burnId).Result;
                                Console.WriteLine(showResponse);

                                var updateResponse = UpdateBurn(client, burnId, "53487714").Result;
                                Console.WriteLine(updateResponse);

                                var deleteResponse = DeleteBurn(client, burnId).Result;
                                Console.WriteLine(deleteResponse);

                                var indexResponse = ShowBurns(client).Result;
                                Console.WriteLine(indexResponse);
                        }
                }

                private static string BuildApiUrl(string format = "", params object[] args)
                {
                        return ApiUrl + string.Format(format, args);
                }
        }
}

Sample: Creating a Background Burn

Code

Include the URL of an image to create a Background Burn from, along with your API key and User ID:

private static async Task<string> CreateBurn(HttpClient client)
{
        var formContent = new FormUrlEncodedContent(new[]
        {
                new KeyValuePair<string, string>("url", "https://bonanzapublic.s3.amazonaws.com/images/background_burner/colorful_bag.jpg"), 
                new KeyValuePair<string, string>("key", ApiKey),
                new KeyValuePair<string, string>("user_id", UserId),
        });
        
        var response = await client.PostAsync(BuildApiUrl(), formContent);
        return await response.Content.ReadAsStringAsync();
}

Data

createResponse's value (JSON [formatted for legibility] ):

{
   "action":"create",
   "message":"Successfully queued",
   "user_id":"1",
   "burn_user_id":1,
   "access_token":"abc",
   "id":5132,
   "url":"http://burner.bonanza.com/background_burns/5132",
   "position_in_queue":18,
   "complete":false,
   "selected_mask_id":null,
   "final_result_url":null,
   "source_image_url":"http://bonanzaimagestest.s3.amazonaws.com/uploads/burnees/1477068004-12394-0015-1747.jpg",
   "masks":[

   ],
   "review_complete":false
}

Sample: Showing a Background Burn

Code

Once a Background Burn has been created, use the ID from the create response to get the status of the Background Burn:

private static async Task<string> ShowBurn(HttpClient client, string burnId)
{
        var response = await client.GetAsync(BuildApiUrl("/{0}?key={1}&user_id={2}", burnId, ApiKey, UserId));
        return await response.Content.ReadAsStringAsync();
}

Data

showResponse's value (JSON [formatted for legibility] ):

{
   "action":"show",
   "message":"Found existing burn for user 1 with id: 5132",
   "user_id":"1",
   "burn_user_id":1,
   "access_token":"abc",
   "id":5132,
   "url":"http://burner.bonanza.com/background_burns/5132",
   "position_in_queue":18,
   "complete":false,
   "selected_mask_id":null,
   "final_result_url":null,
   "source_image_url":"http://bonanzaimagestest.s3.amazonaws.com/uploads/burnees/1477068004-12394-0015-1747.jpg",
   "masks":[

   ],
   "review_complete":false
}

Sample: Updating a Background Burn

Code

Once a Background Burn is completed and has masks in the show response, use the ID or a mask to select the final mask for the Background Burn:

private static async Task<string> UpdateBurn(HttpClient client, string burnId, string selectedMaskId)
{
        var formContent = new FormUrlEncodedContent(new[]
        {
                new KeyValuePair<string, string>("selected_mask_id", selectedMaskId), 
                new KeyValuePair<string, string>("key", ApiKey),
                new KeyValuePair<string, string>("user_id", UserId),
        });
        var response = await client.PutAsync(BuildApiUrl("/{0}", burnId), formContent);
        return await response.Content.ReadAsStringAsync();
}

updateResponse's value (JSON [formatted for legibility] ):

{
   "action":"update",
   "message":"Saved final mask for burn with id: 16599244",
   "user_id":"1",
   "burn_user_id":1,
   "access_token":"abc",
   "id":16599244,
   "url":"http://burner.bonanza.com/background_burns/16599244",
   "position_in_queue":0,
   "complete":true,
   "selected_mask_id":53487714,
   "final_result_url":"https://images.bonanzastatic.com/uploads/burns/16599244.jpg?1477071758",
   "source_image_url":"https://images.bonanzastatic.com/uploads/burnees/1477071734-3877314-0001-7229.jpg",
   "masks":[
      {
         "id":53487685,
         "url":"http://burner.bonanza.com/background_masks/53487685.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53487685.png?1477072655&composite=true&no_crop=false&transparent=false",
         "variation":"flooded",
         "quality_score":506
      },
      {
         "id":53487702,
         "url":"http://burner.bonanza.com/background_masks/53487702.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53487702.png?1477072655&composite=true&no_crop=false&transparent=false",
         "variation":"adjacent-probable",
         "quality_score":495
      },
      {
         "id":53487703,
         "url":"http://burner.bonanza.com/background_masks/53487703.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53487703.png?1477072655&composite=true&no_crop=false&transparent=false",
         "variation":"raw-probable",
         "quality_score":481
      },
      {
         "id":53487704,
         "url":"http://burner.bonanza.com/background_masks/53487704.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53487704.png?1477072655&composite=true&no_crop=false&transparent=false",
         "variation":"side_burn-probable",
         "quality_score":435
      },
      {
         "id":53487705,
         "url":"http://burner.bonanza.com/background_masks/53487705.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53487705.png?1477072655&composite=true&no_crop=false&transparent=false",
         "variation":"bg-probable",
         "quality_score":434
      }
   ],
   "review_complete":false
}

Sample: Deleting a Background Burn

Code

To remove a Background Burn from your list of Background Burns, you can archive ("delete") it:

private static async Task<string> DeleteBurn(HttpClient client, string burnId)
{
        var response = await client.DeleteAsync(BuildApiUrl("/{0}?key={1}&user_id={2}", burnId, ApiKey, UserId));
        return await response.Content.ReadAsStringAsync();
}

deleteResponse's value (JSON [formatted for legibility] ):

{
   "action":"destroy",
   "message":"Archived burn with id: 16599638",
   "user_id":"1",
   "burn_user_id":1,
   "access_token":"abc",
   "id":16599638,
   "url":"http://burner.bonanza.com/background_burns/16599638",
   "position_in_queue":0,
   "complete":true,
   "selected_mask_id":null,
   "final_result_url":null,
   "source_image_url":"https://images.bonanzastatic.com/uploads/burnees/1477073323-2402909-0002-6928.jpg",
   "masks":[
      {
         "id":53489204,
         "url":"http://burner.bonanza.com/background_masks/53489204.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53489204.png?1477073333&composite=true&no_crop=false&transparent=false",
         "variation":"flooded",
         "quality_score":506
      },
      {
         "id":53489205,
         "url":"http://burner.bonanza.com/background_masks/53489205.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53489205.png?1477073333&composite=true&no_crop=false&transparent=false",
         "variation":"adjacent-probable",
         "quality_score":495
      },
      {
         "id":53489206,
         "url":"http://burner.bonanza.com/background_masks/53489206.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53489206.png?1477073333&composite=true&no_crop=false&transparent=false",
         "variation":"raw-probable",
         "quality_score":481
      },
      {
         "id":53489207,
         "url":"http://burner.bonanza.com/background_masks/53489207.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53489207.png?1477073333&composite=true&no_crop=false&transparent=false",
         "variation":"side_burn-probable",
         "quality_score":435
      },
      {
         "id":53489208,
         "url":"http://burner.bonanza.com/background_masks/53489208.png?",
         "composite_url":"http://burner.bonanza.com/background_masks/53489208.png?1477073333&composite=true&no_crop=false&transparent=false",
         "variation":"bg-probable",
         "quality_score":434
      }
   ],
   "review_complete":false
}

Sample: Listing Background Burns

Code

You can get a list of all your Background Burns:

private static async Task<string> ShowBurns(HttpClient client)
{
        var response = await client.GetAsync(BuildApiUrl("?key={0}&user_id={1}", ApiKey, UserId));
        return await response.Content.ReadAsStringAsync();
}

deleteResponse's value (JSON [formatted for legibility] ):

{
   "action":"index",
   "message":"Success",
   "user_id":"1",
   "burn_user_id":1,
   "access_token":"abc",
   "count":1,
   "background_burns":[
      {
         "id":16599250,
         "url":"http://burner.bonanza.com/background_burns/16599250",
         "position_in_queue":0,
         "complete":true,
         "selected_mask_id":null,
         "final_result_url":"https://images.bonanzastatic.com/uploads/burns/16599250.jpg?1477072654",
         "source_image_url":"https://images.bonanzastatic.com/uploads/burnees/1477071760-2326885-0001-4412.jpg",
         "masks":[
            {
               "id":53487714,
               "url":"http://burner.bonanza.com/background_masks/53487714.png?",
               "composite_url":"http://burner.bonanza.com/background_masks/53487714.png?1477073333&composite=true&no_crop=false&transparent=false",
               "variation":"flooded",
               "quality_score":506
            }
         ],
         "review_complete":false
      }
   ]
}