From ef1198fa7195e79adbc13ca3df346a83ec672649 Mon Sep 17 00:00:00 2001 From: Tuomas Hietanen Date: Sun, 7 Jun 2020 21:42:52 +0100 Subject: [PATCH] Redone "PR Webexception handling, possible fix for #132". --- .../Provider.OpenApiClient.fs | 14 +----- .../Provider.SwaggerClient.fs | 25 +---------- src/SwaggerProvider.DesignTime/Utils.fs | 44 ++++++++++++++++++- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/SwaggerProvider.DesignTime/Provider.OpenApiClient.fs b/src/SwaggerProvider.DesignTime/Provider.OpenApiClient.fs index 69b4d162..7c20e20f 100644 --- a/src/SwaggerProvider.DesignTime/Provider.OpenApiClient.fs +++ b/src/SwaggerProvider.DesignTime/Provider.OpenApiClient.fs @@ -55,18 +55,8 @@ type public OpenApiClientTypeProvider(cfg : TypeProviderConfig) as this = let addCache() = lazy let schemaData = - match schemaPathRaw.StartsWith("http", true, null) with - | true -> - let request = new HttpRequestMessage(HttpMethod.Get, schemaPathRaw) - // using a custom handler means that we can set the default credentials. - use handler = new HttpClientHandler(UseDefaultCredentials = true) - use client = new HttpClient(handler) - async { - let! response = client.SendAsync(request) |> Async.AwaitTask - return! response.Content.ReadAsStringAsync() |> Async.AwaitTask - } |> Async.RunSynchronously - | false -> - schemaPathRaw |> IO.File.ReadAllText + SwaggerProvider.Internal.SchemaReader.readSchemaPath "" schemaPathRaw + |> Async.RunSynchronously let openApiReader = Microsoft.OpenApi.Readers.OpenApiStringReader() let (schema, diagnostic) = openApiReader.Read(schemaData) diff --git a/src/SwaggerProvider.DesignTime/Provider.SwaggerClient.fs b/src/SwaggerProvider.DesignTime/Provider.SwaggerClient.fs index 6ec671b6..4bd662a2 100644 --- a/src/SwaggerProvider.DesignTime/Provider.SwaggerClient.fs +++ b/src/SwaggerProvider.DesignTime/Provider.SwaggerClient.fs @@ -2,7 +2,6 @@ namespace SwaggerProvider open System open System.Reflection -open System.Net.Http open ProviderImplementation.ProvidedTypes open Microsoft.FSharp.Core.CompilerServices open Swagger @@ -70,28 +69,8 @@ type public SwaggerTypeProvider(cfg : TypeProviderConfig) as this = let addCache() = lazy let schemaData = - match schemaPathRaw.StartsWith("http", true, null) with - | true -> - let headers = - headersStr.Split('|') - |> Seq.choose (fun x -> - let pair = x.Split('=') - if (pair.Length = 2) - then Some (pair.[0],pair.[1]) - else None - ) - let request = new HttpRequestMessage(HttpMethod.Get, schemaPathRaw) - for (name, value) in headers do - request.Headers.TryAddWithoutValidation(name, value) |> ignore - // using a custom handler means that we can set the default credentials. - use handler = new HttpClientHandler(UseDefaultCredentials = true) - use client = new HttpClient(handler) - async { - let! response = client.SendAsync(request) |> Async.AwaitTask - return! response.Content.ReadAsStringAsync() |> Async.AwaitTask - } |> Async.RunSynchronously - | false -> - schemaPathRaw |> IO.File.ReadAllText + SwaggerProvider.Internal.SchemaReader.readSchemaPath headersStr schemaPathRaw + |> Async.RunSynchronously let schema = SwaggerParser.parseSchema schemaData let defCompiler = DefinitionCompiler(schema, preferNullable) diff --git a/src/SwaggerProvider.DesignTime/Utils.fs b/src/SwaggerProvider.DesignTime/Utils.fs index 0a4b79b3..e50fd535 100644 --- a/src/SwaggerProvider.DesignTime/Utils.fs +++ b/src/SwaggerProvider.DesignTime/Utils.fs @@ -1,4 +1,46 @@ -namespace SwaggerProvider.Internal +namespace SwaggerProvider.Internal + +module SchemaReader = + open System + open System.Net.Http + + let readSchemaPath (headersStr:string) (schemaPathRaw:string) = + async { + match schemaPathRaw.StartsWith("http", true, null) with + | true -> + let headers = + headersStr.Split('|') + |> Seq.choose (fun x -> + let pair = x.Split('=') + if (pair.Length = 2) + then Some (pair.[0],pair.[1]) + else None + ) + let request = new HttpRequestMessage(HttpMethod.Get, schemaPathRaw) + for (name, value) in headers do + request.Headers.TryAddWithoutValidation(name, value) |> ignore + // using a custom handler means that we can set the default credentials. + use handler = new HttpClientHandler(UseDefaultCredentials = true) + use client = new HttpClient(handler) + let! res = + async { + let! response = client.SendAsync(request) |> Async.AwaitTask + return! response.Content.ReadAsStringAsync() |> Async.AwaitTask + } |> Async.Catch + match res with + | Choice1Of2 x -> return x + | Choice2Of2 (:? System.Net.WebException as wex) -> + use stream = wex.Response.GetResponseStream() + use reader = new System.IO.StreamReader(stream) + let err = reader.ReadToEnd() + return + if String.IsNullOrEmpty err then raise wex + else err.ToString() + | Choice2Of2 e -> return failwith(e.ToString()) + | false -> + use sr = new System.IO.StreamReader(schemaPathRaw) + return! sr.ReadToEndAsync() |> Async.AwaitTask + } type UniqueNameGenerator() = let hash = System.Collections.Generic.HashSet<_>()