Created
February 9, 2018 21:31
-
-
Save TakesTheBiscuit/de3d66e4f71525523e7a9705bd762885 to your computer and use it in GitHub Desktop.
Payload templated inputs for semi dynamic API responses and input cycle
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
<?php | |
// P.D / TakesTheBiscuit proof of concept 9-2-2018 21:00 | |
// This small demo shows how you can send an input template | |
// .. back to an api client (for example) which can then be auto populated | |
Class micro_api { | |
public function getResponse($input_id) { | |
// in practice the payload->input_template can be driven now by | |
//.. a database table of input templates or even many rows in a HABTM system | |
$out = [ | |
'action_id'=>$input_id, | |
'payload'=> [ | |
'input_template'=> [ | |
'orderNumber', | |
'senderEmail', | |
'recipientEmail' | |
] | |
] | |
]; | |
return json_encode($out); | |
} | |
public function replyToRequest($reply_inputs) { | |
// this is just an assertion example, in practice a receiving API will handle these inputs | |
// for the sake of the demo we will just send it back as proof that this is now a closed loop | |
$out = ['you_said'=> $reply_inputs]; | |
return json_encode($out); | |
} | |
public function wasInputComplete($template, $inputs) { | |
$template_keys = array_values($template); | |
$input_keys = array_keys($inputs); | |
sort($template_keys); | |
sort($input_keys); | |
if ($template_keys == $input_keys) { | |
return true; | |
} | |
// fallthrough | |
return false; | |
} | |
public function whichKeysVary($template, $inputs) { | |
$template_keys = array_values($template); | |
$input_keys = array_keys($inputs); | |
sort($template_keys); | |
sort($input_keys); | |
return array_diff($template_keys, $input_keys); | |
} | |
} | |
//1. build a list of business data or localised data from say a database or order table into an array | |
$all_possible_inputs = [ | |
'senderEmail'=>'TakesTheBiscuit@greatfirewallofchina.ch', | |
'recipientEmail'=>'someone@hotmail.com', | |
'orderNumber'=>12342424 | |
]; | |
// 2. call our api | |
$dummy_api_call = new micro_api(); | |
$response = json_decode($dummy_api_call->getResponse(201)); | |
$reply_input = []; | |
if (isset($response->action_id)) { | |
if (isset($response->payload)) { | |
if (isset($response->payload->input_template)) { | |
// we have an input template! | |
foreach ($response->payload->input_template as $key) { | |
if (isset($all_possible_inputs[$key])) { | |
$reply_input[$key] = $all_possible_inputs[$key]; | |
} | |
} | |
} | |
} | |
} | |
// @todo some sort of UI/ visual tidy up | |
// 3. send back the templated payload as an input if its got something to say, else, log an exeption | |
if (count($reply_input)) { | |
// final call to our 'api' | |
$assertion = json_decode($dummy_api_call->replyToRequest($reply_input)); | |
// print_r($assertion); | |
$assert_same_keys = $dummy_api_call->wasInputComplete($response->payload->input_template, $reply_input); | |
if ($assert_same_keys) { | |
echo "Total match achieved. All inputs match all possible templated keys."; | |
} else { | |
echo "Not all values matched"; | |
echo '<hr>'; | |
echo '<h4>These were the variances/missing fields:</h4>'; | |
print_r($dummy_api_call->whichKeysVary($response->payload->input_template, $reply_input)); | |
echo '<hr>'; | |
echo '<h4>Template:</h4>'; | |
print_r($response->payload->input_template); | |
echo '<hr>'; | |
echo '<h4>You sent:</h4>'; | |
print_r($reply_input); | |
echo '<hr>'; | |
echo '<h4>We had these options our side:</h4>'; | |
print_r($all_possible_inputs); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment