Example #1
0
        public static string GetContainerId(string hub_id, string project_id)
        {
            credentials = Credentials.FromFile(Path.Combine(
                                                   Directory.GetCurrentDirectory(), "token.json"));
            string       container_id = String.Empty;
            TwoLeggedApi oauth        = new TwoLeggedApi();
            dynamic      bearer       = oauth.Authenticate(FORGE_CLIENT_ID,
                                                           FORGE_CLIENT_SECRET, "client_credentials",
                                                           new Scope[] { Scope.DataRead });


            RestClient  client  = new RestClient(BASE_URL);
            RestRequest request = new RestRequest("project/v1/hubs/{hub_id}/projects", RestSharp.Method.GET);

            request.AddHeader("Authorization", "Bearer " + credentials.TokenInternal);
            request.AddParameter("hub_id", hub_id, ParameterType.UrlSegment);
            request.AddParameter("page[limit]", 99, ParameterType.QueryString);


            IRestResponse response = client.Execute(request);
            dynamic       projects = JObject.Parse(response.Content);

            foreach (var project in projects.data)
            {
                if (project.id.ToString() == project_id)
                {
                    container_id = project.relationships.issues.data.id.ToString();
                }
            }

            return(container_id);
        }
        /**
         * Initialize the 2-legged OAuth 2.0 client, and optionally set specific scopes.
         */
        private static void initializeOAuth()
        {
            // You must provide at least one valid scope
            Scope[] scopes = new Scope[] { Scope.DataRead, Scope.DataWrite, Scope.BucketCreate, Scope.BucketRead };

            oauth2TwoLegged      = new TwoLeggedApi();
            twoLeggedCredentials = oauth2TwoLegged.Authenticate(FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, oAuthConstants.CLIENT_CREDENTIALS, scopes);
            bucketsApi.Configuration.AccessToken     = twoLeggedCredentials.access_token;
            objectsApi.Configuration.AccessToken     = twoLeggedCredentials.access_token;
            derivativesApi.Configuration.AccessToken = twoLeggedCredentials.access_token;
        }
Example #3
0
        public static List <Project> GetProjects(string hub_id)
        {
            credentials = Credentials.FromFile(Path.Combine(
                                                   Directory.GetCurrentDirectory(), "token.json"));
            List <Project> projectsList = new List <Project>();
            TwoLeggedApi   oauth        = new TwoLeggedApi();
            dynamic        bearer       = oauth.Authenticate(FORGE_CLIENT_ID,
                                                             FORGE_CLIENT_SECRET, "client_credentials",
                                                             new Scope[] { Scope.DataRead });


            RestClient  client  = new RestClient(BASE_URL);
            RestRequest request = new RestRequest("project/v1/hubs/{hub_id}/projects", RestSharp.Method.GET);

            request.AddHeader("Authorization", "Bearer " + credentials.TokenInternal);
            request.AddParameter("hub_id", hub_id, ParameterType.UrlSegment);
            request.AddParameter("page[limit]", 99, ParameterType.QueryString);

            IRestResponse response = client.Execute(request);
            dynamic       projects = JObject.Parse(response.Content);

            // List<string> projectsForIssueTransfer = JsonConvert.DeserializeObject<List<string>>(System.IO.File.ReadAllText(Path.Combine(
            // Directory.GetCurrentDirectory(), "projects.json"), Encoding.GetEncoding(1251)));

            foreach (var project in projects.data)
            {
                List <ProjInfo> projectsJson = JsonConvert.DeserializeObject <List <ProjInfo> >(System.IO.File.ReadAllText(@"projects.json"));
                foreach (var proj in projectsJson)
                {
                    if (project.attributes.name.ToString() == proj.Project)
                    {
                        string projectId        = project.id.ToString();
                        string issueContainerId = project.relationships.issues.data.id.ToString();

                        Project pr = new Project()
                        {
                            ProjectBIM360ID = project.id.ToString(),
                            Title           = project.attributes.name.ToString()
                        };
                        projectsList.Add(pr);
                    }
                }
            }

            return(projectsList);
        }
Example #4
0
        /// <summary>
        /// Get access token sync.
        /// </summary>
        /// <returns>Access token</returns>
        public Token GetToken()
        {
            var response = api.Authenticate(clientID, clientSecret,
                                            grantType: "client_credentials", scopes);

            if (string.IsNullOrEmpty(response.ToString()))
            {
                return(null);
            }
            Dictionary <string, dynamic> json = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(response.ToString());
            Token token = new Token(json["token_type"],
                                    (int)json["expires_in"],
                                    json["access_token"]);


            return(token);
        }
