Created
April 10, 2018 04:51
-
-
Save Jwsonic/9840aece63be89c0d7c1dc406186b175 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 type ApiClient = {let getFile: unit => string;}; | |
module FakeApiClient: ApiClient = { | |
let getFile = () => "cat.jpg"; | |
}; | |
let unwrapUnsafely = data => | |
switch (data) { | |
| Some(v) => v | |
| None => raise(Invalid_argument("unwrapUnsafely called on None")) | |
}; | |
module HttpApiClient: ApiClient = { | |
let getFile = () => | |
Js.Promise.( | |
Fetch.fetch("https://aws.random.cat/meow") | |
|> then_(Fetch.Response.json) | |
|> then_(json => Js.Json.decodeObject(json) |> resolve) | |
|> then_(opt => unwrapUnsafely(opt) |> resolve) | |
|> then_(~file => file |> resolve) | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment