Skip to content

Commit

Permalink
Merge branch 'release/5.0.4'.
Browse files Browse the repository at this point in the history
  • Loading branch information
petrbroz committed Sep 10, 2024
2 parents 162340e + a62e9e7 commit b41f6e9
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 188 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [5.0.4] - 2024-09-10

- Added
- Cleaning up error logs for Axios-related errors
- Modified
- Upgraded to newer version of APS SDK

## [5.0.3] - 2024-04-09

- Modified
Expand Down
6 changes: 2 additions & 4 deletions bin/svf-to-gltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const program = require('commander');
const path = require('path');
const { SdkManagerBuilder } = require('@aps_sdk/autodesk-sdkmanager');
const { ModelDerivativeClient} = require('@aps_sdk/model-derivative');
const { Scopes } = require('@aps_sdk/authentication');
const { SvfReader, GltfWriter, BasicAuthenticationProvider, TwoLeggedAuthenticationProvider } = require('../lib');
Expand Down Expand Up @@ -70,10 +69,9 @@ program
if (guid) {
await convertRemote(urn, guid, folder, options);
} else {
const sdkManager = SdkManagerBuilder.create().build();
const modelDerivativeClient = new ModelDerivativeClient(sdkManager);
const modelDerivativeClient = new ModelDerivativeClient();
const accessToken = await authenticationProvider.getToken([Scopes.ViewablesRead]);
const manifest = await modelDerivativeClient.getManifest(accessToken, urn);
const manifest = await modelDerivativeClient.getManifest(urn, { accessToken });
const derivatives = [];
function traverse(derivative) {
if (derivative.type === 'resource' && derivative.role === 'graphics' && derivative.mime === 'application/autodesk-svf') {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svf-utils",
"version": "5.0.3",
"version": "5.0.4",
"description": "Tools for working with the SVF format used by Autodesk Platform Services.",
"main": "lib/index.js",
"bin": {
Expand Down Expand Up @@ -36,9 +36,9 @@
"typescript": "^4.5.5"
},
"dependencies": {
"@aps_sdk/authentication": "^0.1.0-beta.1",
"@aps_sdk/autodesk-sdkmanager": "^0.0.7-beta.1",
"@aps_sdk/model-derivative": "^0.1.0-beta.1",
"@aps_sdk/authentication": "^1.0.0-beta.1",
"@aps_sdk/autodesk-sdkmanager": "^1.0.0-beta.2",
"@aps_sdk/model-derivative": "^1.0.0-beta.3",
"adm-zip": "^0.5.9",
"axios": "^1.6.8",
"commander": "^3.0.2",
Expand Down
28 changes: 17 additions & 11 deletions samples/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ const { AuthenticationClient, Scopes } = require('@aps_sdk/authentication');
const { ModelDerivativeClient } = require('@aps_sdk/model-derivative');

async function downloadDerivative(urn, derivativeUrn, clientId, clientSecret) {
const sdkManager = SdkManagerBuilder.create().build();
const authenticationClient = new AuthenticationClient(sdkManager);
const modelDerivativeClient = new ModelDerivativeClient(sdkManager);
const credentials = await authenticationClient.getTwoLeggedToken(clientId, clientSecret, [Scopes.ViewablesRead]);
const downloadInfo = await modelDerivativeClient.getDerivativeUrl(credentials.access_token, derivativeUrn, urn);
const response = await axios.get(downloadInfo.url, { responseType: 'arraybuffer', decompress: false });
return response.data;
const authenticationClient = new AuthenticationClient(SdkManagerBuilder.create().build());
const modelDerivativeClient = new ModelDerivativeClient();
try {
const credentials = await authenticationClient.getTwoLeggedToken(clientId, clientSecret, [Scopes.ViewablesRead]);
const downloadInfo = await modelDerivativeClient.getDerivativeUrl(derivativeUrn, urn, { accessToken: credentials.access_token });
const response = await axios.get(downloadInfo.url, { responseType: 'arraybuffer', decompress: false });
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
throw new Error(`Could not download derivative ${derivativeUrn}: ${error.message}`);
} else {
throw error;
}
}
}

async function getSvfDerivatives(urn, clientId, clientSecret) {
const sdkManager = SdkManagerBuilder.create().build();
const authenticationClient = new AuthenticationClient(sdkManager);
const modelDerivativeClient = new ModelDerivativeClient(sdkManager);
const authenticationClient = new AuthenticationClient(SdkManagerBuilder.create().build());
const modelDerivativeClient = new ModelDerivativeClient();
const credentials = await authenticationClient.getTwoLeggedToken(clientId, clientSecret, [Scopes.ViewablesRead]);
const manifest = await modelDerivativeClient.getManifest(credentials.access_token, urn);
const manifest = await modelDerivativeClient.getManifest(urn, { accessToken: credentials.access_token });
const derivatives = [];
function traverse(derivative) {
if (derivative.type === 'resource' && derivative.role === 'graphics' && derivative.mime === 'application/autodesk-svf') {
Expand Down
8 changes: 3 additions & 5 deletions src/common/authentication-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthenticationClient, ResponseType, Scopes } from "@aps_sdk/authentication";
import { SdkManager, SdkManagerBuilder } from "@aps_sdk/autodesk-sdkmanager";
import { AuthenticationClient, Scopes } from "@aps_sdk/authentication";
import { SdkManagerBuilder } from "@aps_sdk/autodesk-sdkmanager";

export interface IAuthenticationProvider {
getToken(scopes: Scopes[]): Promise<string>;
Expand All @@ -15,12 +15,10 @@ export class BasicAuthenticationProvider implements IAuthenticationProvider {
}

export class TwoLeggedAuthenticationProvider implements IAuthenticationProvider {
protected sdkManager: SdkManager;
protected authenticationClient: AuthenticationClient;

constructor(protected clientId: string, protected clientSecret: string) {
this.sdkManager = SdkManagerBuilder.create().build();
this.authenticationClient = new AuthenticationClient(this.sdkManager);
this.authenticationClient = new AuthenticationClient(SdkManagerBuilder.create().build());
}

async getToken(scopes: Scopes[]): Promise<string> {
Expand Down
29 changes: 17 additions & 12 deletions src/f2d/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as path from 'path';
import * as zlib from 'zlib';
import * as fse from 'fs-extra';
import axios from 'axios';
import { ManifestDerivativesChildren, ModelDerivativeClient } from '@aps_sdk/model-derivative';
import { SdkManager, SdkManagerBuilder } from '@aps_sdk/autodesk-sdkmanager';
import { ManifestResources, ModelDerivativeClient } from '@aps_sdk/model-derivative';
import { IAuthenticationProvider } from '../common/authentication-provider';
import { Scopes } from '@aps_sdk/authentication';

Expand All @@ -26,12 +25,10 @@ interface IDownloadContext {
}

export class Downloader {
protected sdkManager: SdkManager;
protected modelDerivativeClient: ModelDerivativeClient;

constructor(protected authenticationProvider: IAuthenticationProvider) {
this.sdkManager = SdkManagerBuilder.create().build();
this.modelDerivativeClient = new ModelDerivativeClient(this.sdkManager);
this.modelDerivativeClient = new ModelDerivativeClient();
}

download(urn: string, options?: IDownloadOptions): IDownloadTask {
Expand All @@ -48,18 +45,26 @@ export class Downloader {
}

private async _downloadDerivative(urn: string, derivativeUrn: string) {
const accessToken = await this.authenticationProvider.getToken([Scopes.ViewablesRead]);
const downloadInfo = await this.modelDerivativeClient.getDerivativeUrl(accessToken, derivativeUrn, urn);
const response = await axios.get(downloadInfo.url as string, { responseType: 'arraybuffer', decompress: false });
return response.data;
try {
const accessToken = await this.authenticationProvider.getToken([Scopes.ViewablesRead]);
const downloadInfo = await this.modelDerivativeClient.getDerivativeUrl(derivativeUrn, urn, { accessToken });
const response = await axios.get(downloadInfo.url as string, { responseType: 'arraybuffer', decompress: false });
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
throw new Error(`Could not download derivative ${derivativeUrn}: ${error.message}`);
} else {
throw error;
}
}
}

private async _download(urn: string, context: IDownloadContext): Promise<void> {
context.log(`Downloading derivative ${urn}`);
const accessToken = await this.authenticationProvider.getToken([Scopes.ViewablesRead]);
const manifest = await this.modelDerivativeClient.getManifest(accessToken, urn);
let derivatives: ManifestDerivativesChildren[] = [];
function collectDerivatives(derivative: ManifestDerivativesChildren) {
const manifest = await this.modelDerivativeClient.getManifest(urn, { accessToken });
let derivatives: ManifestResources[] = [];
function collectDerivatives(derivative: ManifestResources) {
if (derivative.type === 'resource' && derivative.role === 'graphics' && (derivative as any).mime === 'application/autodesk-f2d') {
derivatives.push(derivative);
}
Expand Down
29 changes: 17 additions & 12 deletions src/svf/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import * as path from 'path';
import * as fse from 'fs-extra';
import axios from 'axios';
import { SvfReader } from '..';
import { SdkManager, SdkManagerBuilder } from '@aps_sdk/autodesk-sdkmanager';
import { IAuthenticationProvider } from '../common/authentication-provider';
import { ManifestDerivativesChildren, ModelDerivativeClient } from '@aps_sdk/model-derivative';
import { ManifestResources, ModelDerivativeClient } from '@aps_sdk/model-derivative';
import { Scopes } from '@aps_sdk/authentication';

export interface IDownloadOptions {
Expand All @@ -26,12 +25,10 @@ interface IDownloadContext {
}

export class Downloader {
protected sdkManager: SdkManager;
protected modelDerivativeClient: ModelDerivativeClient;

constructor(protected authenticationProvider: IAuthenticationProvider, host?: string, region?: string) {
this.sdkManager = SdkManagerBuilder.create().build();
this.modelDerivativeClient = new ModelDerivativeClient(this.sdkManager);
this.modelDerivativeClient = new ModelDerivativeClient();
}

download(urn: string, options?: IDownloadOptions): IDownloadTask {
Expand All @@ -48,20 +45,28 @@ export class Downloader {
}

private async _downloadDerivative(urn: string, derivativeUrn: string) {
const accessToken = await this.authenticationProvider.getToken([Scopes.ViewablesRead]);
const downloadInfo = await this.modelDerivativeClient.getDerivativeUrl(accessToken, derivativeUrn, urn);
const response = await axios.get(downloadInfo.url as string, { responseType: 'arraybuffer', decompress: false });
return response.data;
try {
const accessToken = await this.authenticationProvider.getToken([Scopes.ViewablesRead]);
const downloadInfo = await this.modelDerivativeClient.getDerivativeUrl(derivativeUrn, urn, { accessToken });
const response = await axios.get(downloadInfo.url as string, { responseType: 'arraybuffer', decompress: false });
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
throw new Error(`Could not download derivative ${derivativeUrn}: ${error.message}`);
} else {
throw error;
}
}
}

private async _download(urn: string, context: IDownloadContext): Promise<void> {
context.log(`Downloading derivative ${urn}`);
const accessToken = await this.authenticationProvider.getToken([Scopes.ViewablesRead]);
const manifest = await this.modelDerivativeClient.getManifest(accessToken, urn);
const manifest = await this.modelDerivativeClient.getManifest(urn, { accessToken });
const urnDir = path.join(context.outputDir || '.', urn);

const derivatives: ManifestDerivativesChildren[] = [];
function collectDerivatives(derivative: ManifestDerivativesChildren) {
const derivatives: ManifestResources[] = [];
function collectDerivatives(derivative: ManifestResources) {
if (derivative.type === 'resource' && derivative.role === 'graphics' && (derivative as any).mime === 'application/autodesk-svf') {
derivatives.push(derivative);
}
Expand Down
30 changes: 18 additions & 12 deletions src/svf/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as fse from 'fs-extra';
import Zip from 'adm-zip';
import axios from 'axios';
import { isNullOrUndefined } from 'util';
import { SdkManagerBuilder } from '@aps_sdk/autodesk-sdkmanager';
import { ManifestDerivativesChildren, ModelDerivativeClient } from '@aps_sdk/model-derivative';
import { ManifestResources, ModelDerivativeClient } from '@aps_sdk/model-derivative';
import { Scopes } from '@aps_sdk/authentication';
import { PropDbReader } from '../common/propdb-reader';
import { parseFragments } from './fragments';
Expand Down Expand Up @@ -212,12 +211,11 @@ export class Reader {
*/
static async FromDerivativeService(urn: string, guid: string, authenticationProvider: IAuthenticationProvider, host?: string, region?: string): Promise<Reader> {
urn = urn.replace(/=/g, '');
const sdkManager = SdkManagerBuilder.create().build();
const modelDerivativeClient = new ModelDerivativeClient(sdkManager);
const modelDerivativeClient = new ModelDerivativeClient();
const accessToken = await authenticationProvider.getToken([Scopes.ViewablesRead]);
const manifest = await modelDerivativeClient.getManifest(accessToken, urn);
let foundDerivative: ManifestDerivativesChildren | null = null;
function findDerivative(derivative: ManifestDerivativesChildren) {
const manifest = await modelDerivativeClient.getManifest(urn, { accessToken });
let foundDerivative: ManifestResources | null = null;
function findDerivative(derivative: ManifestResources) {
if (derivative.type === 'resource' && derivative.role === 'graphics' && derivative.guid === guid) {
foundDerivative = derivative;
}
Expand All @@ -239,17 +237,25 @@ export class Reader {
}

async function downloadDerivative(urn: string, derivativeUrn: string) {
const accessToken = await authenticationProvider.getToken([Scopes.ViewablesRead]);
const downloadInfo = await modelDerivativeClient.getDerivativeUrl(accessToken, derivativeUrn, urn);
const response = await axios.get(downloadInfo.url as string, { responseType: 'arraybuffer', decompress: false });
return response.data;
try {
const accessToken = await authenticationProvider.getToken([Scopes.ViewablesRead]);
const downloadInfo = await modelDerivativeClient.getDerivativeUrl(derivativeUrn, urn, { accessToken });
const response = await axios.get(downloadInfo.url as string, { responseType: 'arraybuffer', decompress: false });
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
throw new Error(`Could not download derivative ${derivativeUrn}: ${error.message}`);
} else {
throw error;
}
}
}

const svfUrn = (foundDerivative as any).urn;
const svf = await downloadDerivative(urn, encodeURI(svfUrn)) as Buffer;
const baseUri = svfUrn.substr(0, svfUrn.lastIndexOf('/'));
const resolve = async (uri: string) => {
const buffer = await downloadDerivative(urn, encodeURI(path.join(baseUri, uri)));
const buffer = await downloadDerivative(urn, encodeURI(path.normalize(path.join(baseUri, uri))));
return buffer;
};
return new Reader(svf, resolve);
Expand Down
Loading

0 comments on commit b41f6e9

Please sign in to comment.