Service functions

This commit is contained in:
dev-chiefworks
2022-03-29 22:34:07 -04:00
parent 6d25fc0159
commit 08a15c5984
4 changed files with 251 additions and 2 deletions
+92 -1
View File
@@ -2,6 +2,14 @@
namespace App\Controllers;
use CodeIgniter\HTTP\URI;
use App\Services\FloatLogin;
use App\Services\UserProfile;
use GuzzleHttp\Client as HTTPClient;
use Exception;
class Savvy extends BaseController
{
@@ -15,6 +23,18 @@ class Savvy extends BaseController
public function user(){
header("Access-Control-Allow-Origin: *");
header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, client_id");
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS");
header('Content-type: application/json');
// $res = FloatLogin::floatLoginUser();
if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
exit();
}
// what is the endpoint
$uri = current_url(true);
$pieces = explode("/", $uri);
@@ -40,6 +60,51 @@ class Savvy extends BaseController
);
if(array_key_exists( $endpoint, $endpoints)){
// echo "EXYTACT INPUT DATA HERE";
}
else{
http_response_code(404);
// tell the user product does not exist
echo json_encode(array("message" => "Product does not exist."));
}
if (!isset($endpoints[$endpoint])) {
header('HTTP/1.1 400 Bad Request');
header('Status: 400 Bad Request');
echo "{\"status\":\"Invalid endpoint url\"}";
exit();
}
$methods = $endpoints[$endpoint];
if (array_search($_SERVER['REQUEST_METHOD'], $methods)===false) {
header('HTTP/1.1 405 Method Not Allowed');
header('Status: 405 Method Not Allowed');
echo "{\"status\":\"Invalid request method\"}";
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($endpoint == "uploadfile") {
upload_file_call();
exit();
} else {
$in = $this->flatten(json_decode(file_get_contents('php://input'), true));
}
}
if ($_SERVER["REQUEST_METHOD"] == "PUT") {
parse_str(file_get_contents('php://input'), $in);
}
$in["loc"] = $_SERVER["REMOTE_ADDR"]; // get who is connecting IP
$in["pid"] = 100;
$res=[];
switch ($endpoint) {
case 'getdrycleanservicelist': $in["action"] = SAVVYEXT_USER_DRYCLIST;
@@ -51,6 +116,16 @@ class Savvy extends BaseController
$in["loc"] = $_SERVER["REMOTE_ADDR"];
break;
case 'userlogin': $in["action"] = SAVVYEXT_USER_LOGIN;
$res = FloatLogin::floatLoginUser();
if (!empty($res)){
$data = json_decode($res, TRUE);
if (isset($data['accessToken'])){
$res = UserProfile::floatUserProfile($data['accessToken']);
}
}
break;
case 'updateprofile': $in["action"] = SAVVYEXT_USER_PROFILE;
$in["street1"] = $in["streetaddress"];
@@ -91,7 +166,23 @@ class Savvy extends BaseController
break;
}
echo json_encode($res);
exit();
}
private function flatten($data, $parentkey="") {
$result = array();
if (!is_array($data) ){
return [];
}
foreach ($data as $key=>$val) {
if (is_array($val)) {
$result = array_merge($result, flatten($val, $parentkey.$key."_"));
} else {
$result[$parentkey.$key] = $val;
}
}
return $result;
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace App\Services;
use GuzzleHttp\Client as HTTPClient;
class FloatLogin
{
public static function floatLoginUser()
{
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://10.20.30.26:8000/api/v1/user/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"test@float.sg\",\"password\":\"111111\",\"sessionid\":\"DUMMYSESSION\"}");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Devicetoken: ';
$headers[] = 'X-Float-Device-Location-Latitude: 0';
$headers[] = 'Authorization: Token 99dfe35fcb7de1ee';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json, text/plain, */*';
$headers[] = 'X-Float-Device-Location-Longitude: 0';
$headers[] = 'Referer: http://10.0.0.252:8100/';
$headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36';
$headers[] = 'X-Session-Id: ';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
// $apiEndpointsConfig = config('ApiEndpoints');
// $baseUrl = $apiEndpointsConfig->baseUrl;
// $username = $apiEndpointsConfig->clientCredentials['username'];
// $password = $apiEndpointsConfig->clientCredentials['password'];
$client = new HTTPClient();
$baseUrl = "http://10.20.30.26:8000/api/v1/user";
$headers = array();
$headers[] = 'X-Devicetoken: ';
$headers[] = 'X-Float-Device-Location-Latitude: 0';
$headers[] = 'Authorization: Token 99dfe35fcb7de1ee';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json, text/plain, */*';
$headers[] = 'X-Float-Device-Location-Longitude: 0';
$headers[] = 'Referer: http://10.0.0.252:8100/';
$headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36';
$headers[] = 'X-Session-Id: ';
$response = $client->request(
'POST',
"{$baseUrl}/login",
[
'headers' => $headers,
'body' => json_encode( ["username"=>"test@float.sg",
"password"=>"111111",
"sessionid"=>"DUMMYSESSION"] )
]
);
//$response = json_decode($response->getBody(), true);
return $response;
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace App\Services;
use GuzzleHttp\Client as HTTPClient;
class UserProfile
{
public static function floatUserProfile($token)
{
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://10.20.30.26:8000/api/v1/user/profile');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"test@float.sg\",\"password\":\"111111\",\"sessionid\":\"DUMMYSESSION\"}");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Devicetoken: ';
$headers[] = 'X-Float-Device-Location-Latitude: 0';
$headers[] = "Authorization: Token {$token}";
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json, text/plain, */*';
$headers[] = 'X-Float-Device-Location-Longitude: 0';
$headers[] = 'Referer: http://10.0.0.252:8100/';
$headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36';
$headers[] = 'X-Session-Id: ';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
// $apiEndpointsConfig = config('ApiEndpoints');
// $baseUrl = $apiEndpointsConfig->baseUrl;
// $username = $apiEndpointsConfig->clientCredentials['username'];
// $password = $apiEndpointsConfig->clientCredentials['password'];
$client = new HTTPClient();
$baseUrl = "http://10.20.30.26:8000/api/v1/profile";
$headers = array();
$headers[] = 'X-Devicetoken: ';
$headers[] = 'X-Float-Device-Location-Latitude: 0';
$headers[] = "Authorization: Token {$token}";
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json, text/plain, */*';
$headers[] = 'X-Float-Device-Location-Longitude: 0';
$headers[] = 'Referer: http://10.0.0.252:8100/';
$headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36';
$headers[] = 'X-Session-Id: ';
$response = $client->request(
'POST',
"{$baseUrl}/login",
[
'headers' => $headers,
'body' => json_encode( ["username"=>"test@float.sg",
"password"=>"111111",
"sessionid"=>"DUMMYSESSION"] )
]
);
//$response = json_decode($response->getBody(), true);
return $response;
}
}
+1 -1
View File
@@ -13,7 +13,7 @@
"kint-php/kint": "^4.0",
"laminas/laminas-escaper": "^2.9",
"psr/log": "^1.1",
"guzzlehttp/guzzle": "^7.0"
"guzzlehttp/guzzle": "^7.3"
},
"require-dev": {
"codeigniter/coding-standard": "^1.1",