Skip to content

Commit

Permalink
Updated HubsBrowser/.NET tutorial based on latest code (with latest S…
Browse files Browse the repository at this point in the history
…DKs).
  • Loading branch information
petrbroz committed Oct 31, 2024
1 parent 7b11afc commit 0a81c3c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ dotnet new web
Next we will need to install the dependencies. In this case it will be the required APS SDK packages:

```bash
dotnet add package Autodesk.SdkManager
dotnet add package Autodesk.Authentication
dotnet add package Autodesk.DataManagement
dotnet add package Autodesk.Authentication --version 2.0.0-beta4
dotnet add package Autodesk.DataManagement --version 2.0.0-beta4
```

The `*.csproj` file in your project should now look similar to this (possibly with
Expand All @@ -19,14 +18,11 @@ slightly different version numbers, and additional .NET settings):
```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autodesk.Authentication" Version="1.0.0" />
<PackageReference Include="Autodesk.DataManagement" Version="1.0.0" />
<PackageReference Include="Autodesk.SdkManager" Version="1.0.0" />
<PackageReference Include="Autodesk.Authentication" Version="2.0.0-beta4" />
<PackageReference Include="Autodesk.DataManagement" Version="2.0.0-beta4" />
</ItemGroup>
</Project>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
private readonly ILogger<AuthController> _logger;
private readonly APS _aps;

public AuthController(ILogger<AuthController> logger, APS aps)
public AuthController(APS aps)
{
_logger = logger;
_aps = aps;
}

Expand Down Expand Up @@ -73,34 +70,27 @@ public class AuthController : ControllerBase
}

[HttpGet("profile")]
public async Task<dynamic> GetProfile()
public async Task<ActionResult> GetProfile()
{
var tokens = await PrepareTokens(Request, Response, _aps);
if (tokens == null)
{
return Unauthorized();
}
var profile = await _aps.GetUserProfile(tokens);
return new
{
name = profile.Name
};
return Ok(new { name = profile.Name });
}

[HttpGet("token")]
public async Task<dynamic> GetPublicToken()
public async Task<ActionResult> GetPublicToken()
{
var tokens = await PrepareTokens(Request, Response, _aps);
if (tokens == null)
{
return Unauthorized();
}
return new
{
access_token = tokens.PublicToken,
token_type = "Bearer",
expires_in = Math.Floor((tokens.ExpiresAt - DateTime.Now.ToUniversalTime()).TotalSeconds)
};
var expiresIn = Math.Floor((tokens.ExpiresAt - DateTime.Now.ToUniversalTime()).TotalSeconds);
return Ok(new { access_token = tokens.PublicToken, expires_in = expiresIn });
}
}
```
Expand Down
29 changes: 13 additions & 16 deletions docs/03-tutorials/02-hubs-browser/_shared/dotnet/auth/tokens.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ start by adding the following code to the file:

