Difference between revisions of "API Assets Upload"

From unroole CMS wiki
Jump to: navigation, search
(URI)
 
(10 intermediate revisions by one user not shown)
Line 5: Line 5:
  
 
== URI ==
 
== URI ==
<nowiki>http://api.unroole.com/[account-id]/assets</nowiki>
+
{{:API_Base_URI}}accounts/[account-id]/assets
  
 
== HTTP Method ==
 
== HTTP Method ==
Line 20: Line 20:
 
! Accepted Values
 
! Accepted Values
 
! Description
 
! Description
 +
{{:API Required Parameter Common}}
 
|-
 
|-
| token
+
| asset[]
|
+
|
+
|
+
| [[API Persistence Token]] that identifies this user and its permissions.
+
|-
+
| asset[asset][]
+
 
|
 
|
 
|
 
|
 
|  
 
|  
| List of files. Not required if asset[video_id][] is specified.
+
| List of files. Not required if video_id[] is specified.
 
|-
 
|-
| asset[video_id][]
+
| video_id[]
 
|
 
|
 
|
 
|
 
|
 
|
| List of youtube videos. The unique video id or url provided by youtube. ex: (LADHwoN2LMM, http://www.youtube.com/watch?v=LADHwoN2LMM). Not required if asset[asset][] is specified.
+
| List of youtube videos. The unique video id or url provided by youtube. ex: (LADHwoN2LMM, http://www.youtube.com/watch?v=LADHwoN2LMM). Not required if asset[] is specified.
 
|}
 
|}
  
 
== Optional Paramaters ==
 
== Optional Paramaters ==
  
 +
{| class="wikitable"
 +
|-
 +
! Paramater
 +
! Default
 +
! Maximum Length
 +
! Accepted Values
 
{{:API_Assets_Create_Update_Parameter}}
 
{{:API_Assets_Create_Update_Parameter}}
 +
|-
 +
| url_time_limit
 +
| 60
 +
|
 +
|
 +
| The length of time (in seconds) that the asset url will be accessible for if the asset is private. This only applies to the response.
 +
|}
  
 
== Response ==
 
== Response ==
Line 51: Line 59:
 
! Type
 
! Type
 
! Description
 
! Description
 +
{{:API Response Common}}
 
|-
 
|-
| status
+
| success
| string
+
| [[API Status Code]]
+
|-
+
| success_files
+
 
| array of [[API Asset Fields]]
 
| array of [[API Asset Fields]]
 
| List of files that were successfully uploaded
 
| List of files that were successfully uploaded
 
|-
 
|-
| failed_files
+
| failures
 
| array of files names
 
| array of files names
 
| List of file names that failed to uploaded
 
| List of file names that failed to uploaded
|-
 
| error
 
|
 
|
 
 
|}
 
|}
  
== Example Response ==
+
== Example ==
 +
 
 +
