Skip to content

Commit

Permalink
Added clients python, node and adjusted client php
Browse files Browse the repository at this point in the history
  • Loading branch information
adriano-pinaffo committed Jul 3, 2022
1 parent a68147d commit 30ec2c7
Show file tree
Hide file tree
Showing 11 changed files with 20,255 additions and 62 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
vendor/
node_modules/
__pycache__
.env
.okta.env
*.swp
*.vim
138 changes: 138 additions & 0 deletions client/client.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
`
HOW TO RUN:
Have the server running first:
$ php -S 127.0.0.1:8000 -t public
List all users:
$ node client/client.mjs getallusers
# adjust the prints for the output
List a user ID:
$ node client/client.mjs getuser 2
# adjust the prints for the output
Add a user:
$ node client/client.mjs adduser '{"firstname": "Dennis", "lastname": "Ritchie", "firstparent_id": 4, "secondparent_id": 3}'
Update a user by ID:
$ node client/client.mjs updateuser 5 '{"firstname": "Dennys", "lastname": "Ritchie", "firstparent_id": 4, "secondparent_id": 3}
Delete a user by ID:
$ node client/client.mjs deleteuser 7
`;

import fetch from 'node-fetch';
import { argv } from 'process';
import { exit } from 'process';

// getallusers|getuser|adduser|updateuser|deleteuser

let url = 'http://127.0.0.1:8000/person';
const command = argv[2];
let id = null;
let input = null;
switch (command) {
case 'getallusers':
fetchget();
break;
case 'getuser':
id = typeof argv[3] == 'undefined' ? '' : '/' + argv[3];
fetchget(id);
break;
case 'adduser':
input = argv[3];
fetchpost(input);
break;
case 'updateuser':
id = typeof argv[3] == 'undefined' ? '' : '/' + argv[3];
input = argv[4];
fetchupdate(id, input);
break;
case 'deleteuser':
id = typeof argv[3] == 'undefined' ? '' : '/' + argv[3];
fetchdelete(id, input);
break;
default:
console.log('Command not found!');
exit(1);
}

async function fetchget(id) {
url += id ? id : '';
const data = await fetchapi(url, 'get', input);
//printJson(data); // print in json format
//printMatrix(data); // print 2-dimensional array
printCsv(data);
}

async function fetchpost(input) {
const data = await fetchapi(url, 'post', input);
console.log('ID:', data['id']);
}

async function fetchupdate(id, input) {
url += id;
const data = await fetchapi(url, 'put', input);
console.log('Rows:', data['rows']);
}

async function fetchdelete(id) {
url += id;
const data = await fetchapi(url, 'delete');
console.log('Rows:', data['rows']);
}

async function fetchapi(url, method, input) {
const options = loadOptions(method, input);
const response = await fetch(url, options);
if (!(response.status >= 200 && response.status < 300)) {
console.log(`Error: ${response.status} (${response.statusText})`);
exit(1);
}
const data = await response.json();
console.log(`Status code: ${response.status} (${response.statusText})`);
return data;
}

function printJson(data) {
console.log(data);
}

function printMatrix(data) {
const header = Object.keys(data[0]);
let table = [[...header]];
data.forEach(obj => {
let row = [];
header.forEach(item => {
row.push(obj[item]);
});
table.push(row);
});
console.log(table);
}

function printCsv(data) {
const header = Object.keys(data[0]);
console.log(header.join(', '));

data.forEach(obj => {
let row = [];
header.forEach(item => {
row.push(obj[item]);
});
console.log(row.join(','));
});
}

function loadOptions(method, input) {
const headers = {
'Content-Type': 'application/json',
};
const options = {
method: method,
headers: headers,
body: input,
};
return options;
}
71 changes: 53 additions & 18 deletions client/client.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
<?php
// HOW TO RUN:
// Have the server running first:
// $ php -S 127.0.0.1:8000 -t public
//
// Run it like below:
//
// To get all users:
// $php public/client.php getallusers
// $php public/client.php getuser
// $ php client/client.php getallusers
// $ php client/client.php getuser
// adjust the prints for the output
//
// To get one user with ID 10:
// $php public/client.php getuser 10
// # php client/client.php getuser 10
// adjust the prints for the output
//
// To add a user:
// $php public/client.php adduser '{"firstname": "Linus", "lastname": "Torvalds"}'
// $ php client/client.php adduser '{"firstname": "Linus", "lastname": "Torvalds"}'
//
// To update a user:
// $ php public/client.php updateuser 5 '{"firstname": "Steve", "lastname": "Jobs", "firstparent_id": 3, "secondparent_id": 2}'
// $ php client/client.php updateuser 5 '{"firstname": "Steve", "lastname": "Jobs", "firstparent_id": 3, "secondparent_id": 2}'
//
// To delete a user:
// $ php public/client.php deleteuser 7
// $ php client/client.php deleteuser 7

if ($_SERVER['argc'] == 1) {
echo "Not enough opitons\n";
echo "Not enough options\n";
showHelp();
exit(1);
}
Expand Down Expand Up @@ -96,11 +99,10 @@ function getUser($ch, $id=null) {
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
var_dump($result);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Status code: $status";
echo "\n";
$result = json_decode(curl_exec($ch));
//print_raw($result); // var_dump of result
//print_associative_array($result); // var_dump of associative array
print_csv($result); // echo of results in CSV format
}

function addUser($ch, $input) {
Expand All @@ -111,11 +113,12 @@ function addUser($ch, $input) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

$result = curl_exec($ch);
var_dump($result);
$result = json_decode(curl_exec($ch));
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Status code: $status";
echo "\n";
echo "ID: $result->id";
echo "\n";
}

function updateUser($ch, $id, $input) {
Expand All @@ -130,11 +133,12 @@ function updateUser($ch, $id, $input) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
var_dump($result);
$result = json_decode(curl_exec($ch));
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Status code: $status";
echo "\n";
echo "Rows: $result->rows";
echo "\n";
}

function deleteUser($ch, $id) {
Expand All @@ -144,14 +148,45 @@ function deleteUser($ch, $id) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
var_dump($result);
$result = json_decode(curl_exec($ch));
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Status code: $status";
echo "\n";
echo "Rows: $result->rows";
echo "\n";
}

function print_raw($data) {
var_dump($data);
}

function print_associative_array($data) {
$table = [];
foreach($data as $value) {
//var_dump($value);
$row = [];
foreach($value as $key => $item) {
$row[$key] = $item;
}
array_push($table, $row);
}
var_dump($table);
}

function print_csv($data) {
echo implode(',', array_keys(get_object_vars($data[0])));
echo "\n";
foreach($data as $value) {
//foreach($value as $item) {
//echo $item . ',';
//}
echo implode(',', array_values(get_object_vars($value)));
echo "\n";
}
}

function showHelp() {
echo $_SERVER['argv'][0] . " command [id] [data]\n";
echo " command = getallusers|getuser|adduser|updateuser|deleteuser\n";
}
?>
Loading

0 comments on commit 30ec2c7

Please sign in to comment.