```csharp title="Models/APS.cs"
using System;
using Autodesk.SDKManager;
using Autodesk.Authentication.Model;
using System.Collections.Generic;

Expand All @@ -18,16 +17,14 @@ public class Tokens

public partial class APS
{
private readonly SDKManager _sdkManager;
private readonly string _clientId;
private readonly string _clientSecret;
private readonly string _callbackUri;
private readonly List<Scopes> InternalTokenScopes = new List<Scopes> { Scopes.DataRead, Scopes.ViewablesRead };
private readonly List<Scopes> PublicTokenScopes = new List<Scopes> { Scopes.ViewablesRead };
private readonly List<Scopes> InternalTokenScopes = [Scopes.DataRead, Scopes.ViewablesRead];
private readonly List<Scopes> PublicTokenScopes = [Scopes.ViewablesRead];

public APS(string clientId, string clientSecret, string callbackUri)
{
_sdkManager = SdkManagerBuilder.Create().Build();
_clientId = clientId;
_clientSecret = clientSecret;
_callbackUri = callbackUri;
Expand All @@ -51,41 +48,41 @@ public partial class APS
{
public string GetAuthorizationURL()
{
var authenticationClient = new AuthenticationClient(_sdkManager);
var authenticationClient = new AuthenticationClient();
return authenticationClient.Authorize(_clientId, ResponseType.Code, _callbackUri, InternalTokenScopes);
}

public async Task<Tokens> GenerateTokens(string code)
{
var authenticationClient = new AuthenticationClient(_sdkManager);
var internalAuth = await authenticationClient.GetThreeLeggedTokenAsync(_clientId, _clientSecret, code, _callbackUri);
var publicAuth = await authenticationClient.GetRefreshTokenAsync(_clientId, _clientSecret, internalAuth.RefreshToken, PublicTokenScopes);
var authenticationClient = new AuthenticationClient();
var internalAuth = await authenticationClient.GetThreeLeggedTokenAsync(_clientId, code, _callbackUri, clientSecret: _clientSecret);
var publicAuth = await authenticationClient.RefreshTokenAsync(internalAuth.RefreshToken, _clientId, clientSecret: _clientSecret, scopes: PublicTokenScopes);
return new Tokens
{
PublicToken = publicAuth.AccessToken,
InternalToken = internalAuth.AccessToken,
RefreshToken = publicAuth._RefreshToken,
RefreshToken = publicAuth.RefreshToken,
ExpiresAt = DateTime.Now.ToUniversalTime().AddSeconds((double)internalAuth.ExpiresIn)
};
}

public async Task<Tokens> RefreshTokens(Tokens tokens)
{
var authenticationClient = new AuthenticationClient(_sdkManager);
var internalAuth = await authenticationClient.GetRefreshTokenAsync(_clientId, _clientSecret, tokens.RefreshToken, InternalTokenScopes);
var publicAuth = await authenticationClient.GetRefreshTokenAsync(_clientId, _clientSecret, internalAuth._RefreshToken, PublicTokenScopes);
var authenticationClient = new AuthenticationClient();
var internalAuth = await authenticationClient.RefreshTokenAsync(tokens.RefreshToken, _clientId, clientSecret: _clientSecret, scopes: InternalTokenScopes);
var publicAuth = await authenticationClient.RefreshTokenAsync(internalAuth.RefreshToken, _clientId, clientSecret: _clientSecret, scopes: PublicTokenScopes);
return new Tokens
{
PublicToken = publicAuth.AccessToken,
InternalToken = internalAuth.AccessToken,
RefreshToken = publicAuth._RefreshToken,
RefreshToken = publicAuth.RefreshToken,
ExpiresAt = DateTime.Now.ToUniversalTime().AddSeconds((double)internalAuth.ExpiresIn)
};
}

public async Task<UserInfo> GetUserProfile(Tokens tokens)
{
var authenticationClient = new AuthenticationClient(_sdkManager);
var authenticationClient = new AuthenticationClient();
UserInfo userInfo = await authenticationClient.GetUserInfoAsync(tokens.InternalToken);
return userInfo;
}
Expand Down Expand Up @@ -129,7 +126,7 @@ public class Startup
throw new ApplicationException("Missing required environment variables APS_CLIENT_ID, APS_CLIENT_SECRET, or APS_CALLBACK_URL.");
}
// highlight-start
services.AddSingleton<APS>(new APS(clientID, clientSecret, callbackURL));
services.AddSingleton(new APS(clientID, clientSecret, callbackURL));
// highlight-end
}

Expand Down
50 changes: 32 additions & 18 deletions docs/03-tutorials/02-hubs-browser/_shared/dotnet/data/endpoints.mdx
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
Create a `HubsController.cs` file under the `Controllers` subfolder with the following content:

```csharp title="Controllers/HubsController.cs"
using System.Linq;
using System.Threading.Tasks;
using Autodesk.DataManagement.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

[ApiController]
[Route("api/[controller]")]
public class HubsController : ControllerBase
{
private readonly ILogger<HubsController> _logger;
private readonly APS _aps;

public HubsController(ILogger<HubsController> logger, APS aps)
public HubsController(APS aps)
{
_logger = logger;
_aps = aps;
}

[HttpGet()]
public async Task<ActionResult<string>> ListHubs()
public async Task<ActionResult> ListHubs()
{
var tokens = await AuthController.PrepareTokens(Request, Response, _aps);
if (tokens == null)
{
return Unauthorized();
}
var hubs = await _aps.GetHubs(tokens);
return JsonConvert.SerializeObject(hubs);
return Ok(
from hub in await _aps.GetHubs(tokens)
select new { id = hub.Id, name = hub.Attributes.Name }
);
}

[HttpGet("{hub}/projects")]
public async Task<ActionResult<string>> ListProjects(string hub)
public async Task<ActionResult> ListProjects(string hub)
{
var tokens = await AuthController.PrepareTokens(Request, Response, _aps);
if (tokens == null)
{
return Unauthorized();
}
var projects = await _aps.GetProjects(hub, tokens);
return JsonConvert.SerializeObject(projects);
return Ok(
from project in await _aps.GetProjects(hub, tokens)
select new { id = project.Id, name = project.Attributes.Name }
);
}

[HttpGet("{hub}/projects/{project}/contents")]
public async Task<ActionResult<string>> ListItems(string hub, string project, [FromQuery] string? folder_id)
public async Task<ActionResult> ListItems(string hub, string project, [FromQuery] string folder_id)
{
var tokens = await AuthController.PrepareTokens(Request, Response, _aps);
if (tokens == null)
Expand All @@ -53,26 +55,38 @@ public class HubsController : ControllerBase
}
if (string.IsNullOrEmpty(folder_id))
{
var folders = await _aps.GetTopFolders(hub, project, tokens);
return JsonConvert.SerializeObject(folders);
return Ok(
from folder in await _aps.GetTopFolders(hub, project, tokens)
select new { id = folder.Id, name = folder.Attributes.DisplayName, folder = true }
);
}
else
{
var contents = await _aps.GetFolderContents(project, folder_id, tokens);
return JsonConvert.SerializeObject(contents);
var folders = from entry in contents
where entry is FolderData
select entry as FolderData into folder
select new { id = folder.Id, name = folder.Attributes.DisplayName, folder = true };
var items = from entry in contents
where entry is ItemData
select entry as ItemData into item
select new { id = item.Id, name = item.Attributes.DisplayName, folder = false };
return Ok(folders.Concat(items));
}
}

