Example #1
0
        /// <summary>
        /// Creates WorkItem
        /// </summary>
        /// <returns>True if successful</returns>
        public static async Task <dynamic> CreateWorkItem(String bucketkey)
        {
            string  nickName    = ConsumerKey;
            Bearer  bearer      = (await Get2LeggedTokenAsync(new Scope[] { Scope.CodeAll })).ToObject <Bearer>();
            string  downloadUrl = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketkey, inputFileName);
            string  uploadUrl   = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketkey, outputFileName);
            JObject iptFile     = new JObject
            {
                new JProperty("url", downloadUrl),
                new JProperty("headers",
                              new JObject {
                    new JProperty("Authorization", "Bearer " + InternalToken.access_token)
                })
            };

            JObject inputParams = new JObject
            {
                new JProperty("url", "data:application/json,{\"Color\":\"" + paramColor.ToString() + "\", \"RimStyle\":\"" + paramRim.ToString() + "\"}")
            };

            JObject resultIpt = new JObject
            {
                new JProperty("verb", "put"),
                new JProperty("url", uploadUrl),
                new JProperty("headers",
                              new JObject {
                    new JProperty("Authorization", "Bearer " + InternalToken.access_token)
                })
            };
            WorkItem workItemSpec = new WorkItem(
                null, string.Format("{0}.{1}+{2}", nickName, ACTIVITY_NAME, ALIAS),
                new Dictionary <string, JObject>()
            {
                { "InputIPT", iptFile }, { "InputParams", inputParams }, { "ResultIPT", resultIpt }
            }, null);
            WorkItemsApi workItemApi = new WorkItemsApi();

            workItemApi.Configuration.AccessToken = bearer.AccessToken;
            WorkItemStatus newWorkItem = await workItemApi.WorkItemsCreateWorkItemsAsync(null, null, workItemSpec);

            for (int i = 0; i < 1000; i++)
            {
                System.Threading.Thread.Sleep(1000);
                WorkItemStatus workItemStatus = await workItemApi.WorkItemsGetWorkitemsStatusAsync(newWorkItem.Id);

                if (workItemStatus.Status == WorkItemStatus.StatusEnum.Pending || workItemStatus.Status == WorkItemStatus.StatusEnum.Inprogress)
                {
                    continue;
                }
                break;
            }
            uploadURL = uploadUrl;
            ObjectsApi objectsapi = new ObjectsApi();

            objectsapi.Configuration.AccessToken = InternalToken.access_token;
            dynamic obj = await objectsapi.GetObjectDetailsAsync(bucketkey, outputFileName);

            return(obj);
        }
        /// <summary>
        /// Creates WorkItem
        /// </summary>
        /// <returns>True if successful</returns>
        private static async Task <dynamic> CreateWorkItem(String bucketkey)
        {
            string  nickName    = ConsumerKey;
            Bearer  bearer      = (await Get2LeggedTokenAsync(new Scope[] { Scope.CodeAll })).ToObject <Bearer>();
            string  downloadUrl = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketkey, inputFileName);
            string  uploadUrl   = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketkey, inputFileName);
            JObject iptFile     = new JObject
            {
                new JProperty("url", downloadUrl),
                new JProperty("headers",
                              new JObject {
                    new JProperty("Authorization", "Bearer " + InternalToken.access_token)
                })
            };
            JObject resultIpt = new JObject
            {
                new JProperty("verb", "put"),
                new JProperty("url", uploadUrl),
                new JProperty("headers",
                              new JObject {
                    new JProperty("Authorization", "Bearer " + InternalToken.access_token)
                })
            };
            WorkItem workItemSpec = new WorkItem(
                null, string.Format("{0}.{1}+{2}", nickName, ACTIVITY_NAME, ALIAS),
                new Dictionary <string, JObject>()
            {
                { "InputIPT", iptFile }, { "ResultIPT", resultIpt }
            }, null);
            WorkItemsApi workItemApi = new WorkItemsApi();

            workItemApi.Configuration.AccessToken = bearer.AccessToken;
            WorkItemStatus newWorkItem = await workItemApi.WorkItemsCreateWorkItemsAsync(null, null, workItemSpec);

            for (int i = 0; i < 1000; i++)
            {
                System.Threading.Thread.Sleep(1000);
                WorkItemStatus workItemStatus = await workItemApi.WorkItemsGetWorkitemsStatusAsync(newWorkItem.Id);

                if (workItemStatus.Status == WorkItemStatus.StatusEnum.Pending || workItemStatus.Status == WorkItemStatus.StatusEnum.Inprogress)
                {
                    continue;
                }
                break;
            }
            await CheckintoVault(uploadUrl);

            return(new Output(Output.StatusEnum.Sucess, "Activity created"));
        }
