public static string Post(this RestApiClient networkClient, string url, object content)
        {
            var httpRequest = networkClient.CreatePostRequest(url, content);

            var httpClient      = networkClient.HttpClient;
            var response        = httpClient.SendAsync(httpRequest).Result;
            var responseContent = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {
                return(responseContent);
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                if (networkClient.ShallRefreshToken())
                {
                    if (networkClient.InitiateTokenRefresh())
                    {
                        return(Post(networkClient, url, content));
                    }
                }
            }

            throw new Exception(extractMessageFromErrorResponse(responseContent));
        }
Exemplo n.º 2
0
        public static string Get(this RestApiClient networkClient, string url)
        {
            var httpRequest = networkClient.CreateRequest(HttpMethod.Get, url);

            var httpClient      = networkClient.HttpClient;
            var response        = httpClient.SendAsync(httpRequest).Result;
            var responseContent = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {
                CohesityLog.Instance.WriteCohesityLog(responseContent);
                return(responseContent);
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                if (networkClient.ShallRefreshToken())
                {
                    if (networkClient.InitiateTokenRefresh())
                    {
                        return(Get(networkClient, url));
                    }
                }
            }

            CohesityLog.Instance.WriteCohesityLog(extractMessageFromErrorResponse(responseContent));
            throw new Exception(extractMessageFromErrorResponse(responseContent));
        }
        public static string Post(this RestApiClient networkClient, string url, object content)
        {
            var httpRequest = networkClient.CreatePostRequest(url, content);

            var httpClient      = networkClient.HttpClient;
            var response        = httpClient.SendAsync(httpRequest).Result;
            var responseContent = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {
                return(responseContent);
            }

            throw new Exception(extractMessageFromErrorResponse(responseContent));
        }
        public static ViewBox GetStorageDomainByName(RestApiClient client, string name)
        {
            var preparedUrl = $"/public/viewBoxes";
            var domains     = client.Get <IEnumerable <ViewBox> >(preparedUrl);

            if (domains == null || !domains.Any())
            {
                throw new Exception("Storage domain with matching name not found.");
            }
            var domain = domains.FirstOrDefault(i => i.Name.Equals(name));

            if (domain == null)
            {
                throw new Exception("Storage domain with matching name not found.");
            }

            return(domain);
        }
        public static ProtectionPolicy GetPolicyByName(RestApiClient client, string name)
        {
            var preparedUrl = $"/public/protectionPolicies";
            var policies    = client.Get <IEnumerable <ProtectionPolicy> >(preparedUrl);

            if (policies == null || !policies.Any())
            {
                throw new Exception("Policy with matching name not found.");
            }

            var policy = policies.FirstOrDefault(i => i.Name.Equals(name));

            if (policy == null)
            {
                throw new Exception("Policy with matching name not found.");
            }

            return(policy);
        }
        public static ProtectionJob GetProtectionJobByName(RestApiClient client, string name)
        {
            var qb = new QuerystringBuilder();

            qb.Add("includeLastRunAndStats", true);

            var preparedUrl = $"/public/protectionJobs{qb.Build()}";
            var jobs        = client.Get <IEnumerable <ProtectionJob> >(preparedUrl);

            if (jobs == null || !jobs.Any())
            {
                throw new Exception("Protection job with matching name not found.");
            }

            var job = jobs.FirstOrDefault(i => i.Name.Equals(name));

            if (job == null)
            {
                throw new Exception("Protection job with matching name not found.");
            }

            return(job);
        }
        public static View GetViewByName(RestApiClient client, string name)
        {
            var qb = new QuerystringBuilder();

            qb.Add("includeInactive", true);

            var preparedUrl = $"/public/views{qb.Build()}";
            var result      = client.Get <GetViewsResult>(preparedUrl);

            if (result == null)
            {
                throw new Exception("View with matching name not found.");
            }

            var view = result.Views.FirstOrDefault(i => i.Name.Equals(name));

            if (view == null)
            {
                throw new Exception("View with matching name not found.");
            }

            return(view);
        }
        public static IEnumerable <ProtectionRunInstance> GetProtectionJobRunsByJobId(RestApiClient client, long jobId)
        {
            var qb = new QuerystringBuilder();

            qb.Add("jobId", jobId);

            var preparedUrl = $"/public/protectionRuns{qb.Build()}";
            var jobRuns     = client.Get <IEnumerable <ProtectionRunInstance> >(preparedUrl);

            if (jobRuns == null || !jobRuns.Any())
            {
                throw new Exception("Protection job runs with matching job id not found.");
            }

            return(jobRuns);
        }
        public static T Post <T>(this RestApiClient networkClient, string url, object content)
        {
            var responseContent = Post(networkClient, url, content);

            return(DeserializeObject <T>(responseContent));
        }
        public static T Get <T>(this RestApiClient networkClient, string url)
        {
            var responseContent = Get(networkClient, url);

            return(DeserializeObject <T>(responseContent));
        }