[HttpGet("{hub}/projects/{project}/contents/{item}/versions")]
public async Task<ActionResult<string>> ListVersions(string hub, string project, string item)
public async Task<ActionResult> ListVersions(string hub, string project, string item)
{
var tokens = await AuthController.PrepareTokens(Request, Response, _aps);
if (tokens == null)
{
return Unauthorized();
}
var versions = await _aps.GetVersions(project, item, tokens);
return JsonConvert.SerializeObject(versions);
return Ok(
from version in await _aps.GetVersions(project, item, tokens)
select new { id = version.Id, name = version.Attributes.CreateTime }
);
}
}
```
Expand Down
21 changes: 10 additions & 11 deletions docs/03-tutorials/02-hubs-browser/_shared/dotnet/data/hubs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,41 @@ Create a `APS.Hubs.cs` under the `Models` subfolder with the following content:
using System.Collections.Generic;
using System.Threading.Tasks;
using Autodesk.DataManagement;
using Autodesk.DataManagement.Http;
using Autodesk.DataManagement.Model;

public partial class APS
{
public async Task<IEnumerable<HubsData>> GetHubs(Tokens tokens)
public async Task<IEnumerable<HubData>> GetHubs(Tokens tokens)
{
var dataManagementClient = new DataManagementClient(_sdkManager);
var dataManagementClient = new DataManagementClient();
var hubs = await dataManagementClient.GetHubsAsync(accessToken: tokens.InternalToken);
return hubs.Data;
}

public async Task<IEnumerable<ProjectsData>> GetProjects(string hubId, Tokens tokens)
public async Task<IEnumerable<ProjectData>> GetProjects(string hubId, Tokens tokens)
{
var dataManagementClient = new DataManagementClient(_sdkManager);
var dataManagementClient = new DataManagementClient();
var projects = await dataManagementClient.GetHubProjectsAsync(hubId, accessToken: tokens.InternalToken);
return projects.Data;
}

public async Task<IEnumerable<TopFoldersData>> GetTopFolders(string hubId, string projectId, Tokens tokens)
public async Task<IEnumerable<TopFolderData>> GetTopFolders(string hubId, string projectId, Tokens tokens)
{
var dataManagementClient = new DataManagementClient(_sdkManager);
var dataManagementClient = new DataManagementClient();
var folders = await dataManagementClient.GetProjectTopFoldersAsync(hubId, projectId, accessToken: tokens.InternalToken);
return folders.Data;
}

public async Task<IEnumerable<FolderContentsData>> GetFolderContents(string projectId, string folderId, Tokens tokens)
public async Task<IEnumerable<IFolderContentsData>> GetFolderContents(string projectId, string folderId, Tokens tokens)
{
var dataManagementClient = new DataManagementClient(_sdkManager);
var dataManagementClient = new DataManagementClient();
var contents = await dataManagementClient.GetFolderContentsAsync(projectId, folderId, accessToken: tokens.InternalToken);
return contents.Data;
}

public async Task<IEnumerable<VersionsData>> GetVersions(string projectId, string itemId, Tokens tokens)
public async Task<IEnumerable<VersionData>> GetVersions(string projectId, string itemId, Tokens tokens)
{
var dataManagementClient = new DataManagementClient(_sdkManager);
var dataManagementClient = new DataManagementClient();
var versions = await dataManagementClient.GetItemVersionsAsync(projectId, itemId, accessToken: tokens.InternalToken);
return versions.Data;
}
Expand Down

0 comments on commit 0a81c3c

Please sign in to comment.