How are F# Type Providers used on a changing Rest/JSON Endpoint?












2














I am trying to use F# type providers to include weather data in my application. I am using OpenWeatherMap.org to get current weather data. https://openweathermap.org/current



Sometimes the OpenWeatherMap response does not show any JSON for rain (Eg. There is no rain for a 1 hour window).



So if I do something like this:



type WeatherForecast= JsonProvider<"http://api.openweathermap.org/data/2.5/weather?lat=39.64&lon=-74.28&appid=MyKeyICantSharePublicly">
...
let mydata = WeatherForecast.Load("http://api.openweathermap.org/data/2.5/weather/?lat=12&lon=12&appid=MyKeyICantSharePublicly)

printf "%s" (string mydata.Rain.``1h``)


The printf statement will fail because it doesn't always know what mydata.Rain is because the type provider does not provide the Rain information anymore.



Depending on when I try to compile my application, the build will fail because of the type provider.



How can I use type providers with a changing Rest/Json Endpoint like this?



Example data.



{
"coord": {
"lon": -74.28,
"lat": 39.64
},
"weather": [
{
"id": 701,
"main": "Mist",
"description": "mist",
"icon": "50d"
},
{
"id": 721,
"main": "Haze",
"description": "haze",
"icon": "50d"
}
],
"base": "stations",
"main": {
"temp": 7.71,
"pressure": 1012,
"humidity": 93,
"temp_min": 7,
"temp_max": 9.4
},
"visibility": 4023,
"wind": {
"speed": 5.7,
"deg": 2...









share|improve this question



























    2














    I am trying to use F# type providers to include weather data in my application. I am using OpenWeatherMap.org to get current weather data. https://openweathermap.org/current



    Sometimes the OpenWeatherMap response does not show any JSON for rain (Eg. There is no rain for a 1 hour window).



    So if I do something like this:



    type WeatherForecast= JsonProvider<"http://api.openweathermap.org/data/2.5/weather?lat=39.64&lon=-74.28&appid=MyKeyICantSharePublicly">
    ...
    let mydata = WeatherForecast.Load("http://api.openweathermap.org/data/2.5/weather/?lat=12&lon=12&appid=MyKeyICantSharePublicly)

    printf "%s" (string mydata.Rain.``1h``)


    The printf statement will fail because it doesn't always know what mydata.Rain is because the type provider does not provide the Rain information anymore.



    Depending on when I try to compile my application, the build will fail because of the type provider.



    How can I use type providers with a changing Rest/Json Endpoint like this?



    Example data.



    {
    "coord": {
    "lon": -74.28,
    "lat": 39.64
    },
    "weather": [
    {
    "id": 701,
    "main": "Mist",
    "description": "mist",
    "icon": "50d"
    },
    {
    "id": 721,
    "main": "Haze",
    "description": "haze",
    "icon": "50d"
    }
    ],
    "base": "stations",
    "main": {
    "temp": 7.71,
    "pressure": 1012,
    "humidity": 93,
    "temp_min": 7,
    "temp_max": 9.4
    },
    "visibility": 4023,
    "wind": {
    "speed": 5.7,
    "deg": 2...









    share|improve this question

























      2












      2








      2







      I am trying to use F# type providers to include weather data in my application. I am using OpenWeatherMap.org to get current weather data. https://openweathermap.org/current



      Sometimes the OpenWeatherMap response does not show any JSON for rain (Eg. There is no rain for a 1 hour window).



      So if I do something like this:



      type WeatherForecast= JsonProvider<"http://api.openweathermap.org/data/2.5/weather?lat=39.64&lon=-74.28&appid=MyKeyICantSharePublicly">
      ...
      let mydata = WeatherForecast.Load("http://api.openweathermap.org/data/2.5/weather/?lat=12&lon=12&appid=MyKeyICantSharePublicly)

      printf "%s" (string mydata.Rain.``1h``)


      The printf statement will fail because it doesn't always know what mydata.Rain is because the type provider does not provide the Rain information anymore.



      Depending on when I try to compile my application, the build will fail because of the type provider.



      How can I use type providers with a changing Rest/Json Endpoint like this?



      Example data.



      {
      "coord": {
      "lon": -74.28,
      "lat": 39.64
      },
      "weather": [
      {
      "id": 701,
      "main": "Mist",
      "description": "mist",
      "icon": "50d"
      },
      {
      "id": 721,
      "main": "Haze",
      "description": "haze",
      "icon": "50d"
      }
      ],
      "base": "stations",
      "main": {
      "temp": 7.71,
      "pressure": 1012,
      "humidity": 93,
      "temp_min": 7,
      "temp_max": 9.4
      },
      "visibility": 4023,
      "wind": {
      "speed": 5.7,
      "deg": 2...









      share|improve this question













      I am trying to use F# type providers to include weather data in my application. I am using OpenWeatherMap.org to get current weather data. https://openweathermap.org/current



      Sometimes the OpenWeatherMap response does not show any JSON for rain (Eg. There is no rain for a 1 hour window).



      So if I do something like this:



      type WeatherForecast= JsonProvider<"http://api.openweathermap.org/data/2.5/weather?lat=39.64&lon=-74.28&appid=MyKeyICantSharePublicly">
      ...
      let mydata = WeatherForecast.Load("http://api.openweathermap.org/data/2.5/weather/?lat=12&lon=12&appid=MyKeyICantSharePublicly)

      printf "%s" (string mydata.Rain.``1h``)


      The printf statement will fail because it doesn't always know what mydata.Rain is because the type provider does not provide the Rain information anymore.



      Depending on when I try to compile my application, the build will fail because of the type provider.



      How can I use type providers with a changing Rest/Json Endpoint like this?



      Example data.



      {
      "coord": {
      "lon": -74.28,
      "lat": 39.64
      },
      "weather": [
      {
      "id": 701,
      "main": "Mist",
      "description": "mist",
      "icon": "50d"
      },
      {
      "id": 721,
      "main": "Haze",
      "description": "haze",
      "icon": "50d"
      }
      ],
      "base": "stations",
      "main": {
      "temp": 7.71,
      "pressure": 1012,
      "humidity": 93,
      "temp_min": 7,
      "temp_max": 9.4
      },
      "visibility": 4023,
      "wind": {
      "speed": 5.7,
      "deg": 2...






      functional-programming f# type-providers fsharp.data.typeproviders






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 16:34









      Philip Nguyen

      3691421




      3691421
























          1 Answer
          1






          active

          oldest

          votes


















          3














          Instead of a url, you should provide some samples with and without rain:



          type People = JsonProvider<""" 
          [ { "name":"John", "age":94 },
          { "name":"Tomas" } ] """, SampleIsList=true>

          for item in People.GetSamples() do
          printf "%s " item.Name
          item.Age |> Option.iter (printf "(%d)")
          printfn ""


          The samples above have two records one includes age the other one doesn't. That makes the age field an int option.



          From the documentation: http://fsharp.github.io/FSharp.Data/library/JsonProvider.html






          share|improve this answer























          • Thank you for the help!
            – Philip Nguyen
            Nov 20 at 19:08











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397494%2fhow-are-f-type-providers-used-on-a-changing-rest-json-endpoint%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Instead of a url, you should provide some samples with and without rain:



          type People = JsonProvider<""" 
          [ { "name":"John", "age":94 },
          { "name":"Tomas" } ] """, SampleIsList=true>

          for item in People.GetSamples() do
          printf "%s " item.Name
          item.Age |> Option.iter (printf "(%d)")
          printfn ""


          The samples above have two records one includes age the other one doesn't. That makes the age field an int option.



          From the documentation: http://fsharp.github.io/FSharp.Data/library/JsonProvider.html






          share|improve this answer























          • Thank you for the help!
            – Philip Nguyen
            Nov 20 at 19:08
















          3














          Instead of a url, you should provide some samples with and without rain:



          type People = JsonProvider<""" 
          [ { "name":"John", "age":94 },
          { "name":"Tomas" } ] """, SampleIsList=true>

          for item in People.GetSamples() do
          printf "%s " item.Name
          item.Age |> Option.iter (printf "(%d)")
          printfn ""


          The samples above have two records one includes age the other one doesn't. That makes the age field an int option.



          From the documentation: http://fsharp.github.io/FSharp.Data/library/JsonProvider.html






          share|improve this answer























          • Thank you for the help!
            – Philip Nguyen
            Nov 20 at 19:08














          3












          3








          3






          Instead of a url, you should provide some samples with and without rain:



          type People = JsonProvider<""" 
          [ { "name":"John", "age":94 },
          { "name":"Tomas" } ] """, SampleIsList=true>

          for item in People.GetSamples() do
          printf "%s " item.Name
          item.Age |> Option.iter (printf "(%d)")
          printfn ""


          The samples above have two records one includes age the other one doesn't. That makes the age field an int option.



          From the documentation: http://fsharp.github.io/FSharp.Data/library/JsonProvider.html






          share|improve this answer














          Instead of a url, you should provide some samples with and without rain:



          type People = JsonProvider<""" 
          [ { "name":"John", "age":94 },
          { "name":"Tomas" } ] """, SampleIsList=true>

          for item in People.GetSamples() do
          printf "%s " item.Name
          item.Age |> Option.iter (printf "(%d)")
          printfn ""


          The samples above have two records one includes age the other one doesn't. That makes the age field an int option.



          From the documentation: http://fsharp.github.io/FSharp.Data/library/JsonProvider.html







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 17:04

























          answered Nov 20 at 16:50









          AMieres

          2,100510




          2,100510












          • Thank you for the help!
            – Philip Nguyen
            Nov 20 at 19:08


















          • Thank you for the help!
            – Philip Nguyen
            Nov 20 at 19:08
















          Thank you for the help!
          – Philip Nguyen
          Nov 20 at 19:08




          Thank you for the help!
          – Philip Nguyen
          Nov 20 at 19:08


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397494%2fhow-are-f-type-providers-used-on-a-changing-rest-json-endpoint%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin