Skip to content

Commit

Permalink
Merge branch 'develop'.
Browse files Browse the repository at this point in the history
  • Loading branch information
petrbroz committed Oct 31, 2024
2 parents 0ee33f3 + 0a81c3c commit 82bd333
Show file tree
Hide file tree
Showing 30 changed files with 190 additions and 228 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ In this project we also have a specialized component called `<EnvTabs>` that is
specifically to show 3 predefined tabs for the following "environments":

- Node.js + Visual Studio Code
- .NET6 + Visual Studio Code
- .NET6 + Visual Studio 2022
- .NET 8 + Visual Studio Code
- .NET 8 + Visual Studio 2022

To use it, import the Markdown content for individual tabs as separate React components,
and insert them into the `<EnvTabs>` component like so:
Expand Down
Binary file added docs/02-setup/_shared/dotnet-vscode/dotnet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/02-setup/_shared/dotnet-vscode/dotnet.webp
Binary file not shown.
8 changes: 4 additions & 4 deletions docs/02-setup/_shared/dotnet-vscode/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import VSCodeSetup from '../vscode.mdx';

## Runtime

We will also need the .NET 6 _runtime_ to run our code and manage 3rd party dependencies.
You can get an installer for your platform on https://dotnet.microsoft.com/download/dotnet/6.0.
We will also need the .NET _runtime_ to run our code and manage 3rd party dependencies.
You can get an installer for your platform on https://dotnet.microsoft.com/download/dotnet/8.0.

:::info
Our sample applications have been developed and tested with .NET 6.0.
Our sample applications have been developed and tested with .NET 8.0.
:::

To make sure the tool is available, try running the following command in `bash`:
Expand All @@ -27,7 +27,7 @@ dotnet --version

You should see something like this:

![Checking dotnet in terminal](dotnet.webp)
![Checking dotnet in terminal](dotnet.png)

## Editor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ Next we will need to install the dependencies. In this case it will just be a co
packages from the official APS SDK:

```bash
dotnet add package Autodesk.SdkManager --version 1.0.0
dotnet add package Autodesk.Authentication --version 1.0.0
dotnet add package Autodesk.OSS --version 1.0.0
dotnet add package Autodesk.ModelDerivative --version 1.0.0
dotnet add package Autodesk.Authentication --version 2.0.0-beta4
dotnet add package Autodesk.ModelDerivative --version 2.0.0-beta3
dotnet add package Autodesk.OSS --version 2.0.0-beta2
```

The `*.csproj` file in your project should now look similar to this (possibly with
Expand All @@ -21,13 +20,12 @@ 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>
<PackageReference Include="Autodesk.Authentication" Version="1.0.0" />
<PackageReference Include="Autodesk.ModelDerivative" Version="1.0.0" />
<PackageReference Include="Autodesk.OSS" Version="1.0.0" />
<PackageReference Include="Autodesk.SdkManager" Version="1.0.0" />
<PackageReference Include="Autodesk.Authentication" Version="2.0.0-beta4" />
<PackageReference Include="Autodesk.ModelDerivative" Version="2.0.0-beta3" />
<PackageReference Include="Autodesk.OSS" Version="2.0.0-beta2" />
</ItemGroup>
</Project>
```
Expand All @@ -46,7 +44,7 @@ The folder structure in the editor should look similar to this:
![Folder Structure](folder-structure.webp)

:::note
If the `.vscode` folder is not created automatically, you can create it via the Run & Debug sidepanel
If the `.vscode` folder is not created automatically, you can create it via the _Run & Debug_ sidepanel.
:::

To create the `.vscode` folder click on the *Run and Debug tool on the left sidepanel > create a launch.json file > Select .NET5+ & .NET Core*.
To create the `.vscode` folder click on the *Run and Debug tool on the left sidepanel > create a launch.json file > Select .NET & .NET Core*.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
public record AccessToken(string access_token, long expires_in);

private readonly APS _aps;

public AuthController(APS aps)
Expand All @@ -19,13 +17,14 @@ public class AuthController : ControllerBase
}

[HttpGet("token")]
public async Task<AccessToken> GetAccessToken()
public async Task<IActionResult> GetAccessToken()
{
var token = await _aps.GetPublicToken();
return new AccessToken(
token.AccessToken,
(long)Math.Round((token.ExpiresAt - DateTime.UtcNow).TotalSeconds)
);
return Ok(new
{
access_token = token.AccessToken,
expires_in = (long)Math.Round((token.ExpiresAt - DateTime.UtcNow).TotalSeconds)
});
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ all the APS-specific logic that will be used in different areas of our server ap
start by adding the following code to the file:

```csharp title="Models/APS.cs"
using Autodesk.SDKManager;

public partial class APS
{
private readonly SDKManager _sdkManager;
private readonly string _clientId;
private readonly string _clientSecret;
private readonly string _bucket;

public APS(string clientId, string clientSecret, string bucket = null)
{
_sdkManager = SdkManagerBuilder.Create().Build();
_clientId = clientId;
_clientSecret = clientSecret;
_bucket = string.IsNullOrEmpty(bucket) ? string.Format("{0}-basic-app", _clientId.ToLower()) : bucket;
Expand Down Expand Up @@ -45,22 +41,22 @@ public partial class APS

private async Task<Token> GetToken(List<Scopes> scopes)
{
var authenticationClient = new AuthenticationClient(_sdkManager);
var authenticationClient = new AuthenticationClient();
var auth = await authenticationClient.GetTwoLeggedTokenAsync(_clientId, _clientSecret, scopes);
return new Token(auth.AccessToken, DateTime.UtcNow.AddSeconds((double)auth.ExpiresIn));
}

public async Task<Token> GetPublicToken()
{
if (_publicTokenCache == null || _publicTokenCache.ExpiresAt < DateTime.UtcNow)
_publicTokenCache = await GetToken(new List<Scopes> { Scopes.ViewablesRead });
_publicTokenCache = await GetToken([Scopes.ViewablesRead]);
return _publicTokenCache;
}

private async Task<Token> GetInternalToken()
{
if (_internalTokenCache == null || _internalTokenCache.ExpiresAt < DateTime.UtcNow)
_internalTokenCache = await GetToken(new List<Scopes> { Scopes.BucketCreate, Scopes.BucketRead, Scopes.DataRead, Scopes.DataWrite, Scopes.DataCreate });
_internalTokenCache = await GetToken([Scopes.BucketCreate, Scopes.BucketRead, Scopes.DataRead, Scopes.DataWrite, Scopes.DataCreate]);
return _internalTokenCache;
}
}
Expand Down Expand Up @@ -102,7 +98,7 @@ public class Startup
throw new ApplicationException("Missing required environment variables APS_CLIENT_ID or APS_CLIENT_SECRET.");
}
// highlight-start
services.AddSingleton<APS>(new APS(clientID, clientSecret, bucket));
services.AddSingleton(new APS(clientID, clientSecret, bucket));
// highlight-end
}

