Closed nhirschey closed 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
let fredKey = KeyFile.Load(__SOURCE_DIRECTORY__ + "/fredKey.json").FredKey
let myFred = Fred fredKey
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):
See the changes: DavideCosta-DevKeyFun Branch.
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
Can we make a function
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.