Last active
September 28, 2016 02:07
-
-
Save eatonphil/ba660a7594dec2fb601a1cb1ac0208eb to your computer and use it in GitHub Desktop.
Getting Linode datacenters with Ponyo
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
(* | |
* Usage: ./a.out | |
* | |
* To build: | |
* Install Poly/ML and Ponyo. Build Ponyo using GNU make. | |
* You will need to have OpenSSL development headers. | |
* Build using ponyo-make: ponyo-make linode.sml. Run ./a.out. | |
*) | |
structure Datacenter = | |
struct | |
local | |
structure Format = Ponyo.Format; | |
open Ponyo.Encoding.Json.Marshall | |
infix >>= | |
in | |
type t = { id: string, | |
country: string, | |
label: string } | |
fun marshall (j: Ponyo.Encoding.Json.t) : (Ponyo.Encoding.Json.t * t) option = | |
marshallString (j, "id") >>= (fn (j, id) => | |
marshallString (j, "country") >>= (fn (j, country) => | |
marshallString (j, "label") >>= (fn (j, label) => | |
SOME (j, { id=id, | |
country=country, | |
label=label })))) | |
fun print (datacenter: t) : unit = | |
Format.println [#id datacenter, #label datacenter, #country datacenter] | |
end | |
end | |
structure Datacenters = | |
struct | |
local | |
open Ponyo.Encoding.Json.Marshall | |
infix >>= | |
in | |
type t = { page: int, | |
datacenters: Datacenter.t list, | |
total_pages: int, | |
total_results: int } | |
fun marshall (j: Ponyo.Encoding.Json.t) : t option = | |
marshallInt (j, "page") >>= (fn (j, page) => | |
marshallInt (j, "total_pages") >>= (fn (j, total_pages) => | |
marshallInt (j, "total_results") >>= (fn (j, total_results) => | |
marshallList (j, "datacenters", Datacenter.marshall) >>= (fn (j, datacenters) => | |
SOME { page=page, | |
total_pages=total_pages, | |
total_results=total_results, | |
datacenters=datacenters })))) | |
end | |
end | |
structure Main = | |
struct | |
local | |
structure Http = Ponyo.Net.Http | |
structure Https = Ponyo.Net.Https | |
in | |
fun main () = | |
let | |
val rsp = Https.Client.get ("api.alpha.linode.com/v4/datacenters", NONE); | |
val j = Ponyo.Encoding.Json.parse (#body rsp); | |
val datacenters = Datacenters.marshall (j); | |
in | |
map Datacenter.print (#datacenters (valOf datacenters)); | |
() | |
end | |
end | |
end | |
val main = Main.main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment