Skip to content

Commit

Permalink
Moved all defaults to Defaults module
Browse files Browse the repository at this point in the history
  • Loading branch information
SchlenkR committed Aug 5, 2023
1 parent 7a5c012 commit 43ec350
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 183 deletions.
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
- `Helper` is a module instead of a namespace, and some things were moved.
- All transformers in config are a list of transformers instead of a single item.
- Removed `setHttpClient`. Please use `setHttpClientFactory` instead.
- `setHttpClientFactory` takes a `Config` as input argument.
</PackageReleaseNotes>
</PropertyGroup>

Expand Down
84 changes: 84 additions & 0 deletions src/FsHttp/Defaults.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module internal FsHttp.Defaults

open System
open System.Net
open System.Net.Http

open FsHttp
open System.Text.Json

let defaultJsonDocumentOptions = JsonDocumentOptions()
let defaultJsonSerializerOptions = JsonSerializerOptions JsonSerializerDefaults.Web

let defaultHttpClientFactory (config: Config) =
let getDefaultSslHandler ignoreSslIssues =
#if NETSTANDARD2_0 || NETSTANDARD2_1
let handler = new HttpClientHandler()

if ignoreSslIssues then
handler.ServerCertificateCustomValidationCallback <- (fun msg cert chain errors -> true)

handler
#else
let handler =
new SocketsHttpHandler(UseCookies = false, PooledConnectionLifetime = TimeSpan.FromMinutes 5.0)

if ignoreSslIssues then
handler.SslOptions <-
let options = Security.SslClientAuthenticationOptions()

let callback =
Security.RemoteCertificateValidationCallback(fun sender cert chain errors -> true)

do options.RemoteCertificateValidationCallback <- callback
options

handler
#endif

let ignoreSslIssues =
match config.certErrorStrategy with
| Default -> false
| AlwaysAccept -> true

let handler =
let initHandler = getDefaultSslHandler ignoreSslIssues
config.httpClientHandlerTransformers |> List.fold (fun c n -> n c) initHandler

match config.proxy with
| Some proxy ->
let webProxy = WebProxy(proxy.url)

match proxy.credentials with
| Some cred ->
webProxy.UseDefaultCredentials <- false
webProxy.Credentials <- cred
| None -> webProxy.UseDefaultCredentials <- true

handler.Proxy <- webProxy
| None -> ()

let client = new HttpClient(handler)
do config.timeout |> Option.iter (fun timeout -> client.Timeout <- timeout)
client

let defaultHeadersAndBodyPrintMode = {
format = true
maxLength = Some 7000
}

let defaultConfig = {
timeout = None
printHint = {
requestPrintMode = HeadersAndBody(defaultHeadersAndBodyPrintMode)
responsePrintMode = HeadersAndBody(defaultHeadersAndBodyPrintMode)
}
httpMessageTransformers = []
httpClientHandlerTransformers = []
httpClientTransformers = []
httpClientFactory = defaultHttpClientFactory
httpCompletionOption = HttpCompletionOption.ResponseHeadersRead
proxy = None
certErrorStrategy = Default
bufferResponseContent = false
}
2 changes: 1 addition & 1 deletion src/FsHttp/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Config = {
httpCompletionOption: System.Net.Http.HttpCompletionOption
proxy: Proxy option
certErrorStrategy: CertErrorStrategy
httpClientFactory: (unit -> System.Net.Http.HttpClient) option
httpClientFactory: Config -> System.Net.Http.HttpClient
// Calls `LoadIntoBufferAsync` of the response's HttpContent immediately after receiving.
bufferResponseContent: bool
}
Expand Down
74 changes: 20 additions & 54 deletions src/FsHttp/Dsl.CE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -376,65 +376,47 @@ type IRequestContext<'self> with
type IRequestContext<'self> with

[<CustomOperation("config_update")>]
member inline this.Update(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, configTransformer) =
member this.Update(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, configTransformer) =
Config.update configTransformer context.Self

[<CustomOperation("config_set")>]
member inline this.Set(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, configTransformer) =
member this.Set(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, configTransformer) =
Config.set configTransformer context.Self

