Example #1
0
        public static void MakeHttpRequest(this UiPathWebApi api, HttpMethod method, string url, object dto, out string responseContent, out HttpHeaders headers)
        {
            responseContent = null;
            headers         = null;
            using (var httpRequest = new HttpRequestMessage())
            {
                httpRequest.Method     = new HttpMethod(method.Method);
                httpRequest.RequestUri = new Uri(api.BaseUri, url);

                string requestContent = null;
                if (dto != null)
                {
                    requestContent      = SafeJsonConvert.SerializeObject(dto, api.SerializationSettings);
                    httpRequest.Content = new StringContent(requestContent, System.Text.Encoding.UTF8);
                    httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
                }

                using (var httpResponse = api.HttpClient.SendAsync(httpRequest).Result)
                {
                    if (httpResponse.Content != null)
                    {
                        responseContent = httpResponse.Content.ReadAsStringAsync().Result;
                    }
                    if (!httpResponse.IsSuccessStatusCode)
                    {
                        var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", httpResponse.StatusCode));
                        ex.Request  = new HttpRequestMessageWrapper(httpRequest, requestContent);
                        ex.Response = new HttpResponseMessageWrapper(httpResponse, responseContent);
                        throw ex;
                    }
                    headers = httpResponse.Headers;
                }
            }
        }
        internal static UiPathWebApi MakeApi(AuthToken authToken)
        {
            ServiceClientCredentials creds = null;

            if (authToken.Authenticated == false)
            {
                creds = new BasicAuthenticationCredentials();
            }
            else if (authToken.WindowsCredentials)
            {
                creds = new NetworkAuthenticationCredentials
                {
                    Credentials = CredentialCache.DefaultNetworkCredentials
                };
            }
            else
            {
                creds = new TokenCredentials(authToken.Token);
            }

            var api = new UiPathWebApi(creds)
            {
                BaseUri = new Uri(authToken.URL)
            };

            api.SetRetryPolicy(null);
            api.SerializationSettings.Converters.Add(new SpecificItemDtoConverter());
            if (authToken.OrganizationUnitId.HasValue)
            {
                api.HttpClient.DefaultRequestHeaders.Add("X-UIPATH-OrganizationUnitId", authToken.OrganizationUnitId.Value.ToString());
            }
            return(api);
        }
Example #3
0
        internal static ApiHelper FromTestContext(TestContext testContext)
        {
            var testSettings = TestSettings.FromTestContext(testContext);

            var creds = new BasicAuthenticationCredentials();

            using (var client = new UiPathWebApi(creds)
            {
                BaseUri = new Uri(testSettings.URL)
            })
            {
                var loginModel = new LoginModel
                {
                    TenancyName            = testSettings.TenantName,
                    UsernameOrEmailAddress = testSettings.UserName,
                    Password = testSettings.Password
                };
                var response = client.Account.Authenticate(loginModel);
                var token    = (string)response.Result;

                var tokenCreds = new TokenCredentials(token);

                var api = new UiPathWebApi(tokenCreds)
                {
                    BaseUri = new Uri(testSettings.URL)
                };

                return(new ApiHelper
                {
                    _api = api
                });
            }
        }
        public static void MakeHttpRequest(this UiPathWebApi api, HttpMethod method, string url, object dto, out string responseContent, out HttpHeaders headers)
        {
            responseContent = null;
            headers         = null;
            using (var httpRequest = new HttpRequestMessage())
            {
                httpRequest.Method     = new HttpMethod(method.Method);
                httpRequest.RequestUri = new Uri(api.BaseUri, url);

                string requestContent = null;
                if (dto != null)
                {
                    requestContent      = SafeJsonConvert.SerializeObject(dto, api.SerializationSettings);
                    httpRequest.Content = new StringContent(requestContent, System.Text.Encoding.UTF8);
                    httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
                }

                var    shouldTrace  = ServiceClientTracing.IsEnabled;
                string invocationId = null;

                if (shouldTrace)
                {
                    invocationId = ServiceClientTracing.NextInvocationId.ToString();
                    ServiceClientTracing.SendRequest(invocationId, httpRequest);
                }

                api.Credentials?.ProcessHttpRequestAsync(httpRequest, CancellationToken.None).Wait();

                using (var httpResponse = api.HttpClient.SendAsync(httpRequest).Result)
                {
                    if (shouldTrace)
                    {
                        ServiceClientTracing.ReceiveResponse(invocationId, httpResponse);
                    }

                    if (httpResponse.Content != null)
                    {
                        responseContent = httpResponse.Content.ReadAsStringAsync().Result;
                    }
                    if (!httpResponse.IsSuccessStatusCode)
                    {
                        var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", httpResponse.StatusCode));
                        ex.Request  = new HttpRequestMessageWrapper(httpRequest, requestContent);
                        ex.Response = new HttpResponseMessageWrapper(httpResponse, responseContent);
                        throw ex;
                    }
                    headers = httpResponse.Headers;
                }
            }
        }