-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
154 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const axios = require('axios'); | ||
import * as config from './config'; | ||
|
||
|
||
// instead of using this file to constanly poll the api, it will setup and maintain the webhooks | ||
|
||
const FileShareID = "urn:adsk.wipprod:fs.folder:co.T0n0mYQeS16K0lq1VuuYVQ" | ||
|
||
/** | ||
* Function to manage hook lifecycle | ||
*/ | ||
exports.setup = (credentials: any) => { | ||
axios({ | ||
method: 'GET', | ||
url: 'https://developer.api.autodesk.com/webhooks/v1/hooks', | ||
headers: { | ||
Authorization: `Bearer ${credentials.access_token}` | ||
} | ||
}).then((res: any) => { | ||
return res.data.data; | ||
}).then((data: any) => { | ||
var hookEvents = []; | ||
for (var index in data) { | ||
|
||
const hook = data[index]; | ||
|
||
if (hook.callbackUrl != config.hookCallbackURL) { // hooks with invalid callbacks will be deleted, not added to current list of hooks | ||
deleteHook(hook.event, hook.hookId, credentials); | ||
} else { | ||
hookEvents.push(hook.event); | ||
if (hook.status === 'inactive') { | ||
console.log("reactivating " + hook.event + " hook"); | ||
reactivateHook(hook.event, hook.hookId, credentials); | ||
} | ||
} | ||
} | ||
|
||
// these are the events we want, if not in the list, then register the hook | ||
if (!hookEvents.includes('dm.version.added')) { | ||
createHook('dm.version.added', credentials); | ||
} | ||
|
||
if (!hookEvents.includes('dm.version.modified')) { | ||
createHook('dm.version.modified', credentials); | ||
} | ||
|
||
if (!hookEvents.includes('dm.version.deleted')) { | ||
createHook('dm.version.deleted', credentials); | ||
} | ||
}).catch((err: any) => { | ||
console.log(err); | ||
}) | ||
} | ||
|
||
|
||
const createHook = (event: string, credentials: any) => { | ||
axios({ | ||
method: 'POST', | ||
url: `https://developer.api.autodesk.com/webhooks/v1/systems/data/events/${event}/hooks`, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${credentials.access_token}` | ||
}, | ||
data: { | ||
autoReactivateHook: true, | ||
callbackUrl: config.hookCallbackURL, | ||
scope: { | ||
folder: FileShareID | ||
} | ||
} | ||
}).then((res: any) => { | ||
console.log("sucessfully created hook for " + event); | ||
}).catch((err: any) => { | ||
console.log("error creating hook for " + event + ": " + err.response.status); | ||
}); | ||
} | ||
|
||
const reactivateHook = (event: string, hookID: string, credentials: any) => { | ||
axios({ | ||
method: 'PATCH', | ||
url: `https://developer.api.autodesk.com/webhooks/v1/systems/data/events/${event}/hooks/${hookID}`, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${credentials.access_token}` | ||
}, | ||
data: { | ||
status: 'active', | ||
autoReactivateHook: true, | ||
} | ||
}).then((res: any) => { | ||
console.log("successfully reactivated " + event + " hook"); | ||
}).catch((err: any) => { | ||
console.log("error reactivating hook for " + event + ": " + err.response.status); | ||
}); | ||
} | ||
|
||
const deleteHook = (event:string, hookID: string, credentials: any) => { | ||
axios({ | ||
method: 'DELETE', | ||
url: `https://developer.api.autodesk.com/webhooks/v1/systems/data/events/${event}/hooks/${hookID}`, | ||
headers: { | ||
Authorization: `Bearer ${credentials.access_token}` | ||
} | ||
}).then((res: any) => { | ||
console.log("deleted " + event + " hook with bad callback url"); | ||
}).catch((err: any) => { | ||
console.log("error deleting hook for " + event + ": " + err.response.status); | ||
}); | ||
} |