Example #3
0
        public async Task <IActionResult> StartWorkitem([FromForm] StartWorkitemInput input)
        {
            // basic input validation
            JObject workItemData       = JObject.Parse(input.data);
            string  widthParam         = workItemData["width"].Value <string>();
            string  heigthParam        = workItemData["height"].Value <string>();
            string  activityName       = string.Format("{0}.{1}", NickName, workItemData["activityName"].Value <string>());
            string  browerConnectionId = workItemData["browerConnectionId"].Value <string>();

            // save the file on the server
            var fileSavePath = Path.Combine(_env.ContentRootPath, input.inputFile.FileName);

            using (var stream = new FileStream(fileSavePath, FileMode.Create)) await input.inputFile.CopyToAsync(stream);

            // OAuth token
            dynamic oauth = await OAuthController.GetInternalAsync();

            // upload file to OSS Bucket
            // 1. ensure bucket existis
            string     bucketKey = NickName.ToLower() + "_designautomation";
            BucketsApi buckets   = new BucketsApi();

            buckets.Configuration.AccessToken = oauth.access_token;
            try
            {
                PostBucketsPayload bucketPayload = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                await buckets.CreateBucketAsync(bucketPayload, "US");
            }
            catch { };                                                                                                                       // in case bucket already exists
            // 2. upload inputFile
            string     inputFileNameOSS = string.Format("{0}_input_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), input.inputFile.FileName); // avoid overriding
            ObjectsApi objects          = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;
            using (StreamReader streamReader = new StreamReader(fileSavePath))
                await objects.UploadObjectAsync(bucketKey, inputFileNameOSS, (int)streamReader.BaseStream.Length, streamReader.BaseStream, "application/octet-stream");
            System.IO.File.Delete(fileSavePath);// delete server copy

            // prepare workitem arguments
            // 1. input file
            JObject inputFileArgument = new JObject
            {
                new JProperty("url", string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, inputFileNameOSS)),
                new JProperty("headers",
                              new JObject {
                    new JProperty("Authorization", "Bearer " + oauth.access_token)
                })
            };
            // 2. input json
            dynamic inputJson = new JObject();

            inputJson.Width  = widthParam;
            inputJson.Height = heigthParam;
            JObject inputJsonArgument = new JObject {
                new JProperty("url", "data:application/json, " + ((JObject)inputJson).ToString(Formatting.None).Replace("\"", "'"))
            };                                                                                                                               // ToDo: need to improve this
            // 3. output file
            string  outputFileNameOSS  = string.Format("{0}_output_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), input.inputFile.FileName); // avoid overriding
            JObject outputFileArgument = new JObject
            {
                new JProperty("verb", "PUT"),
                new JProperty("url", string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, outputFileNameOSS)),
                new JProperty("headers",
                              new JObject {
                    new JProperty("Authorization", "Bearer " + oauth.access_token)
                })
            };

            // prepare & submit workitem
            string   callbackUrl  = string.Format("{0}/api/forge/callback/designautomation?id={1}", OAuthController.GetAppSetting("FORGE_WEBHOOK_CALLBACK_HOST"), browerConnectionId);
            WorkItem workItemSpec = new WorkItem(
                null, activityName,
                new Dictionary <string, JObject>()
            {
                { "inputFile", inputFileArgument },
                { "inputJson", inputJsonArgument },
                { "outputFile", outputFileArgument },
                //{ "onProgress", new JObject { new JProperty("verb", "POST"), new JProperty("url", callbackUrl) }},
                { "onComplete", new JObject {
                      new JProperty("verb", "POST"), new JProperty("url", callbackUrl)
                  } }
            },
                null);
            WorkItemsApi workItemApi = new WorkItemsApi();

            workItemApi.Configuration.AccessToken = oauth.access_token;;
            WorkItemStatus newWorkItem = await workItemApi.WorkItemsCreateWorkItemsAsync(null, null, workItemSpec);

            return(Ok(new { WorkItemId = newWorkItem.Id }));
        }