// TODO: Provide certStrategy configs
[<CustomOperation("config_ignoreCertIssues")>]
member inline this.IgnoreCertIssues(context: IRequestContext<#IConfigure<ConfigTransformer, _>>) =
member this.IgnoreCertIssues(context: IRequestContext<#IConfigure<ConfigTransformer, _>>) =
Config.ignoreCertIssues context.Self

[<CustomOperation("config_timeout")>]
member inline this.Timeout(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, value) =
member this.Timeout(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, value) =
Config.timeout value context.Self

[<CustomOperation("config_timeoutInSeconds")>]
member inline this.TimeoutInSeconds(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, value) =
member this.TimeoutInSeconds(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, value) =
Config.timeoutInSeconds value context.Self

[<CustomOperation("config_setHttpClientFactory")>]
member inline this.SetHttpClientFactory
(
context: IRequestContext<#IConfigure<ConfigTransformer, _>>,
httpClientFactory
) =
member this.SetHttpClientFactory(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, httpClientFactory) =
Config.setHttpClientFactory httpClientFactory context.Self

[<CustomOperation("config_transformHttpClient")>]
member inline this.TransformHttpClient(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, transformer) =
member this.TransformHttpClient(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, transformer) =
Config.transformHttpClient transformer context.Self

[<CustomOperation("config_transformHttpRequestMessage")>]
member inline this.TransformHttpRequestMessage
(
context: IRequestContext<#IConfigure<ConfigTransformer, _>>,
transformer
) =
member this.TransformHttpRequestMessage(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, transformer) =
Config.transformHttpRequestMessage transformer context.Self

[<CustomOperation("config_transformHttpClientHandler")>]
member inline this.TransformHttpClientHandler
(
context: IRequestContext<#IConfigure<ConfigTransformer, _>>,
transformer
) =
member this.TransformHttpClientHandler(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, transformer) =
Config.transformHttpClientHandler transformer context.Self

[<CustomOperation("config_proxy")>]
member inline this.Proxy(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, url) =
Config.proxy url context.Self
member this.Proxy(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, url) = Config.proxy url context.Self

[<CustomOperation("config_proxyWithCredentials")>]
member inline this.ProxyWithCredentials
(
context: IRequestContext<#IConfigure<ConfigTransformer, _>>,
url,
credentials
) =
member this.ProxyWithCredentials(context: IRequestContext<#IConfigure<ConfigTransformer, _>>, url, credentials) =
Config.proxyWithCredentials url credentials context.Self


Expand All @@ -445,53 +427,37 @@ type IRequestContext<'self> with
type IRequestContext<'self> with

[<CustomOperation("print_withConfig")>]
member inline this.WithConfig(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>, updatePrintHint) =
member this.WithConfig(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>, updatePrintHint) =
Print.withConfig updatePrintHint context.Self

[<CustomOperation("print_withRequestPrintMode")>]
member inline this.WithRequestPrintMode
(
context: IRequestContext<#IConfigure<PrintHintTransformer, _>>,
updatePrintMode
) =
member this.WithRequestPrintMode(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>, updatePrintMode) =
Print.withRequestPrintMode updatePrintMode context.Self

[<CustomOperation("print_withResponsePrintMode")>]
member inline this.WithResponsePrintMode
(
context: IRequestContext<#IConfigure<PrintHintTransformer, _>>,
updatePrintMode
) =
member this.WithResponsePrintMode(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>, updatePrintMode) =
Print.withResponsePrintMode updatePrintMode context.Self

[<CustomOperation("print_withResponseBody")>]
member inline this.WithResponseBody
(
context: IRequestContext<#IConfigure<PrintHintTransformer, _>>,
updateBodyPrintMode
) =
member this.WithResponseBody(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>, updateBodyPrintMode) =
Print.withResponseBody updateBodyPrintMode context.Self

[<CustomOperation("print_useObjectFormatting")>]
member inline this.UseObjectFormatting(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>) =
member this.UseObjectFormatting(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>) =
Print.useObjectFormatting context.Self

[<CustomOperation("print_headerOnly")>]
member inline this.HeaderOnly(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>) =
member this.HeaderOnly(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>) =
Print.headerOnly context.Self

[<CustomOperation("print_withResponseBodyLength")>]
member inline this.WithResponseBodyLength
(
context: IRequestContext<#IConfigure<PrintHintTransformer, _>>,
maxLength
) =
member this.WithResponseBodyLength(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>, maxLength) =
Print.withResponseBodyLength maxLength context.Self

[<CustomOperation("print_withResponseBodyFormat")>]
member inline this.WithResponseBodyFormat(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>, format) =
member this.WithResponseBodyFormat(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>, format) =
Print.withResponseBodyFormat format context.Self

[<CustomOperation("print_withResponseBodyExpanded")>]
member inline this.WithResponseBodyExpanded(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>) =
member this.WithResponseBodyExpanded(context: IRequestContext<#IConfigure<PrintHintTransformer, _>>) =
Print.withResponseBodyExpanded context.Self
75 changes: 39 additions & 36 deletions src/FsHttp/Dsl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -443,27 +443,30 @@ module Multipart =

module Config =
module With =
let inline ignoreCertIssues config = { config with certErrorStrategy = AlwaysAccept }
let ignoreCertIssues config = { config with certErrorStrategy = AlwaysAccept }

let inline timeout value config = { config with timeout = value }
let timeout value config = { config with timeout = value }

let inline timeoutInSeconds value config = { config with timeout = Some(TimeSpan.FromSeconds value) }
let timeoutInSeconds value config = { config with timeout = Some(TimeSpan.FromSeconds value) }

let inline setHttpClientFactory (clientFactory: unit -> HttpClient) config = {
let setHttpClientFactory httpClientFactory config = { config with httpClientFactory = httpClientFactory }

let transformHttpClient transformer config = {
config with
httpClientFactory = Some clientFactory
httpClientTransformers = config.httpClientTransformers @ [ transformer ]
}

let inline transformHttpClient transformer config =
{ config with httpClientTransformers = config.httpClientTransformers @ [transformer] }

let inline transformHttpRequestMessage transformer config =
{ config with httpMessageTransformers = config.httpMessageTransformers @ [transformer] }
let transformHttpRequestMessage transformer config = {
config with
httpMessageTransformers = config.httpMessageTransformers @ [ transformer ]
}

let inline transformHttpClientHandler transformer config =
{ config with httpClientHandlerTransformers = config.httpClientHandlerTransformers @ [transformer] }
let transformHttpClientHandler transformer config = {
config with
httpClientHandlerTransformers = config.httpClientHandlerTransformers @ [ transformer ]
}

let inline proxy url config = {
let proxy url config = {
config with
proxy =
Some {
Expand All @@ -472,7 +475,7 @@ module Config =
}
}

let inline proxyWithCredentials url credentials config = {
let proxyWithCredentials url credentials config = {
config with
proxy =
Some {
Expand All @@ -481,81 +484,81 @@ module Config =
}
}

let inline update transformer (context: IConfigure<ConfigTransformer, _>) = context.Configure transformer
let update transformer (context: IConfigure<ConfigTransformer, _>) = context.Configure transformer

let inline set (config: Config) context = context |> update (fun _ -> config)
let set (config: Config) context = context |> update (fun _ -> config)

let inline ignoreCertIssues context = context |> update (fun config -> config |> With.ignoreCertIssues)
let ignoreCertIssues context = context |> update (fun config -> config |> With.ignoreCertIssues)

let inline timeout value context = context |> update (fun config -> config |> With.timeout (Some value))
let timeout value context = context |> update (fun config -> config |> With.timeout (Some value))

let inline timeoutInSeconds value context = context |> update (fun config -> config |> With.timeoutInSeconds value)
let timeoutInSeconds value context = context |> update (fun config -> config |> With.timeoutInSeconds value)

let inline setHttpClientFactory (clientFactory: unit -> HttpClient) context =
let setHttpClientFactory httpClientFactory context =
context
|> update (fun config -> config |> With.setHttpClientFactory clientFactory)
|> update (fun config -> config |> With.setHttpClientFactory httpClientFactory)

let inline transformHttpClient transformer context =
let transformHttpClient transformer context =
context |> update (fun config -> config |> With.transformHttpClient transformer)

let inline transformHttpRequestMessage transformer context =
let transformHttpRequestMessage transformer context =
context
|> update (fun config -> config |> With.transformHttpRequestMessage transformer)

let inline transformHttpClientHandler transformer context =
let transformHttpClientHandler transformer context =
context
|> update (fun config -> config |> With.transformHttpClientHandler transformer)

let inline proxy url context = context |> update (fun config -> config |> With.proxy url)
let proxy url context = context |> update (fun config -> config |> With.proxy url)

let inline proxyWithCredentials url credentials context =
let proxyWithCredentials url credentials context =
context
|> update (fun config -> config |> With.proxyWithCredentials url credentials)


module Print =

let inline withConfig transformer (context: IConfigure<PrintHintTransformer, _>) = context.Configure transformer
let withConfig transformer (context: IConfigure<PrintHintTransformer, _>) = context.Configure transformer

let inline withRequestPrintMode updatePrintMode context =
let withRequestPrintMode updatePrintMode context =
context
|> withConfig (fun printHint -> {
printHint with
requestPrintMode = updatePrintMode printHint.requestPrintMode
})

let inline withResponsePrintMode updatePrintMode context =
let withResponsePrintMode updatePrintMode context =
context
|> withConfig (fun printHint -> {
printHint with
responsePrintMode = updatePrintMode printHint.responsePrintMode
})

let inline withResponseBody updateBodyPrintMode context =
let withResponseBody updateBodyPrintMode context =
context
|> withResponsePrintMode (fun printMode ->
match printMode with
| AsObject
| HeadersOnly -> updateBodyPrintMode (GlobalConfig.defaultHeadersAndBodyPrintMode ())
| HeadersOnly -> updateBodyPrintMode (Defaults.defaultHeadersAndBodyPrintMode)
| HeadersAndBody x -> updateBodyPrintMode x
|> HeadersAndBody
)

let inline useObjectFormatting context =
let useObjectFormatting context =
context
|> withRequestPrintMode (fun _ -> AsObject)
|> withResponsePrintMode (fun _ -> AsObject)

let inline headerOnly context = context |> withResponsePrintMode (fun _ -> HeadersOnly)
let headerOnly context = context |> withResponsePrintMode (fun _ -> HeadersOnly)

let inline withResponseBodyLength maxLength context =
let withResponseBodyLength maxLength context =
context
|> withResponseBody (fun bodyPrintMode -> { bodyPrintMode with maxLength = Some maxLength })

let inline withResponseBodyFormat format context =
let withResponseBodyFormat format context =
context
|> withResponseBody (fun bodyPrintMode -> { bodyPrintMode with format = format })

let inline withResponseBodyExpanded context =
let withResponseBodyExpanded context =
context
|> withResponseBody (fun bodyPrintMode -> { bodyPrintMode with maxLength = None })
Loading

0 comments on commit 43ec350

Please sign in to comment.