Create an HTML page from the code below. Run it in Firefox or Chrome. View the returned JSON/XML in Firebug or Chrome Inspector.
 +
{| class="wikitable collapsible collapsed"
 +
|-
 +
! Sample upload web page
 +
|-
 +
| <blockquote>
 +
<nowiki>
 +
<html>
 +
  <head>
 +
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
 +
      <script>
 +
        function progressHandlingFunction(e){
 +
          if(e.lengthComputable){
 +
            $('#progressbar').show();
 +
            $('#progressbar').attr({value:e.loaded, max:e.total});
 +
          }
 +
        };
 +
       
 +
        function errorPrase(e){
 +
          var json = JSON.parse(e.responseText);
 +
          $.each(json.failures, function(i, item) {
 +
            $('#errors').append("<br/>" + item.name + " - " + item.errors.join(","));
 +
          });
 +
        };
 +
       
 +
        $(document).ready(function(){
 +
         
 +
            $('#submit').click(function(e){
 +
              e.preventDefault();
 +
           
 +
              var url = $('#url').val();
 +
              var token = $('#token').val();
 +
              var account = $('#account').val();
 +
              var videos = $('#youtube').val().split(',');
 +
              if("" == $('#youtube').val()){ videos = []; }
 +
             
 +
              var fullurl = url + 'accounts/' + account + '/assets' + '?token=' + token;
 +
       
 +
              var data = new FormData();
 +
               
 +
              // populate the list of files
 +
              $.each($('#file')[0].files, function(i, file) {
 +
                  data.append("asset[]", file);
 +
              });
 +
             
 +
              // populate the list of youtube assets
 +
              $.each(videos, function(i, youtube_url) {
 +
                  data.append("video_id[]", youtube_url);
 +
              });
 +
             
 +
              data.append("tag_list", $('#tags').val());
 +
              data.append("name", $('#name').val());
 +
              data.append("description", $('#description').val());
 +
              data.append("private", $('#private').val());
 +
              data.append("sharable", $('#sharable').val());
 +
              data.append("printable", $('#printable').val());
 +
              data.append("editable", $('#editable').val());
 +
              data.append("url_time_limit", $('#url_time_limit').val());
 +
       
 +
              var request = $.ajax({
 +
                  url: fullurl,
 +
                  data: data,
 +
                  cache: false,
 +
                  contentType: false,
 +
                  processData: false,
 +
                  crossDomain: true,
 +
                  type: 'POST',
 +
                  //dataType: 'json',
 +
                  success: function(e){
 +
                      $('#status').html("Done");
 +
                  },
 +
                  error: function(e){
 +
                  $('#status').html("Error");
 +
                  errorPrase(e);
 +
                  },
 +
                  headers: {
 +
                    Accept : "application/json, text/javascript"
 +
                    // use below for xml instead
 +
                    //Accept : "text/html,application/xhtml+xml,application/xml"
 +
                  },
 +
                  xhr: function() {
 +
                    myXhr = $.ajaxSettings.xhr();
 +
                    if(myXhr.upload){ // check if upload property exists
 +
                      myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
 +
                    }
 +
                    return myXhr;
 +
                  }
 +
              })
 +
             
 +
              $('#errors').html("");
 +
              $('#status').html("Uploading...");
 +
            });
 +
           
 +
        });
 +
      </script>
 +
  </head>
 +
  <body>
 +
      <table>
 +
        <tbody>
 +
            <tr>
 +
              <td>URL:</td>
 +
              <td><input id="url" type="text" style="width:300px" value="http://api.unroole.com/" /></td>
 +
            </tr>
 +
            <tr>
 +
              <td>Token:</td>
 +
              <td><input id="token" type="text" style="width:300px" /></td>
 +
            </tr>
 +
            <tr>
 +
              <td>Account:</td>
 +
              <td><input id="account" type="text" style="width:100px" /></td>
 +
            </tr>
 +
            <tr>
 +
              <td>File:</td>
 +
              <td><input id="file" type="file" size="60" multiple="multiple" /></td>
 +
            </tr>
 +
            <tr>
 +
              <td>Youtube:</td>
 +
              <td><input id="youtube" type="text" style="width:400px" /> (comma seperated list)</td>
 +
            </tr>
 +
            <tr>
 +
              <td>Name:</td>
 +
              <td><input id="name" type="text" style="width:400px" /></td>
 +
            </tr>
 +
            <tr>
 +
              <td>Description:</td>
 +
              <td><input id="description" type="text" style="width:400px" /></td>
 +
            </tr>
 +
            <tr>
 +
              <td>Tags:</td>
 +
              <td><input id="tags" type="text" style="width:400px" /> (comma seperated list)</td>
 +
            </tr>
 +
            <tr>
 +
              <td>Publicly accessible:</td>
 +
              <td>
 +
                  <select id="private">
 +
                    <option selected="selected" value="0">Yes</option>
 +
                    <option value="1">No</option>
 +
                  </select>
 +
              </td>
 +
            </tr>
 +
            <tr>
 +
              <td>Sharable:</td>
 +
              <td>
 +
                  <select id="sharable">
 +
                    <option value="true">Yes</option>
 +
                    <option selected="selected" value="false">No</option>
 +
                  </select>
 +
              </td>
 +
            </tr>
 +
            <tr>
 +
              <td>Printable:</td>
 +
              <td>
 +
                  <select id="printable">
 +
                    <option value="true">Yes</option>
 +
                    <option selected="selected" value="false">No</option>
 +
                  </select>
 +
              </td>
 +
            </tr>
 +
            <tr>
 +
              <td>Editable:</td>
 +
              <td>
 +
                  <select id="editable">
 +
                    <option value="true">Yes</option>
 +
                    <option selected="selected" value="false">No</option>
 +
                  </select>
 +
              </td>
 +
            </tr>
 +
            <tr>
 +
              <td>Url time limit:</td>
 +
              <td><input id="url_time_limit" type="text" style="width:100px" /></td>
 +
            </tr>
 +
        </tbody>
 +
      </table>
 +
      <br />
 +
      <input id="submit" type="button" value="Submit" />
 +
      <br /><br />
 +
      <progress id="progressbar" style="display:none"></progress> <span id="status"></span>
 +
      <br />
 +
      <span id="errors"></span>
 +
  </body>
 +
</html>
 +
</nowiki>
 +
</blockquote>
 +
|-
 +
|}

Latest revision as of 10:14, 25 June 2012

Summary

Allows for uploading a new asset.

URI

https://api.unroole.com/accounts/[account-id]/assets

HTTP Method

POST

Required Paramaters

Paramater Default Maximum Length Accepted Values Description
token API Persistence Token that identifies this user and its permissions.
asset[] List of files. Not required if video_id[] is specified.
video_id[] List of youtube videos. The unique video id or url provided by youtube. ex: (LADHwoN2LMM, http://www.youtube.com/watch?v=LADHwoN2LMM). Not required if asset[] is specified.

Optional Paramaters

Paramater Default Maximum Length Accepted Values
name Filename with spaces replaced with underscores. 255 characters string The name of the asset.
description MySQL TEXT UTF-8 string The description of the asset.
tag_list ∞, but each tag is 255 characters string List of comma separated tags (e.g. Ruby, HTML5, css3).
private false boolean Public accessibility of an asset determines if its links can be shared or bookmarked.
sharable false boolean Asset can be shared through the Mobile Briefcase
editable false boolean Asset can be opened in other applications through the Mobile Briefcase
printable false boolean Asset can be printed through the Mobile Briefcase
url_time_limit 60 The length of time (in seconds) that the asset url will be accessible for if the asset is private. This only applies to the response.

Response

Field Type Description
http_status integer API Status Code
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
error Rails default
success array of API Asset Fields List of files that were successfully uploaded
failures array of files names List of file names that failed to uploaded

Example

Create an HTML page from the code below. Run it in Firefox or Chrome. View the returned JSON/XML in Firebug or Chrome Inspector.