示例#1
0
        public async Task <IActionResult> CreateAppBundle([FromBody] JObject appBundleSpecs)
        {
            // basic input validation
            string zipFileName = appBundleSpecs["zipFileName"].Value <string>();
            string engineName  = appBundleSpecs["engine"].Value <string>();

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

            // check if ZIP with bundle is here
            string packageZipPath = Path.Combine(LocalBundlesFolder, zipFileName + ".zip");

            if (!System.IO.File.Exists(packageZipPath))
            {
                throw new Exception("Appbundle not found at " + packageZipPath);
            }

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

            AppBundlesApi appBundlesApi = new AppBundlesApi();

            appBundlesApi.Configuration.AccessToken = oauth.access_token;

            // get defined app bundles
            PageString appBundles = await appBundlesApi.AppBundlesGetItemsAsync();

            // check if app bundle is already define
            dynamic newAppVersion;
            string  qualifiedAppBundleId = string.Format("{0}.{1}+{2}", NickName, appBundleName, Alias);

            if (!appBundles.Data.Contains(qualifiedAppBundleId))
            {
                // create an appbundle (version 1)
                AppBundle appBundleSpec = new AppBundle(appBundleName, null, engineName, null, null, string.Format("Description for {0}", appBundleName), null, appBundleName);
                newAppVersion = await appBundlesApi.AppBundlesCreateItemAsync(appBundleSpec);

                if (newAppVersion == null)
                {
                    throw new Exception("Cannot create new app");
                }

                // create alias pointing to v1
                Alias aliasSpec = new Alias(1, null, Alias);
                Alias newAlias  = await appBundlesApi.AppBundlesCreateAliasAsync(appBundleName, aliasSpec);
            }
            else
            {
                // create new version
                AppBundle appBundleSpec = new AppBundle(null, null, engineName, null, null, appBundleName, null, null);
                newAppVersion = await appBundlesApi.AppBundlesCreateItemVersionAsync(appBundleName, appBundleSpec);

                if (newAppVersion == null)
                {
                    throw new Exception("Cannot create new version");
                }

                // update alias pointing to v+1
                Alias aliasSpec = new Alias(newAppVersion.Version, null, null);
                Alias newAlias  = await appBundlesApi.AppBundlesModifyAliasAsync(appBundleName, Alias, aliasSpec);
            }

            // upload the zip with .bundle
            RestClient  uploadClient = new RestClient(newAppVersion.UploadParameters.EndpointURL);
            RestRequest request      = new RestRequest(string.Empty, Method.POST);

            request.AlwaysMultipartFormData = true;
            foreach (KeyValuePair <string, object> x in newAppVersion.UploadParameters.FormData)
            {
                request.AddParameter(x.Key, x.Value);
            }
            request.AddFile("file", packageZipPath);
            request.AddHeader("Cache-Control", "no-cache");
            await uploadClient.ExecuteTaskAsync(request);

            return(Ok(new { AppBundle = qualifiedAppBundleId, Version = newAppVersion.Version }));
        }
示例#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"));
        }