public async Task <List <ObjectDetails> > GetBucketObjectsAsync(string bucketKey, string beginsWith = null)
        {
            var    objects = new List <ObjectDetails>();
            string startAt = null; // next page pointer

            do
            {
                DynamicJsonResponse response = await WithObjectsApiAsync(async api =>
                {
                    return(await api.GetObjectsAsync(bucketKey, PageSize, beginsWith, startAt));
                });


                foreach (KeyValuePair <string, dynamic> objInfo in new DynamicDictionaryItems((response as dynamic).items))
                {
                    dynamic item = objInfo.Value;

                    var details = new ObjectDetails
                    {
                        BucketKey = item.bucketKey,
                        ObjectId  = item.objectId,
                        ObjectKey = item.objectKey,
                        Sha1      = Encoding.ASCII.GetBytes(item.sha1),
                        Size      = (int?)item.size,
                        Location  = item.location
                    };
                    objects.Add(details);
                }

                startAt = GetNextStartAt(response.Dictionary);
            } while (startAt != null);

            return(objects);
        }
        public async Task <List <ObjectDetails> > GetBucketObjectsAsync(string bucketKey, string beginsWith = null)
        {
            var    objects = new List <ObjectDetails>();
            string startAt = null; // next page pointer

            do
            {
                DynamicJsonResponse response = await WithObjectsApiAsync(async api =>
                {
                    return(await api.GetObjectsAsync(bucketKey, PageSize, beginsWith, startAt));
                });


                foreach (KeyValuePair <string, dynamic> objInfo in new DynamicDictionaryItems((response as dynamic).items))
                {
                    dynamic item = objInfo.Value;

                    var details = new ObjectDetails
                    {
                        BucketKey = item.bucketKey,
                        ObjectId  = item.objectId,
                        ObjectKey = item.objectKey,
                        Sha1      = Encoding.ASCII.GetBytes(item.sha1),
                        Size      = (int?)item.size,
                        Location  = item.location
                    };
                    objects.Add(details);
                }

                startAt = null;

                // check if there is a next page with projects
                if (response.Dictionary.TryGetValue("next", out var nextPage))
                {
                    string nextPageUrl = (string)nextPage;
                    if (!string.IsNullOrEmpty(nextPageUrl))
                    {
                        Uri nextUri = new Uri(nextPageUrl, UriKind.Absolute);
                        Dictionary <string, StringValues> query = QueryHelpers.ParseNullableQuery(nextUri.Query);
                        startAt = query["startAt"];
                    }
                }
            } while (startAt != null);

            return(objects);
        }
Пример #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "workitem")] HttpRequest req,
            [Table("token", "token", "token", Connection = "StorageConnectionString")] Token token,
            [Table("workItems", Connection = "StorageConnectionString")] IAsyncCollector <WorkItemStatusEntity> workItemsTable,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed CreateWorkItem.");

            try
            {
                Autodesk.Forge.Client.Configuration.Default.AccessToken = token.ForgeToken.access_token;
                // Parse the body of the request
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                WorkItemDescription workItemDescription = JsonConvert.DeserializeObject <WorkItemDescription>(requestBody);

                int existingCredits = await _utilities.GetConversionCredits(workItemDescription.userId);

                if (existingCredits > 0)
                {
                    // Create two signed URLs to upload the file to the activity and download the result
                    ObjectsApi        apiInstance           = new ObjectsApi();
                    string            rvtBucketKey          = Environment.GetEnvironmentVariable("rvtStorageKey"); // string | URL-encoded bucket key
                    string            ifcBucketKey          = Environment.GetEnvironmentVariable("ifcStorageKey"); // string | URL-encoded bucket key
                    string            inputObjectName       = workItemDescription.inputObjectName;                 // string | URL-encoded object name
                    string            outputObjectName      = workItemDescription.outputObjectName;
                    string            onCompleteCallbackUrl = Environment.GetEnvironmentVariable("api_uri") + "/api/onworkitemcomplete";
                    PostBucketsSigned postBucketsSigned     = new PostBucketsSigned(60 * 24 * 30);

                    DynamicJsonResponse dynamicJsonResponseDownload = await apiInstance.CreateSignedResourceAsync(rvtBucketKey, inputObjectName, postBucketsSigned, "read");

                    PostObjectSigned    downloadSigned            = dynamicJsonResponseDownload.ToObject <PostObjectSigned>();
                    DynamicJsonResponse dynamicJsonResponseUpload = await apiInstance.CreateSignedResourceAsync(ifcBucketKey, outputObjectName, postBucketsSigned, "readwrite");

                    PostObjectSigned uploadSigned = dynamicJsonResponseUpload.ToObject <PostObjectSigned>();

                    Autodesk.Forge.DesignAutomation.Model.WorkItem workItem = new Autodesk.Forge.DesignAutomation.Model.WorkItem()
                    {
                        ActivityId = workItemDescription.activityId,
                        Arguments  = new Dictionary <string, IArgument>
                        {
                            { "rvtFile", new XrefTreeArgument()
                              {
                                  Url = downloadSigned.SignedUrl
                              } },
                            { "result", new XrefTreeArgument {
                                  Verb = Verb.Put, Url = uploadSigned.SignedUrl
                              } },
                            { "onComplete", new XrefTreeArgument {
                                  Verb = Verb.Post, Url = onCompleteCallbackUrl
                              } }
                        }
                    };

                    Autodesk.Forge.Core.ApiResponse <WorkItemStatus> workItemResponse = await _workItemApi.CreateWorkItemAsync(workItem);

                    // ApiResponse<Page<string>> page = await _engineApi.GetEnginesAsync();
                    WorkItemStatus workItemStatusCreationResponse = workItemResponse.Content;

                    WorkItemStatusEntity WorkItemStatusObject = Mappings.ToWorkItemStatusEntity(
                        workItemStatusCreationResponse,
                        workItemDescription.userId,
                        workItemDescription.fileSize,
                        workItemDescription.version,
                        workItemDescription.fileName,
                        uploadSigned.SignedUrl,
                        downloadSigned.SignedUrl);

                    await workItemsTable.AddAsync(WorkItemStatusObject);

                    WorkItemStatusResponse workItemStatusResponse = new WorkItemStatusResponse()
                    {
                        WorkItemId             = workItemResponse.Content.Id,
                        OutputUrl              = uploadSigned.SignedUrl,
                        WorkItemCreationStatus = WorkItemCreationStatus.Created
                    };

                    return(new OkObjectResult(workItemStatusResponse));
                }
                else
                {
                    WorkItemStatusResponse workItemStatusResponse = new WorkItemStatusResponse()
                    {
                        WorkItemId             = null,
                        OutputUrl              = null,
                        WorkItemCreationStatus = WorkItemCreationStatus.NotEnoughCredit
                    };

                    return(new OkObjectResult(workItemStatusResponse));
                }
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex));
            }
        }