Example #1
0
        private async Task <IEnumerable <HttpRequest> > LoadInternal(Uri[] source)
        {
            var restRequests = new List <HttpRequest>();
            var processor    = new SwaggerProcessor(_swaggerOptions);

            foreach (var uri in source)
            {
                using (var handler = new HttpClientHandler())
                {
                    if (_swaggerOptions.UseNTLMAuth)
                    {
                        NetworkCredential credentials;
                        if (string.IsNullOrEmpty(_swaggerOptions.UserName) || string.IsNullOrEmpty(_swaggerOptions.Password))
                        {
                            credentials = CredentialCache.DefaultNetworkCredentials;
                        }
                        else
                        {
                            credentials = new NetworkCredential(_swaggerOptions.UserName, _swaggerOptions.Password);
                        }

                        var credentialsCache = new CredentialCache {
                            { uri, "NTLM", credentials }
                        };
                        handler.Credentials = credentialsCache;
                    }

                    if (_swaggerOptions.DisableSSLValidation && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
                    {
                        handler.ClientCertificateOptions = ClientCertificateOption.Manual;
                        handler.ServerCertificateCustomValidationCallback =
                            (httpRequestMessage, cert, cetChain, policyErrors) => true;
                    }

                    using (var httpClient = new HttpClient(handler))
                    {
                        if (_swaggerOptions.UseBasicAuth)
                        {
                            var authenticationString = $"{_swaggerOptions.UserName}:{_swaggerOptions.Password}";
                            var base64EncodedAuthenticationString = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(authenticationString));
                            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString);
                        }

                        var stream = await httpClient.GetStreamAsync(uri);

                        var openApiDocument = SwaggerParser.GenerateOpenApiDocument(stream, _swaggerOptions.UseStrictParsing);

                        var url = _swaggerOptions.BaseUrl != null ? _swaggerOptions.BaseUrl : new Uri($"{uri.Scheme}://{uri.Host}");

                        var requests = processor.MapFromOpenApiDocument(url, openApiDocument);

                        restRequests.AddRange(requests);
                    }
                }
            }

            return(restRequests);
        }
Example #2
0
        /// <summary>
        /// Load swagger file sources from a files on a disk
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public Task <IEnumerable <HttpRequest> > Load(IEnumerable <FileInfo> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var restRequests = new List <HttpRequest>();
            var processor    = new SwaggerProcessor(_swaggerOptions);

            foreach (var filePath in source)
            {
                using (var sourceStream = File.OpenRead(filePath.FullName))
                {
                    var openApiDocument = SwaggerParser.GenerateOpenApiDocument(sourceStream, _swaggerOptions.UseStrictParsing);

                    var requests = processor.MapFromOpenApiDocument(_swaggerOptions.BaseUrl, openApiDocument);

                    restRequests.AddRange(requests);
                }
            }

            return(Task.FromResult(restRequests.AsEnumerable()));
        }