fsprojects / FsHttp

A lightweight F# HTTP library by @SchlenkR and @dawedawe
https://fsprojects.github.io/FsHttp/
Apache License 2.0
424 stars 42 forks source link

Make `logfn` optional #133

Closed 64J0 closed 10 months ago

64J0 commented 10 months ago

I'd like to have a toggle option to make the usage of logfn optional. After making several requests, it's weird to see the same string pattern appearing at the terminal. In this case, I'd prefer to not show it at all.

The version I'm using is 11.0.0.

Let me know if you think this is something that we could add to this project.

SchlenkR commented 10 months ago

Hi @64J0 and thanks for the issue. I'm not sure what's your exact use case, so let me explain how it is meant to work:

Can you explain your use case, and if there's still an issue after all?

Thank you.

SchlenkR commented 10 months ago

Note:

https://github.com/fsprojects/FsHttp/blob/74b10e38f2c8a2d54257cee8c36d3cc27d0aec83/src/FsHttp/Fsi.fs#L3-L15

https://github.com/fsprojects/FsHttp/blob/74b10e38f2c8a2d54257cee8c36d3cc27d0aec83/src/FsHttp/FsiInit.fs#L45-L53

64J0 commented 10 months ago

Nice, I was not aware of this configuration! Thanks for the information @RonaldSchlenker.

To give more context, I'm using an FSI script to trigger some requests,:

dotnet fsi samples/ResponseCachingApp/test-run.fsx

After adding the Fsi.disableDebugLogs() to the top of my script, the debug logs are not showing anymore.

64J0 commented 10 months ago

I'll close this issue now, thanks again @RonaldSchlenker!

SchlenkR commented 10 months ago

Nice, glad we could solve it :) Have a nice day!

64J0 commented 10 months ago

I'm using it on this PR -> https://github.com/giraffe-fsharp/Giraffe/pull/553

SchlenkR commented 10 months ago

Nice that you contribute to Giraffe! I have seen the fsx, and wanted to ask you what do you think of some changes:

#r "nuget: FsHttp, 11.0.0"

open System
open FsHttp

// Uncomment if you don't want FsHttp debug logs
// Fsi.disableDebugLogs()

type QueryParams = (string * obj) list
type Url = Url of string
type Title = Title of string

let urls =
    {|
        notCached = Url "http://localhost:5000/cached/not"
        publicCached = Url "http://localhost:5000/cached/public"
        privateCached = Url "http://localhost:5000/cached/private"
        publicCachedNoVaryByQueryKeys = Url "http://localhost:5000/cached/vary/not"
        cachedVaryByQueryKeys = Url "http://localhost:5000/cached/vary/yes"
    |}

let queryParams1: QueryParams = [ ("query1", "a"); ("query2", "b") ]
let queryParams2: QueryParams = [ ("query1", "c"); ("query2", "d") ]

let waitForOneSecond () =
    do Threading.Thread.Sleep (TimeSpan.FromSeconds 1.0)

let makeRequest (Url url: Url) (queryParams: list<string * obj>) =
    let response =
        http {
            GET url
            CacheControl "max-age=3600"
            query queryParams
        }
        |> Request.send
        |> Response.toFormattedText

    printfn "%s" response
    printfn ""

let printRunTitle (Title title) =
    printfn "-----------------------------------"
    printfn "%s" title
    printfn ""

let printTimeTaken (duration: TimeSpan) =
    printfn "The time it took to finish:"
    printfn "%.2f seconds" duration.TotalSeconds
    printfn ""

let run (qps: QueryParams list) title url =
    printRunTitle title

    let stopWatch = Diagnostics.Stopwatch.StartNew()
    for queryParams in qps do
        makeRequest url queryParams |> waitForOneSecond

    stopWatch.Stop()
    printTimeTaken stopWatch.Elapsed

let runFiveRequests = run [ for _ in 1..5 do [] ]

let testPublicCachedNoVaryByQueryKeys () =
    let allQueryParams = 
        [
            queryParams1
            queryParams1
            queryParams2
            queryParams2
        ]
    let title = Title "Testing the /cached/vary/not endpoint"
    let url = urls.publicCachedNoVaryByQueryKeys
    run allQueryParams title url

let testCachedVaryByQueryKeys () =
    let title = Title "Testing the /cached/vary/yes endpoint"
    let url = urls.cachedVaryByQueryKeys
    let allQueryParams = 
        [
            queryParams1
            queryParams1
            queryParams2
            queryParams2
        ]
    run allQueryParams title url

let main () =
    runFiveRequests (Title "Testing the /cached/not endpoint") urls.notCached
    runFiveRequests (Title "Testing the /cached/public endpoint") urls.publicCached
    runFiveRequests (Title "Testing the /cached/private endpoint") urls.privateCached
    testPublicCachedNoVaryByQueryKeys ()
    testCachedVaryByQueryKeys ()

main ()

The changes are basically:

You can think about it and if you like, use it. Say hi to Jimmy, and have a good day / night.

P.S.: As an alternative to blocking the thread using Thread.Sleep, you could lift your computation to Async<_> and use Async.Sleep (or also Task<_>).

64J0 commented 10 months ago

Great suggestions @RonaldSchlenker, I'm going to apply it there.