Skip to content

Commit

Permalink
Redone "PR Webexception handling, possible fix for #132".
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorium authored and sergey-tihon committed Jun 8, 2020
1 parent 4f76f3c commit b01af09
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
14 changes: 2 additions & 12 deletions src/SwaggerProvider.DesignTime/Provider.OpenApiClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 2 additions & 23 deletions src/SwaggerProvider.DesignTime/Provider.SwaggerClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
44 changes: 43 additions & 1 deletion src/SwaggerProvider.DesignTime/Utils.fs
Original file line number Diff line number Diff line change
@@ -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<_>()
Expand Down

0 comments on commit b01af09

Please sign in to comment.