nhirschey / FSharp.Data.Fred

Wrapper for Federal Reserve Economic Data API https://nhirschey.github.io/FSharp.Data.Fred/
MIT License
2 stars 3 forks source link

Dev Key library function #4

Closed nhirschey closed 2 years ago

nhirschey commented 2 years ago

Can we make a function


let devKey () = 
    let var = "FRED_KEY"
    if envVars.Contains var then 
        envVars.[var] :?> string
    elif File.Exists(KeyJson) then 
        KeyFile.Load(KeyJson).FredKey
    else failwith "could not find Fred API key"

then whenever we want to use it in docs or testing we can just call that function.

Is it possible to make it a static member of the Fred type so that we could do let key = Fred.devKey() in docs? I can’t recall if this would work. If not just do the function as above.


type Fred(key:string) =
        member this.Series = Series.Series(key)
        static member this.devKey() = … code …        
nhirschey commented 2 years ago

devKey should probably accept a keyFile:string argument:

let devKey (keyFile:string) = 
    let var = "FRED_KEY"
    if envVars.Contains var then 
        envVars.[var] :?> string
    elif File.Exists(keyFile) then 
        KeyFile.Load(keyFile).FredKey
    else failwith "Could not find Fred API key in keyFile or FRED_KEY environment variable."

We should also probably

  1. Document in README.md how to use the fredKey.json for development purposes.
  2. Document how to load the key from json as a user
let fredKey = KeyFile.Load(__SOURCE_DIRECTORY__ + "/fredKey.json").FredKey

let myFred = Fred fredKey

See https://github.com/fslaborg/RProvider#developing

DavideGCosta commented 2 years ago

Hello professor,

I tried to solve this problem but only found dead ends. This is sketchy because everything works just fine, except for the index.fsx file.

The following error occurs in the HTML page (on the methods that use FSharp.Data):

Screenshot 2021-10-08 at 14 08 02

See the changes: DavideCosta-DevKeyFun Branch.

nhirschey commented 2 years ago

Hmm, not sure what's up.

Looking into this I saw there's a lot of places where "developer" as key are not caught, like https://github.com/nhirschey/FSharp.Data.Fred/blob/3d4aa00c23c968c188e0fd7302baefae768a0c27/src/FSharp.Data.Fred/JsonApi.fs#L182

It's probably better to do the match on developer key inside the request function to prevent this from happening

    module internal Helpers =
        let request key endpoint query =
            match key with
            | "developer" ->
                match endpoint with
                | "series/search" -> EmbeddedResources.SearchResponseSample
                .... other endpoints
                | _ -> failwith "unknown endpoint used with developer key"
            | _ ->  
              Http.RequestString($"https://api.stlouisfed.org/fred/{endpoint}?",
                       query = query @ [ "api_key", key; "file_type", "json"], 
                       headers = [ HttpRequestHeaders.UserAgent "FSharp.Data.Fred" 
                                   HttpRequestHeaders.Accept HttpContentTypes.Json ])

Can you submit a new pull request with this fix? It can be on a new separate branch from your dev-keyFun attempts