Author Topic: View and Data API MVC App  (Read 2278 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
View and Data API MVC App
« on: May 13, 2015, 12:57:58 AM »

Messing around with View and Data API


Instead of using this class
I thought it was much easier to deal with
Code - C#: [Select]
  1. public class AuViewerClient
  2.     {
  3.         HttpClient Client { get; set; }
  4.         private static string acessToken = string.Empty;
  5.         private static DateTime expireTime = DateTime.MinValue;
  6.         private readonly FormUrlEncodedContent _form;
  7.  
  8.         public AuViewerClient(string key, string password, Uri baseAddressUri)
  9.         {
  10.             Client = new HttpClient()
  11.             {
  12.                 BaseAddress = baseAddressUri
  13.             };
  14.             _form = new FormUrlEncodedContent(
  15. new Dictionary<string, string> { { "client_id", key },
  16.                 { "client_secret", password }, { "grant_type", "client_credentials" } });
  17.         }
  18.  
  19.         public async Task<string> GetAccessTokenAsync()
  20.         {
  21.             if (expireTime > DateTime.Now)
  22.             {
  23.                 return acessToken;
  24.             }
  25.             var response = await GetToken();
  26.             if (!response.IsSuccessStatusCode)
  27.             {
  28.                 return String.Empty;
  29.             }
  30.             expireTime = DateTime.Now.AddMinutes(18);
  31.             var tokenResponse = await response.Content.ReadAsAsync<AccessTokenResponse>();
  32.             acessToken = tokenResponse.AccessToken;
  33.             return acessToken;
  34.         }
  35.  
  36.         private async Task<HttpResponseMessage> GetToken()
  37.         {
  38.             return await Client.PostAsync("authentication/v1/authenticate", _form);
  39.         }
  40.  
  41.         public async Task<HttpResponseMessage> Upload(HttpPostedFileBase file, string bucketKey)
  42.         {
  43.             var token = await GetAccessTokenAsync();
  44.             Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  45.             var objectKey = Uri.EscapeDataString(file.FileName);
  46.  
  47.             var requestMessage = new HttpRequestMessage
  48.             {
  49.                 Method = HttpMethod.Put,
  50.                 RequestUri = new Uri(Client.BaseAddress, "oss/v1/buckets/" + bucketKey + "/objects/" + objectKey),
  51.                 Content = new StreamContent(file.InputStream)
  52.             };
  53.  
  54.             requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);
  55.             requestMessage.Content.Headers.ContentLength = file.ContentLength;
  56.             Client.Timeout = TimeSpan.FromSeconds(3600);
  57.             return await Client.SendAsync(requestMessage);
  58.         }
  59.  
  60.         public async Task<HttpResponseMessage> Register(string fileId)
  61.         {
  62.             var token = await GetAccessTokenAsync();
  63.             Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  64.             var requestMessage = new HttpRequestMessage()
  65.             {
  66.                 Method = HttpMethod.Post,
  67.                 RequestUri = new Uri(Client.BaseAddress, "viewingservice/v1/register")
  68.             };
  69.             requestMessage.Headers.Add("x-ads-force", "true");
  70.             requestMessage.Content = new StringContent("{\"urn\":\"" + fileId + "\"}", Encoding.UTF8, "application/json");
  71.             requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  72.  
  73.             return await Client.SendAsync(requestMessage);
  74.         }
  75.  
  76.         public async Task<HttpResponseMessage> GetViewable(string urn)
  77.         {
  78.             var token = await GetAccessTokenAsync();
  79.             Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  80.             return await Client.GetAsync("viewingservice/v1/" + urn + "/status");
  81.         }
  82.     }
  83.  


Here is a drawing and a revit project uploaded, registered, and translated with API.


DRAWING



REVIT PROJECT



Attached is website solution.


A couple notes about website
  • The selected model in combobox will be shown in viewer
  • Instead of database it uses a text file serialized to Json to keep up with models uploaded so anyone playing with it would not have to mess with creating, attaching, etc... a database
  • To upload a view you have to browse to model then click view upload button

Using built-in control reason why 2 buttons and would normally change to a AJAX request.
Which in turn when uploading model there is no feedback for status just will refresh to viewer index page or an error page will show.


When you upload a model successfully it will be added to combobox and for it show properly in viewer it must return success and complete



It uses a user account I created with gmail account so do not upload any models that you would not want anyone else to see.




In almost all examples you see
/// NEVER DO THIS IN REAL APP
/// NEVER STORE PASSWORD INFO IN PUBLIC FILES


So instead of using clause I store info in a file private.config with Build Action set to NONE
If you use the this method with git make sure to add private.config to your list of ignore files.
Code: [Select]
<appSettings>
  <add key="client_id" value="DMxD1g5NVG5A4HALFe6925ADCUEMphgw" />
  <add key="client_secret" value="GnqG5LgYmZLmPFUM" />
  <add key="bucket_key" value="testertransientbucket" />
  <add key="redirect_url" value="https://developer.api.autodesk.com" />
</appSettings>



Inside the web.config it just reference the file which is not published or copied with version control which you need to set.
Code: [Select]
  <appSettings file="Private.config">
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>


Once published for example Azure will let you add the keys and values by logging in and adding them through portal allowing you to test and never expose info publicly.


Project should work with IE11, firefox, and chrome and visual studio 2013.
Do not think any setup or settings required except if you use IE11 will need to set these setting mentioned in ADN blog


Let me know if anyone has problems getting working and needs help.


Having problem attaching project so here is dropbox temporarily until get it attached.
Download Solution
« Last Edit: May 13, 2015, 02:23:19 AM by Jeff H »

flaxoice

  • Mosquito
  • Posts: 1
Re: View and Data API MVC App
« Reply #1 on: November 10, 2016, 09:58:54 AM »
The dropbox link is broken please can you share.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: View and Data API MVC App
« Reply #2 on: November 10, 2016, 11:54:13 AM »