var React = require("react");
var ReactRender = require("react-dom/server").renderToString;
var got = require('got');
var modelNumber = "MK0C2AM/A";
var zipCode = "94117";
var url = "http://www.apple.com/shop/retailStore/availabilitySearch?parts.0="+ encodeURIComponent(modelNumber) +"&zip=";
exports.tonicEndpoint = async function(aRequest, aResponse)
{
try
{
var zip = require('url').parse(aRequest.url, true).query.zip || "94117";
var json = require('url').parse(aRequest.url, true).query.json || false;
var results = await applePencilAvailability(zip);
if (json)
aResponse.end(JSON.stringify(results));
aResponse.setHeader('Content-Type', 'text/html');
aResponse.end(ReactRender(<ul>{ results.map(aResult => <li>{ aResult }</li>) }</ul>));
}
catch(e)
{
aResponse.end(e + "!!");
}
}
//await exports.handleRequest({ url: "https://a/?zip=94117" }, { end: console.log });
//JSON.stringify(await applePencilAvailability("94567"))