Expand Down
15 changes: 7 additions & 8 deletions docs/03-tutorials/01-simple-viewer/_shared/dotnet/data/data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public partial class APS
{
private async Task EnsureBucketExists(string bucketKey)
{
const string region = "US";
var auth = await GetInternalToken();
var ossClient = new OssClient(_sdkManager);
var ossClient = new OssClient();
try
{
await ossClient.GetBucketDetailsAsync(bucketKey, accessToken: auth.AccessToken);
Expand All @@ -28,9 +27,9 @@ public partial class APS
var payload = new CreateBucketsPayload
{
BucketKey = bucketKey,
PolicyKey = "persistent"
PolicyKey = PolicyKey.Persistent
};
await ossClient.CreateBucketAsync(region, payload, auth.AccessToken);
await ossClient.CreateBucketAsync(Region.US, payload, auth.AccessToken);
}
else
{
Expand All @@ -39,20 +38,20 @@ public partial class APS
}
}

public async Task<ObjectDetails> UploadModel(string objectName, string pathToFile)
public async Task<ObjectDetails> UploadModel(string objectName, Stream stream)
{
await EnsureBucketExists(_bucket);
var auth = await GetInternalToken();
var ossClient = new OssClient(_sdkManager);
var objectDetails = await ossClient.Upload(_bucket, objectName, pathToFile, auth.AccessToken, new System.Threading.CancellationToken());
var ossClient = new OssClient();
var objectDetails = await ossClient.Upload(_bucket, objectName, stream, accessToken: auth.AccessToken);
return objectDetails;
}

public async Task<IEnumerable<ObjectDetails>> GetObjects()
{
await EnsureBucketExists(_bucket);
var auth = await GetInternalToken();
var ossClient = new OssClient(_sdkManager);
var ossClient = new OssClient();
const int PageSize = 64;
var results = new List<ObjectDetails>();
var response = await ossClient.GetObjectsAsync(_bucket, PageSize, accessToken: auth.AccessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using System.Threading.Tasks;
using Autodesk.ModelDerivative;
using Autodesk.ModelDerivative.Model;

public record TranslationStatus(string Status, string Progress, IEnumerable<string>? Messages);
public record TranslationStatus(string Status, string Progress, IEnumerable<string> Messages);

public partial class APS
{
Expand All @@ -21,7 +21,7 @@ public partial class APS
public async Task<Job> TranslateModel(string objectId, string rootFilename)
{
var auth = await GetInternalToken();
var modelDerivativeClient = new ModelDerivativeClient(_sdkManager);
var modelDerivativeClient = new ModelDerivativeClient();
var payload = new JobPayload
{
Input = new JobPayloadInput
Expand All @@ -30,41 +30,32 @@ public partial class APS
},
Output = new JobPayloadOutput
{
Formats = new List<JobPayloadFormat>
{
new JobSvf2OutputFormat
Formats =
[
new JobPayloadFormatSVF2
{
Views = new List<View>
{
View._2d,
View._3d
}
Views = [View._2d, View._3d]
}
},
Destination = new JobPayloadOutputDestination()
{
Region = Region.US
}
]
}
};
if (!string.IsNullOrEmpty(rootFilename))
{
payload.Input.RootFilename = rootFilename;
payload.Input.CompressedUrn = true;
}
var job = await modelDerivativeClient.StartJobAsync(jobPayload: payload, accessToken: auth.AccessToken);
var job = await modelDerivativeClient.StartJobAsync(jobPayload: payload, region: Region.US, accessToken: auth.AccessToken);
return job;
}

public async Task<TranslationStatus> GetTranslationStatus(string urn)
{
var auth = await GetInternalToken();
var modelDerivativeClient = new ModelDerivativeClient(_sdkManager);
var modelDerivativeClient = new ModelDerivativeClient();
try
{
var manifest = await modelDerivativeClient.GetManifestAsync(urn, accessToken: auth.AccessToken);
var messages = new List<string>();
return new TranslationStatus(manifest.Status, manifest.Progress, messages);
return new TranslationStatus(manifest.Status, manifest.Progress, []);
}
catch (ModelDerivativeApiException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ModelsController : ControllerBase
public class UploadModelForm
{
[FromForm(Name = "model-zip-entrypoint")]
public string? Entrypoint { get; set; }
public string Entrypoint { get; set; }

[FromForm(Name = "model-file")]
public IFormFile File { get; set; }
Expand All @@ -48,12 +48,8 @@ public class ModelsController : ControllerBase
[HttpPost(), DisableRequestSizeLimit]
public async Task<BucketObject> UploadAndTranslateModel([FromForm] UploadModelForm form)
{
var tempFilePath = Path.GetTempFileName();
using (var stream = System.IO.File.Create(tempFilePath))
{
await form.File.CopyToAsync(stream);
}
var obj = await _aps.UploadModel(form.File.FileName, tempFilePath);
using var stream = form.File.OpenReadStream();
var obj = await _aps.UploadModel(form.File.FileName, stream);
var job = await _aps.TranslateModel(obj.ObjectId, form.Entrypoint);
return new BucketObject(obj.ObjectKey, job.Urn);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ Create an `auth.js` file under the `routes` subfolder with the following content

```js title="routes/auth.js"
const express = require('express');
const { getPublicToken } = require('../services/aps.js');
const { getViewerToken } = require('../services/aps.js');

let router = express.Router();

router.get('/api/auth/token', async function (req, res, next) {
try {
res.json(await getPublicToken());
res.json(await getViewerToken());
} catch (err) {
next(err);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,30 @@ all the APS logic that will be used in different areas of our server application
by adding the following code to the file:

```js title="services/aps.js"
const { SdkManagerBuilder } = require('@aps_sdk/autodesk-sdkmanager');
const { AuthenticationClient, Scopes } = require('@aps_sdk/authentication');
const { OssClient, CreateBucketsPayloadPolicyKeyEnum, CreateBucketXAdsRegionEnum } = require('@aps_sdk/oss');
const { ModelDerivativeClient, View, Type } = require('@aps_sdk/model-derivative');
const { OssClient, Region, PolicyKey } = require('@aps_sdk/oss');
const { ModelDerivativeClient, View, OutputType } = require('@aps_sdk/model-derivative');
const { APS_CLIENT_ID, APS_CLIENT_SECRET, APS_BUCKET } = require('../config.js');

const sdk = SdkManagerBuilder.create().build();
const authenticationClient = new AuthenticationClient(sdk);
const ossClient = new OssClient(sdk);
const modelDerivativeClient = new ModelDerivativeClient(sdk);
const authenticationClient = new AuthenticationClient();
const ossClient = new OssClient();
const modelDerivativeClient = new ModelDerivativeClient();

const service = module.exports = {};

service.getInternalToken = async () => {
async function getInternalToken() {
const credentials = await authenticationClient.getTwoLeggedToken(APS_CLIENT_ID, APS_CLIENT_SECRET, [
Scopes.DataRead,
Scopes.DataCreate,
Scopes.DataWrite,
Scopes.BucketCreate,
Scopes.BucketRead
]);
return credentials;
};
return credentials.access_token;
}

service.getPublicToken = async () => {
const credentials = await authenticationClient.getTwoLeggedToken(APS_CLIENT_ID, APS_CLIENT_SECRET, [
Scopes.DataRead
]);
return credentials;
service.getViewerToken = async () => {
return await authenticationClient.getTwoLeggedToken(APS_CLIENT_ID, APS_CLIENT_SECRET, [Scopes.ViewablesRead]);
};
```

Expand Down
Loading

0 comments on commit 82bd333

Please sign in to comment.