Skip to content

Instantly share code, notes, and snippets.

@AngelMunoz
Created February 4, 2020 15:48
Show Gist options
  • Save AngelMunoz/8d1d8ada72763c5c3646f836650281f5 to your computer and use it in GitHub Desktop.
Save AngelMunoz/8d1d8ada72763c5c3646f836650281f5 to your computer and use it in GitHub Desktop.
Use F# Type Providers with the unsplash API
module Unsplash
open FSharp.Data
open System.IO
open System.Net.Http
type RandomPhoto =
JsonProvider<"https://api.unsplash.com/photos/random?client_id=<YOUR ACCESS TOKEN>">
let getRandomPhoto = RandomPhoto.AsyncGetSample()
let getImageFromUrl (url: string) =
async {
use http = new HttpClient()
let! str = http.GetStreamAsync(url) |> Async.AwaitTask
let name = Path.GetTempFileName()
use file = File.Create(name)
str.CopyTo(file)
return name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment