public async Task <IActionResult> CreateActivity([FromBody] JObject activitySpecs)
        {
            // basic input validation
            string zipFileName = activitySpecs["zipFileName"].Value <string>();
            string engineName  = activitySpecs["engine"].Value <string>();

            // define Activities API
            dynamic oauth = await OAuthController.GetInternalAsync();

            ActivitiesApi activitiesApi = new ActivitiesApi();

            activitiesApi.Configuration.AccessToken = oauth.access_token;

            // standard name for this sample
            string appBundleName = zipFileName + "AppBundle";
            string activityName  = zipFileName + "Activity";

            //
            PageString activities = await activitiesApi.ActivitiesGetItemsAsync();

            string qualifiedActivityId = string.Format("{0}.{1}+{2}", NickName, activityName, Alias);

            if (!activities.Data.Contains(qualifiedActivityId))
            {
                // define the activity
                // ToDo: parametrize for different engines...
                string         commandLine  = string.Format(@"$(engine.path)\\{0} /i $(args[inputFile].path) /al $(appbundles[{1}].path)", Executable(engineName), appBundleName);
                ModelParameter inputFile    = new ModelParameter(false, false, ModelParameter.VerbEnum.Get, "input file", true, "$(inputFile)");
                ModelParameter inputJson    = new ModelParameter(false, false, ModelParameter.VerbEnum.Get, "input json", false, "params.json");
                ModelParameter outputFile   = new ModelParameter(false, false, ModelParameter.VerbEnum.Put, "output file", true, "outputFile.rvt");
                Activity       activitySpec = new Activity(
                    new List <string>()
                {
                    commandLine
                },
                    new Dictionary <string, ModelParameter>()
                {
                    { "inputFile", inputFile },
                    { "inputJson", inputJson },
                    { "outputFile", outputFile }
                },
                    engineName, new List <string>()
                {
                    string.Format("{0}.{1}+{2}", NickName, appBundleName, Alias)
                }, null,
                    string.Format("Description for {0}", activityName), null, activityName);
                Activity newActivity = await activitiesApi.ActivitiesCreateItemAsync(activitySpec);

                // specify the alias for this Activity
                Alias aliasSpec = new Alias(1, null, Alias);
                Alias newAlias  = await activitiesApi.ActivitiesCreateAliasAsync(activityName, aliasSpec);

                return(Ok(new { Activity = qualifiedActivityId }));
            }

            // as this activity points to a AppBundle "dev" alias (which points to the last version of the bundle),
            // there is no need to update it (for this sample), but this may be extended for different contexts
            return(Ok(new { Activity = "Activity already defined" }));
        }
Example #2
0
        /// <summary>
        /// Creates Activity
        /// </summary>
        /// <returns>True if successful</returns>
        public static async Task <dynamic> CreateActivity()
        {
            Bearer bearer   = (await Get2LeggedTokenAsync(new Scope[] { Scope.CodeAll })).ToObject <Bearer>();
            string nickName = ConsumerKey;

            AppBundlesApi appBundlesApi = new AppBundlesApi();

            appBundlesApi.Configuration.AccessToken = bearer.AccessToken;
            PageString appBundles = await appBundlesApi.AppBundlesGetItemsAsync();

            string appBundleID = string.Format("{0}.{1}+{2}", nickName, APPNAME, ALIAS);

            if (!appBundles.Data.Contains(appBundleID))
            {
                if (!System.IO.File.Exists(LocalAppPackageZip))
                {
                    return(new Output(Output.StatusEnum.Error, "Bundle not found at " + LocalAppPackageZip));
                }
                // create new bundle
                AppBundle appBundleSpec = new AppBundle(APPNAME, null, EngineName, null, null, APPNAME, null, APPNAME);
                AppBundle newApp        = await appBundlesApi.AppBundlesCreateItemAsync(appBundleSpec);

                if (newApp == null)
                {
                    return(new Output(Output.StatusEnum.Error, "Cannot create new app"));
                }
                // create alias
                Alias aliasSpec = new Alias(1, null, ALIAS);
                Alias newAlias  = await appBundlesApi.AppBundlesCreateAliasAsync(APPNAME, aliasSpec);

                // upload the zip bundle
                RestClient  uploadClient = new RestClient(newApp.UploadParameters.EndpointURL);
                RestRequest request      = new RestRequest(string.Empty, Method.POST);
                request.AlwaysMultipartFormData = true;
                foreach (KeyValuePair <string, object> x in newApp.UploadParameters.FormData)
                {
                    request.AddParameter(x.Key, x.Value);
                }
                request.AddFile("file", LocalAppPackageZip);
                request.AddHeader("Cache-Control", "no-cache");
                var res = await uploadClient.ExecuteTaskAsync(request);
            }
            ActivitiesApi activitiesApi = new ActivitiesApi();

            activitiesApi.Configuration.AccessToken = bearer.AccessToken;
            PageString activities = await activitiesApi.ActivitiesGetItemsAsync();

            string activityID = string.Format("{0}.{1}+{2}", nickName, ACTIVITY_NAME, ALIAS);

            if (!activities.Data.Contains(activityID))
            {
                // create activity
                string         commandLine  = string.Format(@"$(engine.path)\\inventorcoreconsole.exe /i $(args[InputIPT].path) /al $(appbundles[{0}].path) $(args[InputParams].path)", APPNAME);
                ModelParameter iptFile      = new ModelParameter(false, false, ModelParameter.VerbEnum.Get, "Input Ipt File", true, inputFileName);
                ModelParameter result       = new ModelParameter(false, false, ModelParameter.VerbEnum.Put, "Resulting Ipt File", true, outputFileName);
                ModelParameter inputParams  = new ModelParameter(false, false, ModelParameter.VerbEnum.Get, "Input params", false, "params.json");
                Activity       activitySpec = new Activity(
                    new List <string> {
                    commandLine
                },
                    new Dictionary <string, ModelParameter>()
                {
                    { "InputIPT", iptFile },
                    { "InputParams", inputParams },
                    { "ResultIPT", result },
                },
                    EngineName,
                    new List <string>()
                {
                    string.Format("{0}.{1}+{2}", nickName, APPNAME, ALIAS)
                },
                    null,
                    ACTIVITY_NAME,
                    null,
                    ACTIVITY_NAME
                    );
                Activity newActivity = await activitiesApi.ActivitiesCreateItemAsync(activitySpec);

                Alias aliasSpec = new Alias(1, null, ALIAS);
                Alias newAlias  = await activitiesApi.ActivitiesCreateAliasAsync(ACTIVITY_NAME, aliasSpec);
            }
            return(new Output(Output.StatusEnum.Sucess, "Activity created"));
        }