Created
February 4, 2020 15:48
-
-
Save AngelMunoz/8d1d8ada72763c5c3646f836650281f5 to your computer and use it in GitHub Desktop.
Use F# Type Providers with the unsplash API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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