Example #5
0
        public static string GetHubs()
        {
            credentials = Credentials.FromFile(Path.Combine(
                                                   Directory.GetCurrentDirectory(), "token.json"));
            string       hub_id = String.Empty;
            TwoLeggedApi oauth  = new TwoLeggedApi();
            dynamic      bearer = oauth.Authenticate(FORGE_CLIENT_ID,
                                                     FORGE_CLIENT_SECRET, "client_credentials",
                                                     new Scope[] { Scope.DataRead });
            RestClient  client  = new RestClient(BASE_URL);
            RestRequest request = new RestRequest("/project/v1/hubs", RestSharp.Method.GET);

            request.AddHeader("Authorization", "Bearer " + credentials.TokenInternal);
            IRestResponse response = client.Execute(request);
            dynamic       hubs     = JObject.Parse(response.Content);
            List <string> hubsIds  = new List <string>();

            foreach (dynamic hub in hubs.data)
            {
                //if (hub.attributes.name.ToString() == @"АО «МР Групп»")
                hub_id = hub.id.ToString();
            }
            return(hub_id);
        }
Example #6
0
        public Token gettoken()
        {
            Token returnobj = new Token();

            TwoLeggedApi oauth = new TwoLeggedApi();

            string grantType = "client_credentials";

            string FORGE_CLIENT_ID     = "g0kwQcyAs8TpiFGe5OPq6dbIh20BaKhw";
            string FORGE_CLIENT_SECRET = "wsK6CJQcs4pEgopw";

            PublicToken = oauth.Authenticate(
                FORGE_CLIENT_ID,
                FORGE_CLIENT_SECRET,
                grantType,
                new Scope[] { Scope.DataRead, Scope.BucketRead, Scope.AccountRead, Scope.BucketUpdate, Scope.DataWrite });

            JObject jobj = PublicToken.ToJson();

            returnobj.access_token = jobj["access_token"].ToString();
            returnobj.expires_in   = jobj["expires_in"].ToString();

            return(returnobj);
        }
Example #7
0
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file == null || file.ContentLength == 0)
            {
                return(RedirectToAction("Index"));
            }

            string fileName     = Path.GetFileName(file.FileName);
            string fileSavePath = Path.Combine(Server.MapPath("~/App_Data/"), fileName);

            file.SaveAs(fileSavePath);

            // get a write enabled token
            TwoLeggedApi oauthApi = new TwoLeggedApi();
            dynamic      bearer   = oauthApi.Authenticate(
                WebConfigurationManager.AppSettings["FORGE_CLIENT_ID"],
                WebConfigurationManager.AppSettings["FORGE_CLIENT_SECRET"],
                "client_credentials",
                new Scope[] { Scope.BucketCreate, Scope.DataCreate, Scope.DataWrite, Scope.DataRead });

            // create a randomg bucket name (fixed prefix + randomg guid)
            string bucketKey = "forgeapp" + Guid.NewGuid().ToString("N").ToLower();

            // create the Forge bucket
            PostBucketsPayload postBucket = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient /* erase after 24h*/);
            BucketsApi         bucketsApi = new BucketsApi();

            bucketsApi.Configuration.AccessToken = bearer.access_token;
            dynamic newBucket = bucketsApi.CreateBucket(postBucket);

            // upload file (a.k.a. Objects)
            ObjectsApi objectsApi = new ObjectsApi();

            oauthApi.Configuration.AccessToken = bearer.access_token;
            dynamic newObject;

            using (StreamReader fileStream = new StreamReader(fileSavePath))
            {
                newObject = objectsApi.UploadObject(bucketKey, fileName,
                                                    (int)fileStream.BaseStream.Length, fileStream.BaseStream,
                                                    "application/octet-stream");
            }

            // translate file
            string objectIdBase64 = ToBase64(newObject.objectId);
            List <JobPayloadItem> postTranslationOutput = new List <JobPayloadItem>()
            {
                new JobPayloadItem(
                    JobPayloadItem.TypeEnum.Svf /* Viewer*/,
                    new List <JobPayloadItem.ViewsEnum>()
                {
                    JobPayloadItem.ViewsEnum._3d,
                    JobPayloadItem.ViewsEnum._2d
                })
            };

            JobPayload postTranslation = new JobPayload(
                new JobPayloadInput(objectIdBase64),
                new JobPayloadOutput(postTranslationOutput));
            DerivativesApi derivativeApi = new DerivativesApi();

            derivativeApi.Configuration.AccessToken = bearer.access_token;
            dynamic translation = derivativeApi.Translate(postTranslation);

            // check if is ready
            int progress = 0;

            do
            {
                System.Threading.Thread.Sleep(1000); // wait 1 second
                try
                {
                    dynamic manifest = derivativeApi.GetManifest(objectIdBase64);
                    progress = (string.IsNullOrWhiteSpace(Regex.Match(manifest.progress, @"\d+").Value) ? 100 : Int32.Parse(Regex.Match(manifest.progress, @"\d+").Value));
                }
                catch (Exception ex)
                {
                }
            } while (progress < 100);

            // clean up
            System.IO.File.Delete(fileSavePath);
            //Directory.Delete(fileSavePath, true);

            return(RedirectToAction("DisplayModel", "Home", new { characterName = objectIdBase64 }));
        }