From 1c4be692dca8bb675d2703d66820f9a0a2e8333e Mon Sep 17 00:00:00 2001 From: Joel Salisbury Date: Mon, 6 Feb 2017 17:13:55 -0500 Subject: [PATCH] more decision stuffz --- api/routes/decision.php | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/api/routes/decision.php b/api/routes/decision.php index 79492cb..df1870e 100644 --- a/api/routes/decision.php +++ b/api/routes/decision.php @@ -28,3 +28,96 @@ $response = $response->withJSON($response_json); return $response; }); + +function getFdctLoginInfo(){ + $endpoint = "http://api.fooducate.com/fdct/login/basic/?username=guest&password=&responseformat=json"; + $ch = curl_init($endpoint); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + "X-Fdct-Udid: RuddCenter_Test", + "X-Fdct-User-Agent: RuddCenter/1.0 ( OSX; 10.10.4; MacPro/2009; 1.0 )", + )); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + curl_close($ch); + $response = json_decode($response,true); + $importantStuff ['fdctuserid'] = $response['response']['body']['user']['@attributes']['id']; + $importantStuff ['fdctsessionid'] = $response['response']['head']['session']['@attributes']['id']; + $importantStuff ['fdcttimestamp'] = $response['response']['head']['request']['@attributes']['timestamp']; + return $importantStuff; +} +function getProductIngredients($fdct_id) { + $endpoint ="http://www.fooducate.com/internal/chef_client_proxy/get_product_by_id/".$fdct_id."/null"; + $ch = curl_init($endpoint); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + $res = json_decode($response,true); + $greds = $res['response']['body']['product_list']['products'][0]['product_ingredients']['original_ingredients']['@text']; + return $greds; + curl_close($ch); +} + + + + +$app->get('/getProductInfo/{upc}', function($request, $response) { + + $upc = $request->getAttribute('upc'); + + $fdctconfig = array(); + $fdctconfig['passphrase'] = "Partially Hydrogenated Vegetable"; + //login and get the session id and userid from the login + $fdctstuff = getFdctLoginInfo(); + // set the default timezone to match fooducate server + date_default_timezone_set("UTC"); + $tstamp = date(DATE_ISO8601, time()); + // build the token + $token = $fdctstuff['fdctuserid']. ":" . $fdctstuff['fdctsessionid'] . ":" . $tstamp; + // create the hash w the passphrase + $signature = base64_encode(hash_hmac('sha256', $token, $fdctconfig['passphrase'],true)); + // get ready and stuff + $endpoint = "http://api.fooducate.com/fdct/product/view/product/?reason=scan&upc=".$upc."&responseformat=json"; + $ch = curl_init($endpoint); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + //set the headers based on the api spec..right? WHY YOU NO WORK! + $headers = array( + "X-Fdct-Udid: RuddCenter_Test", + "X-Fdct-User-Agent: RuddCenter/1.0 ( OSX; 10.10.4; MacPro/2009; 1.0 )", + "X-Fdct-Signature-Method: SHA1-HMAC256", + "X-Fdct-Timestamp: ".$tstamp, + "X-Fdct-Session: " .$fdctstuff['fdctsessionid'], + "X-Fdct-Signature: ". $signature); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + // set our own response headers and output the result + // $response->headers->set('Content-Type', 'application/json'); + $fdctresponse = curl_exec($ch); + $res = json_decode($fdctresponse,true); + $fdct_id = $res['response']['body']['product_list']['products'][0]['@attributes']['id']; + $ingreds = getProductIngredients($fdct_id); + $res['response']['body']['product_list']['products'][0]['@attributes']['ingreds'] = $ingreds; + + + + $response_json = [ + "status" => [ + "code" => $response->getStatusCode(), + "message" => "OK" + ], + "data" => [$res] + ]; + + $response = $response->withJSON($response_json); + return $response; + + + curl_close($ch); +}); + + +$app->get('/getDecision/{upc}/{category}', function($request, $response) { + + // get the ingredients by UPC + // get the rules by category + + // for each rules, evaluate ingredients + // profit! +}); \ No newline at end of file